private static void addProperty(ClassNode cNode, PropertyNode pNode) { final FieldNode fn = pNode.getField(); cNode.getFields().remove(fn); cNode.addProperty(pNode.getName(), pNode.getModifiers() | ACC_FINAL, pNode.getType(), pNode.getInitialExpression(), pNode.getGetterBlock(), pNode.getSetterBlock()); final FieldNode newfn = cNode.getField(fn.getName()); cNode.getFields().remove(newfn); cNode.addField(fn); }
public PropertyNode addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) { for (PropertyNode pn : getProperties()) { if (pn.getName().equals(name)) { if (pn.getInitialExpression() == null && initialValueExpression != null) pn.getField().setInitialValueExpression(initialValueExpression); if (pn.getGetterBlock() == null && getterBlock != null) pn.setGetterBlock(getterBlock); if (pn.getSetterBlock() == null && setterBlock != null) pn.setSetterBlock(setterBlock); return pn; } } PropertyNode node = new PropertyNode(name, modifiers, type, redirect(), initialValueExpression, getterBlock, setterBlock); addProperty(node); return node; }
public void testProperties() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, ClassHelper.OBJECT_TYPE); classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); assertField(fooClass, "bar", 0, ClassHelper.STRING_TYPE); assertGetProperty(bean, "bar", null); assertSetProperty(bean, "bar", "newValue"); }
public void testInheritedProperties() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC + ACC_SUPER, ClassHelper.make(DummyBean.class)); classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); assertField(fooClass, "bar", 0, ClassHelper.STRING_TYPE); assertGetProperty(bean, "name", "James"); assertSetProperty(bean, "name", "Bob"); assertGetProperty(bean, "bar", null); assertSetProperty(bean, "bar", "newValue"); } }
protected void assertIterate(String methodName, Expression listExpression) throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null)); Statement loopStatement = createPrintlnStatement(new VariableExpression("i")); BlockStatement block = new BlockStatement(); block.addStatement(new ExpressionStatement(new DeclarationExpression(new VariableExpression("list"), Token.newSymbol("=", 0, 0), listExpression))); block.addStatement(new ForStatement(new Parameter(ClassHelper.DYNAMIC_TYPE, "i"), new VariableExpression("list"), loopStatement)); classNode.addMethod(new MethodNode(methodName, ACC_PUBLIC, ClassHelper.VOID_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block)); Class fooClass = loadClass(classNode); assertTrue("Loaded a new class", fooClass != null); Object bean = fooClass.newInstance(); assertTrue("Managed to create bean", bean != null); System.out.println("################ Now about to invoke method"); try { InvokerHelper.invokeMethod(bean, methodName, null); } catch (InvokerInvocationException e) { System.out.println("Caught: " + e.getCause()); e.getCause().printStackTrace(); fail("Should not have thrown an exception"); } System.out.println("################ Done"); }
classNode.addProperty(propertyNode); } else { fieldNode.setModifiers(modifiers);
public void testLoop() throws Exception { ClassNode classNode = new ClassNode("Foo", ACC_PUBLIC, ClassHelper.OBJECT_TYPE); classNode.addConstructor(new ConstructorNode(ACC_PUBLIC, null)); classNode.addProperty(new PropertyNode("bar", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null)); classNode.addProperty(new PropertyNode("result", ACC_PUBLIC, ClassHelper.STRING_TYPE, classNode, null, null, null));
/** * Adds the property to the class node passed as first argument * * @param classNode the class we want to add the property to * @param propertyNode the property we want to add * @since 0.3.0 */ public void addProperty(final ClassNode classNode, final PropertyNode propertyNode) { classNode.addProperty(propertyNode); }
/** * Adds a property to the class node passed as first argument only * if it wasn't present in the first place * * @param classNode the class we want to add the property to * @param propertyNode the property we want to add * @since 0.3.0 */ public void addPropertyIfNotPresent(final ClassNode classNode, final PropertyNode propertyNode) { if (!classNode.hasProperty(propertyNode.getName())) { classNode.addProperty(propertyNode); } }
public PropertyNode addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) { for (Iterator iter = getProperties().iterator(); iter.hasNext();) { PropertyNode pn = (PropertyNode) iter.next(); if (pn.getName().equals(name)) return pn; } PropertyNode node = new PropertyNode(name, modifiers, type, redirect(), initialValueExpression, getterBlock, setterBlock); addProperty(node); return node; }
public static void injectConstant(ClassNode classNode, String propertyName, Class<?> propertyClass, Object value) { final boolean hasProperty = hasOrInheritsProperty(classNode, propertyName); if (!hasProperty) { // inject into furthest relative // ClassNode parent = getFurthestParent(classNode); Expression initialExpression = new ConstantExpression(value); classNode.addProperty(propertyName, Modifier.PUBLIC | Modifier.FINAL, new ClassNode(propertyClass), initialExpression, null, null); } }
public static void injectProperty(ClassNode classNode, String propertyName, int modifiers, ClassNode propertyClass, Object value) { if (!hasOrInheritsProperty(classNode, propertyName)) { // inject into furthest relative ClassNode parent = getFurthestParent(classNode); Expression initialExpression = value instanceof Expression ? (Expression) value : null; if (value != null && initialExpression == null) { initialExpression = new ConstantExpression(value); } parent.addProperty(propertyName, modifiers, propertyClass, initialExpression, null, null); } }
private void addProperty(ClassNode cNode, PropertyNode pNode) { final FieldNode fn = pNode.getField(); cNode.getFields().remove(fn); cNode.addProperty(pNode.getName(), pNode.getModifiers() | ACC_FINAL, pNode.getType(), pNode.getInitialExpression(), pNode.getGetterBlock(), pNode.getSetterBlock()); final FieldNode newfn = cNode.getField(fn.getName()); cNode.getFields().remove(newfn); cNode.addField(fn); }
private void addProperty(ClassNode cNode, PropertyNode pNode) { final FieldNode fn = pNode.getField(); cNode.getFields().remove(fn); cNode.addProperty(pNode.getName(), pNode.getModifiers() | ACC_FINAL, pNode.getType(), pNode.getInitialExpression(), pNode.getGetterBlock(), pNode.getSetterBlock()); final FieldNode newfn = cNode.getField(fn.getName()); cNode.getFields().remove(newfn); cNode.addField(fn); }
private void addProperty(ClassNode cNode, PropertyNode pNode) { final FieldNode fn = pNode.getField(); cNode.getFields().remove(fn); cNode.addProperty(pNode.getName(), pNode.getModifiers() | ACC_FINAL, pNode.getType(), pNode.getInitialExpression(), pNode.getGetterBlock(), pNode.getSetterBlock()); final FieldNode newfn = cNode.getField(fn.getName()); cNode.getFields().remove(newfn); cNode.addField(fn); }
private FieldNode setMetaClassFieldIfNotExists(ClassNode node, FieldNode metaClassField){ if (metaClassField != null) return metaClassField; StaticMethodCallExpression initMetaClassCall = new StaticMethodCallExpression( ClassHelper.make(ScriptBytecodeAdapter.class), "initMetaClass", VariableExpression.THIS_EXPRESSION); PropertyNode metaClassProperty = node.addProperty("metaClass", ACC_PUBLIC, ClassHelper.make(MetaClass.class), initMetaClassCall, null, null); metaClassProperty.setSynthetic(true); FieldNode field = metaClassProperty.getField(); field.setModifiers(field.getModifiers() | ACC_TRANSIENT); return field; }
@Override public void visitBlockStatement(BlockStatement block) { if (block.isEmpty() || this.xformed) { return; } ClosureExpression closure = beans(block); if (closure != null) { // Add a marker interface to the current script this.classNode.addInterface(ClassHelper.make(SOURCE_INTERFACE)); // Implement the interface by adding a public read-only property with the // same name as the method in the interface (getBeans). Make it return the // closure. this.classNode.addProperty( new PropertyNode(BEANS, Modifier.PUBLIC | Modifier.FINAL, ClassHelper.CLOSURE_TYPE.getPlainNodeReference(), this.classNode, closure, null, null)); // Only do this once per class this.xformed = true; } }
public PropertyNode addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) { for (PropertyNode pn : getProperties()) { if (pn.getName().equals(name)) { if (pn.getInitialExpression() == null && initialValueExpression != null) pn.getField().setInitialValueExpression(initialValueExpression); if (pn.getGetterBlock() == null && getterBlock != null) pn.setGetterBlock(getterBlock); if (pn.getSetterBlock() == null && setterBlock != null) pn.setSetterBlock(setterBlock); return pn; } } PropertyNode node = new PropertyNode(name, modifiers, type, redirect(), initialValueExpression, getterBlock, setterBlock); addProperty(node); return node; }
public PropertyNode addProperty(String name, int modifiers, ClassNode type, Expression initialValueExpression, Statement getterBlock, Statement setterBlock) { for (Object o : getProperties()) { PropertyNode pn = (PropertyNode) o; if (pn.getName().equals(name)) { if (pn.getInitialExpression() == null && initialValueExpression != null) pn.getField().setInitialValueExpression(initialValueExpression); if (pn.getGetterBlock() == null && getterBlock != null) pn.setGetterBlock(getterBlock); if (pn.getSetterBlock() == null && setterBlock != null) pn.setSetterBlock(setterBlock); return pn; } } PropertyNode node = new PropertyNode(name, modifiers, type, redirect(), initialValueExpression, getterBlock, setterBlock); addProperty(node); return node; }
@Override public void visitBlockStatement(BlockStatement block) { if (block.isEmpty() || this.xformed) { return; } ClosureExpression closure = ratpack(block); if (closure != null) { this.classNode.addAnnotation(new AnnotationNode(ClassHelper .make("Component"))); // Add a marker interface to the current script this.classNode.addInterface(ClassHelper.make(SOURCE_INTERFACE)); // Implement the interface by adding a public read-only property with the // same name as the method in the interface (getRatpack). Make it return the // closure. this.classNode.addProperty(new PropertyNode(RATPACK, Modifier.PUBLIC | Modifier.FINAL, ClassHelper.CLOSURE_TYPE .getPlainNodeReference(), this.classNode, closure, null, null)); // Only do this once per class this.xformed = true; } }