/** * Adds a <code>toString()</code> method to a class that returns a fixed, pre-computed value. * * @param classFab * ClassFab used to construct the new class. * @param toStringResult * fixed result to be returned by the method. */ public static void addToStringMethod(ClassFab classFab, String toStringResult) { StringBuffer buffer = new StringBuffer("return "); buffer.append(QUOTE); buffer.append(toStringResult); buffer.append(QUOTE); buffer.append(";"); classFab.addMethod(Modifier.PUBLIC, new MethodSignature(String.class, "toString", null, null), buffer.toString()); }
private void finalizeIncompleteMethods() { Iterator i = _incompleteMethods.entrySet().iterator(); while(i.hasNext()) { Map.Entry e = (Map.Entry) i.next(); MethodSignature sig = (MethodSignature) e.getKey(); BodyBuilder builder = (BodyBuilder) e.getValue(); // Each BodyBuilder is created and given a begin(), this is // the matching end() builder.end(); _classFab.addMethod(Modifier.PUBLIC, sig, builder.toString()); } }
private void finalizeIncompleteMethods() { Iterator i = _incompleteMethods.entrySet().iterator(); while(i.hasNext()) { Map.Entry e = (Map.Entry) i.next(); MethodSignature sig = (MethodSignature) e.getKey(); BodyBuilder builder = (BodyBuilder) e.getValue(); // Each BodyBuilder is created and given a begin(), this is // the matching end() builder.end(); _classFab.addMethod(Modifier.PUBLIC, sig, builder.toString()); } }
cf.addMethod(Modifier.PUBLIC, m, body.toString());
void createLocalReferences(OgnlContext context, ClassFab classFab, Class[] params) throws CannotCompileException, NotFoundException { Map referenceMap = context.getLocalReferences(); if (referenceMap == null || referenceMap.size() < 1) return; Iterator it = referenceMap.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); LocalReference ref = (LocalReference) referenceMap.get(key); String widener = ref.getType().isPrimitive() ? " " : " ($w) "; String body = "{"; body += " return " + widener + ref.getExpression() + ";"; body += "}"; body = body.replaceAll("\\.\\.", "."); if (_log.isDebugEnabled()) _log.debug("createLocalReferences() body is:\n" + body); MethodSignature method = new MethodSignature(ref.getType(), ref.getName(), params, null); classFab.addMethod(Modifier.PUBLIC, method, body); it.remove(); } }
public void addMethod(int modifier, MethodSignature sig, String methodBody, Location location) { Defense.notNull(sig, "sig"); Defense.notNull(methodBody, "methodBody"); Defense.notNull(location, "location"); Location existing = (Location) _methods.get(sig); if (existing != null) throw new ApplicationRuntimeException(EnhanceMessages.methodConflict(sig, existing), location, null); _methods.put(sig, location); _classFab.addMethod(modifier, sig, methodBody); }
void createLocalReferences(OgnlContext context, ClassFab classFab, Class[] params) throws CannotCompileException, NotFoundException { Map referenceMap = context.getLocalReferences(); if (referenceMap == null || referenceMap.size() < 1) return; Iterator it = referenceMap.keySet().iterator(); while (it.hasNext()) { String key = (String) it.next(); LocalReference ref = (LocalReference) referenceMap.get(key); String widener = ref.getType().isPrimitive() ? " " : " ($w) "; String body = "{"; body += " return " + widener + ref.getExpression() + ";"; body += "}"; body = body.replaceAll("\\.\\.", "."); if (_log.isDebugEnabled()) _log.debug("createLocalReferences() body is:\n" + body); MethodSignature method = new MethodSignature(ref.getType(), ref.getName(), params, null); classFab.addMethod(Modifier.PUBLIC, method, body); it.remove(); } }
public void addMethod(int modifier, MethodSignature sig, String methodBody, Location location) { Defense.notNull(sig, "sig"); Defense.notNull(methodBody, "methodBody"); Defense.notNull(location, "location"); Location existing = (Location) _methods.get(sig); if (existing != null) throw new ApplicationRuntimeException(EnhanceMessages.methodConflict(sig, existing), location, null); _methods.put(sig, location); _classFab.addMethod(modifier, sig, methodBody); }
/** @since 1.1 */ private void addSerializable(String pointId) { _classFab.addInterface(Serializable.class); BodyBuilder bb = new BodyBuilder(); bb.add( "return {0}.getServiceSerializationSupport().getServiceTokenForService(\"{1}\");", ServiceSerializationHelper.class.getName(), pointId); MethodSignature sig = new MethodSignature(Object.class, "writeReplace", null, null); _classFab.addMethod(Modifier.PRIVATE, sig, bb.toString()); }
/** @since 1.1 */ private static void addDelegateAccessor(ClassFab classFab, ServicePoint servicePoint, Object delegate) { classFab.addField("_shutdown", boolean.class); Class delegateClass = ClassFabUtils.getInstanceClass(delegate, servicePoint .getServiceInterface()); classFab.addField("_delegate", delegateClass); classFab.addConstructor(new Class[] { delegateClass }, null, "{ super(); _delegate = $1; }"); classFab.addInterface(RegistryShutdownListener.class); if( RegistryShutdownListener.class.isAssignableFrom( delegateClass ) ) { classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, "registryDidShutdown", null, null), "{ _delegate.registryDidShutdown(); _delegate = null; _shutdown = true; }"); } else { classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, "registryDidShutdown", null, null), "{ _delegate = null; _shutdown = true; }"); } BodyBuilder builder = new BodyBuilder(); builder.begin(); builder.addln("if (_shutdown)"); builder.addln(" throw org.apache.hivemind.HiveMind#createRegistryShutdownException();"); builder.add("return _delegate;"); builder.end(); classFab.addMethod(Modifier.FINAL | Modifier.PRIVATE, new MethodSignature(delegateClass, DELEGATE_ACCESSOR_METHOD_NAME, null, null), builder.toString()); }
generateClassFab(compiled).addMethod(Modifier.PUBLIC, valueGetter, getBody); } catch (Throwable t) generateClassFab(compiled).addMethod(Modifier.PUBLIC, expressionSetter, "{ _node = $1; }"); generateClassFab(compiled).addMethod(Modifier.PUBLIC, expressionSetter, "{ _node = $1; }"); generateClassFab(compiled).addMethod(Modifier.PUBLIC, valueSetter, setBody);
/** * Creates a method that delegates to the _delegate object; this is used for * methods that are not logged. */ private void addPassThruMethodImplementation(ClassFab classFab, MethodSignature sig) { BodyBuilder builder = new BodyBuilder(); builder.begin(); builder.add("return ($r) _delegate."); builder.add(sig.getName()); builder.addln("($$);"); builder.end(); classFab.addMethod(Modifier.PUBLIC, sig, builder.toString()); }
generateClassFab(compiled).addMethod(Modifier.PUBLIC, valueGetter, getBody); } catch (Throwable t) generateClassFab(compiled).addMethod(Modifier.PUBLIC, expressionSetter, "{ _node = $1; }"); generateClassFab(compiled).addMethod(Modifier.PUBLIC, expressionSetter, "{ _node = $1; }"); generateClassFab(compiled).addMethod(Modifier.PUBLIC, valueSetter, setBody);
/** * We construct a method that always goes through this service model's * {@link #getServiceImplementationForCurrentThread())} method. */ private static void addServiceAccessor(ClassFab classFab, String serviceModelMethodName, ServicePoint servicePoint) { Class serviceInterface = servicePoint.getServiceInterface(); classFab.addField(SERVICE_ACCESSOR_METHOD_NAME, serviceInterface); BodyBuilder builder = new BodyBuilder(); builder.begin(); builder.add("return ("); builder.add(serviceInterface.getName()); builder.add(") _serviceModel."); builder.add(serviceModelMethodName); builder.add("();"); builder.end(); classFab.addMethod(Modifier.PRIVATE | Modifier.FINAL, new MethodSignature(serviceInterface, SERVICE_ACCESSOR_METHOD_NAME, null, null), builder.toString()); } }
classFab.addMethod(Modifier.PUBLIC | Modifier.FINAL, new MethodSignature(void.class, "registryDidShutdown", null, null), "{ _shutdown = true; }"); classFab.addMethod( Modifier.PUBLIC | Modifier.SYNCHRONIZED | Modifier.FINAL, new MethodSignature(void.class, "_setInner", new Class[] builder.end(); classFab.addMethod(Modifier.PRIVATE, new MethodSignature(serviceInterface, "_getInner", null, null), builder.toString());
builder.addln("($$);"); builder.end(); _classFab.addMethod(Modifier.PUBLIC, m, builder.toString());
MethodFab methodFab = classFab.addMethod(Modifier.PUBLIC, sig, builder.toString());