/** * Adds a ClassNode directly to the unit (ie. without source). * WARNING: the source is needed for error reporting, using * this method without setting a SourceUnit will cause * NullPinterExceptions */ public void addClassNode(ClassNode node) { ModuleNode module = new ModuleNode(this.ast); this.ast.addModule(module); module.addClass(node); }
private static void createBuilderFactoryMethod(AnnotationNode anno, ClassNode buildee, ClassNode builder) { buildee.getModule().addClass(builder); buildee.addMethod(createBuilderMethod(anno, builder)); }
private void buildCommon(ClassNode buildee, AnnotationNode anno, List<FieldNode> fieldNodes, ClassNode builder) { String prefix = getMemberStringValue(anno, "prefix", ""); String buildMethodName = getMemberStringValue(anno, "buildMethodName", "create"); createBuilderConstructors(builder, buildee, fieldNodes); buildee.getModule().addClass(builder); String builderMethodName = getMemberStringValue(anno, "builderMethodName", "createInitializer"); buildee.addMethod(createBuilderMethod(buildMethodName, builder, fieldNodes.size(), builderMethodName)); for (int i = 0; i < fieldNodes.size(); i++) { builder.addMethod(createBuilderMethodForField(builder, fieldNodes, prefix, i)); } builder.addMethod(createBuildMethod(builder, buildMethodName, fieldNodes)); }
cu.addSource(su); cu.compile(Phases.CONVERSION); su.getAST().addClass(cn); cu.compile(Phases.CLASS_GENERATION); @SuppressWarnings("unchecked")
helper = new InnerClassNode(cn.getPlainNodeReference(), cn.getName() + "$CollectorHelper", ACC_PUBLIC | ACC_STATIC | ACC_FINAL, ClassHelper.OBJECT_TYPE.getPlainNodeReference()); cn.getModule().addClass(helper); helper.addAnnotation(new AnnotationNode(COMPILESTATIC_CLASSNODE)); MethodNode serializeClass = collector.getClassNode().getMethod("serializeClass", Parameter.EMPTY_ARRAY);
private static void addHolderClassIdiomBody(BlockStatement body, FieldNode fieldNode, Expression initExpr) { final ClassNode declaringClass = fieldNode.getDeclaringClass(); final ClassNode fieldType = fieldNode.getType(); final int visibility = ACC_PRIVATE | ACC_STATIC; final String fullName = declaringClass.getName() + "$" + fieldType.getNameWithoutPackage() + "Holder_" + fieldNode.getName().substring(1); final InnerClassNode holderClass = new InnerClassNode(declaringClass, fullName, visibility, ClassHelper.OBJECT_TYPE); final String innerFieldName = "INSTANCE"; // we have two options: // (1) embed initExpr within holder class but redirect field access/method calls to declaring class members // (2) keep initExpr within a declaring class method that is only called by the holder class // currently we have gone with (2) for simplicity with only a slight memory footprint increase in the declaring class final String initializeMethodName = (fullName + "_initExpr").replace('.', '_'); addGeneratedMethod(declaringClass, initializeMethodName, ACC_PRIVATE | ACC_STATIC | ACC_FINAL, fieldType, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, returnS(initExpr)); holderClass.addField(innerFieldName, ACC_PRIVATE | ACC_STATIC | ACC_FINAL, fieldType, callX(declaringClass, initializeMethodName)); final Expression innerField = propX(classX(holderClass), innerFieldName); declaringClass.getModule().addClass(holderClass); body.addStatement(returnS(innerField)); }
protected Expression anonymousInnerClassDef(AST node) { ClassNode oldNode = classNode; ClassNode outerClass = getClassOrScript(oldNode); String fullName = outerClass.getName() + '$' + innerClassCounter; innerClassCounter++; if (enumConstantBeingDef) { classNode = new EnumConstantClassNode(outerClass, fullName, Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE); } else { classNode = new InnerClassNode(outerClass, fullName, Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE); } ((InnerClassNode) classNode).setAnonymous(true); classNode.setEnclosingMethod(methodNode); assertNodeType(OBJBLOCK, node); objectBlock(node); output.addClass(classNode); AnonymousInnerClassCarrier ret = new AnonymousInnerClassCarrier(); ret.innerClass = classNode; classNode = oldNode; return ret; }
protected void enumDef(AST enumNode) { assertNodeType(ENUM_DEF, enumNode); List<AnnotationNode> annotations = new ArrayList<AnnotationNode>(); AST node = enumNode.getFirstChild(); int modifiers = Opcodes.ACC_PUBLIC; if (isType(MODIFIERS, node)) { modifiers = modifiers(node, annotations, modifiers); node = node.getNextSibling(); } String name = identifier(node); node = node.getNextSibling(); ClassNode[] interfaces = interfaces(node); node = node.getNextSibling(); boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0); modifiers &= ~Opcodes.ACC_SYNTHETIC; String enumName = (classNode != null ? name : dot(getPackageName(), name)); ClassNode enumClass = EnumHelper.makeEnumNode(enumName, modifiers, interfaces, classNode); enumClass.setSyntheticPublic(syntheticPublic); ClassNode oldNode = classNode; enumClass.addAnnotations(annotations); classNode = enumClass; configureAST(classNode, enumNode); assertNodeType(OBJBLOCK, node); objectBlock(node); classNode = oldNode; output.addClass(enumClass); }
} else { thatType = new InnerClassNode(node.redirect(), node.getName() + "$1", ACC_STATIC | ACC_SYNTHETIC, ClassHelper.OBJECT_TYPE); node.getModule().addClass(thatType);
output.addClass(classNode); classNode = null;
private static void createComparatorFor(ClassNode classNode, PropertyNode property, boolean reversed) { String propName = StringGroovyMethods.capitalize((CharSequence) property.getName()); String className = classNode.getName() + "$" + propName + "Comparator"; ClassNode superClass = makeClassSafeWithGenerics(AbstractComparator.class, classNode); InnerClassNode cmpClass = new InnerClassNode(classNode, className, ACC_PRIVATE | ACC_STATIC, superClass); classNode.getModule().addClass(cmpClass); addGeneratedMethod(cmpClass, "compare", ACC_PUBLIC, ClassHelper.int_TYPE, params(param(newClass(classNode), ARG0), param(newClass(classNode), ARG1)), ClassNode.EMPTY_ARRAY, createCompareMethodBody(property, reversed) ); String fieldName = "this$" + propName + "Comparator"; // private final Comparator this$<property>Comparator = new <type>$<property>Comparator(); FieldNode cmpField = classNode.addField( fieldName, ACC_STATIC | ACC_FINAL | ACC_PRIVATE | ACC_SYNTHETIC, COMPARATOR_TYPE, ctorX(cmpClass)); addGeneratedMethod(classNode, "comparatorBy" + propName, ACC_PUBLIC | ACC_STATIC, COMPARATOR_TYPE, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, returnS(fieldX(cmpField)) ); }
output.addClass(classNode);
output.addClass(classNode);
/** * Adds a ClassNode directly to the unit (ie. without source). * WARNING: the source is needed for error reporting, using * this method without setting a SourceUnit will cause * NullPinterExceptions */ public void addClassNode(ClassNode node) { ModuleNode module = new ModuleNode(this.ast); this.ast.addModule(module); module.addClass(node); }
/** * Adds a ClassNode directly to the unit (ie. without source). * WARNING: the source is needed for error reporting, using * this method without setting a SourceUnit will cause * NullPinterExceptions */ public void addClassNode(ClassNode node) { ModuleNode module = new ModuleNode(this.ast); this.ast.addModule(module); module.addClass(node); }
/** * Adds a ClassNode directly to the unit (ie. without source). * WARNING: the source is needed for error reporting, using * this method without setting a SourceUnit will cause * NullPinterExceptions */ public void addClassNode(ClassNode node) { ModuleNode module = new ModuleNode(this.ast); this.ast.addModule(module); module.addClass(node); }
/** * Adds a ClassNode directly to the unit (ie. without source). * WARNING: the source is needed for error reporting, using * this method without setting a SourceUnit will cause * NullPinterExceptions */ public void addClassNode(ClassNode node) { ModuleNode module = new ModuleNode(this.ast); this.ast.addModule(module); module.addClass(node); }
private void addHolderClassIdiomBody(BlockStatement body, FieldNode fieldNode, Expression initExpr) { final ClassNode declaringClass = fieldNode.getDeclaringClass(); final ClassNode fieldType = fieldNode.getType(); final int visibility = ACC_PRIVATE | ACC_STATIC; final String fullName = declaringClass.getName() + "$" + fieldType.getNameWithoutPackage() + "Holder_" + fieldNode.getName().substring(1); final InnerClassNode holderClass = new InnerClassNode(declaringClass, fullName, visibility, ClassHelper.OBJECT_TYPE); final String innerFieldName = "INSTANCE"; holderClass.addField(innerFieldName, ACC_PRIVATE | ACC_STATIC | ACC_FINAL, fieldType, initExpr); final Expression innerField = new PropertyExpression(new ClassExpression(holderClass), innerFieldName); declaringClass.getModule().addClass(holderClass); body.addStatement(new ReturnStatement(innerField)); }
protected Expression anonymousInnerClassDef(AST node) { ClassNode oldNode = classNode; ClassNode outerClass = getClassOrScript(oldNode); String fullName = outerClass.getName() + '$' + innerClassCounter; innerClassCounter++; if (enumConstantBeingDef) { classNode = new EnumConstantClassNode(outerClass, fullName, Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE); } else { classNode = new InnerClassNode(outerClass, fullName, Opcodes.ACC_PUBLIC, ClassHelper.OBJECT_TYPE); } ((InnerClassNode) classNode).setAnonymous(true); classNode.setEnclosingMethod(methodNode); assertNodeType(OBJBLOCK, node); objectBlock(node); output.addClass(classNode); AnonymousInnerClassCarrier ret = new AnonymousInnerClassCarrier(); ret.innerClass = classNode; classNode = oldNode; return ret; }