@Override public List<ConstructorNode> getDeclaredConstructors() { List<ConstructorNode> nodes = new LinkedList<ConstructorNode>(); for (ClassNode delegate : delegates) { nodes.addAll(delegate.getDeclaredConstructors()); } return nodes; }
private static List<ConstructorNode> sortConstructors(ConstructorCallExpression call, ClassNode callNode) { // sort in a new list to prevent side effects List<ConstructorNode> constructors = new ArrayList<ConstructorNode>(callNode.getDeclaredConstructors()); Comparator comp = new Comparator() { public int compare(Object arg0, Object arg1) { ConstructorNode c0 = (ConstructorNode) arg0; ConstructorNode c1 = (ConstructorNode) arg1; String descriptor0 = BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, c0.getParameters()); String descriptor1 = BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, c1.getParameters()); return descriptor0.compareTo(descriptor1); } }; Collections.sort(constructors, comp); return constructors; }
public static boolean hasNoArgConstructor(ClassNode cNode) { List<ConstructorNode> constructors = cNode.getDeclaredConstructors(); for (ConstructorNode next : constructors) { if (next.getParameters().length == 0) { return true; } } return false; }
@Override public List<ConstructorNode> getDeclaredConstructors() { lazyInitMembers(); return super.getDeclaredConstructors(); }
private void printConstructors(PrintWriter out, ClassNode classNode) { @SuppressWarnings("unchecked") List<ConstructorNode> constrs = (List<ConstructorNode>) constructors.clone(); if (constrs != null) { constrs.addAll(classNode.getDeclaredConstructors()); for (ConstructorNode constr : constrs) { printConstructor(out, classNode, constr); } } }
private void completeEnum(ClassNode enumClass) { boolean isAic = isAnonymousInnerClass(enumClass); if (enumClass.getDeclaredConstructors().isEmpty()) { addImplicitConstructors(enumClass, isAic); } for (ConstructorNode ctor : enumClass.getDeclaredConstructors()) { transformConstructor(ctor, isAic); } }
public List<ConstructorNode> getDeclaredConstructors() { if (redirect != null) return redirect().getDeclaredConstructors(); lazyClassInit(); if (constructors == null) constructors = new ArrayList<ConstructorNode> (); return constructors; }
protected void addDefaultParameterConstructors(final ClassNode node) { List methods = new ArrayList(node.getDeclaredConstructors()); addDefaultParameters(methods, new DefaultArgsAction() { public void call(ArgumentListExpression arguments, Parameter[] newParams, MethodNode method) { ConstructorNode ctor = (ConstructorNode) method; ConstructorCallExpression expression = new ConstructorCallExpression(ClassNode.THIS, arguments); Statement code = new ExpressionStatement(expression); addConstructor(newParams, ctor, code, node); } }); }
private void checkNoConstructor(final ClassNode cNode) { if (!cNode.getDeclaredConstructors().isEmpty()) { addError("Error processing trait '" + cNode.getName() + "'. " + " Constructors are not allowed.", cNode); } }
private static boolean isExisting(ClassNode classNode, Parameter[] params) { for (ConstructorNode consNode : classNode.getDeclaredConstructors()) { if (matchingTypes(params, consNode.getParameters())) { return true; } } return false; }
/** * Finds a constructor matching the given parameters in this class. * * @return the constructor matching the given parameters or null */ public ConstructorNode getDeclaredConstructor(Parameter[] parameters) { for (ConstructorNode method : getDeclaredConstructors()) { if (parametersEqual(method.getParameters(), parameters)) { return method; } } return null; }
private static MethodNode selectConstructor(ClassNode node, Parameter[] paraTypes) { List<ConstructorNode> cl = node.getDeclaredConstructors(); MethodNode res = null; for (ConstructorNode cn : cl) { if (ParameterUtils.parametersEqual(cn.getParameters(), paraTypes)) { res = cn; break; } } if (res !=null && res.isPublic()) return res; return null; }
protected void addInitialization(final ClassNode node) { boolean addSwapInit = moveOptimizedConstantsInitialization(node); for (ConstructorNode cn : node.getDeclaredConstructors()) { addInitialization(node, cn); } if (addSwapInit) { BytecodeSequence seq = new BytecodeSequence( new BytecodeInstruction() { @Override public void visit(MethodVisitor mv) { mv.visitMethodInsn(INVOKESTATIC, BytecodeHelper.getClassInternalName(node), SWAP_INIT, "()V", false); } }); List<Statement> swapCall = new ArrayList<Statement>(1); swapCall.add(seq); node.addStaticInitializerStatements(swapCall, true); } }
protected void addDefaultConstructor(ClassNode node) { if (!node.getDeclaredConstructors().isEmpty()) return; BlockStatement empty = new BlockStatement(); empty.setSourcePosition(node); ConstructorNode constructor = new ConstructorNode(ACC_PUBLIC, empty); constructor.setSourcePosition(node); constructor.setHasNoRealSourcePosition(true); node.addConstructor(constructor); }
private void adjustConstructorAndFields(int skipIndex, ClassNode type) { List<ConstructorNode> constructors = type.getDeclaredConstructors(); if (constructors.size() == 1) { ConstructorNode constructor = constructors.get(0); Parameter[] params = constructor.getParameters(); Parameter[] newParams = new Parameter[params.length - 1]; int to = 0; for (int from = 0; from < params.length; from++) { if (from != skipIndex) { newParams[to++] = params[from]; } } type.removeConstructor(constructor); // code doesn't mention the removed param at this point, okay to leave as is addGeneratedConstructor(type, constructor.getModifiers(), newParams, constructor.getExceptions(), constructor.getCode()); type.removeField(variableName); } }
/** * Determine if an explicit (non-generated) constructor is in the class. * * @param xform if non null, add an error if an explicit constructor is found * @param cNode the type of the containing class * @return true if an explicit (non-generated) constructor was found */ public static boolean hasExplicitConstructor(AbstractASTTransformation xform, ClassNode cNode) { List<ConstructorNode> declaredConstructors = cNode.getDeclaredConstructors(); for (ConstructorNode constructorNode : declaredConstructors) { // allow constructors added by other transforms if flagged as Generated if (hasAnnotation(constructorNode, GENERATED_TYPE)) { continue; } if (xform != null) { xform.addError("Error during " + xform.getAnnotationName() + " processing. Explicit constructors not allowed for class: " + cNode.getNameWithoutPackage(), constructorNode); } return true; } return false; }
/** * Add map and no-arg constructor or mirror those of the superclass (i.e. base enum). */ private static void addImplicitConstructors(ClassNode enumClass, boolean aic) { if (aic) { ClassNode sn = enumClass.getSuperClass(); List<ConstructorNode> sctors = new ArrayList<ConstructorNode>(sn.getDeclaredConstructors()); if (sctors.isEmpty()) { addMapConstructors(enumClass); } else { for (ConstructorNode constructorNode : sctors) { ConstructorNode init = new ConstructorNode(ACC_PUBLIC, constructorNode.getParameters(), ClassNode.EMPTY_ARRAY, new BlockStatement()); enumClass.addConstructor(init); } } } else { addMapConstructors(enumClass); } }
private void processClass(ClassNode cNode, AnnotationNode node) { if (cNode.isInterface()) { addError("Error processing interface '" + cNode.getName() + "'. " + MY_TYPE_NAME + " only allowed for classes.", cNode); return; } boolean copyConstructorAnnotations = memberHasValue(node, "constructorAnnotations", true); boolean copyParameterAnnotations = memberHasValue(node, "parameterAnnotations", true); ClassNode sNode = cNode.getSuperClass(); List<AnnotationNode> superAnnotations = sNode.getAnnotations(MY_TYPE); if (superAnnotations.size() == 1) { // We need @InheritConstructors from parent classes processed first // so force that order here. The transformation is benign on an already // processed node so processing twice in any order won't matter bar // a very small time penalty. processClass(sNode, node); } for (ConstructorNode cn : sNode.getDeclaredConstructors()) { addConstructorUnlessAlreadyExisting(cNode, cn, copyConstructorAnnotations, copyParameterAnnotations); } }
public void visitContents(GroovyClassVisitor visitor) { // now let's visit the contents of the class for (PropertyNode pn : getProperties()) { visitor.visitProperty(pn); } for (FieldNode fn : getFields()) { visitor.visitField(fn); } for (ConstructorNode cn : getDeclaredConstructors()) { visitor.visitConstructor(cn); } for (MethodNode mn : getMethods()) { visitor.visitMethod(mn); } }
private static void createSimpleClone(ClassNode cNode, List<FieldNode> fieldNodes, List<String> excludes) { if (cNode.getDeclaredConstructors().isEmpty()) { // add no-arg constructor addGeneratedConstructor(cNode, ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, block(EmptyStatement.INSTANCE)); } addSimpleCloneHelperMethod(cNode, fieldNodes, excludes); final Expression result = varX("_result", cNode); ClassNode[] exceptions = {make(CloneNotSupportedException.class)}; addGeneratedMethod(cNode, "clone", ACC_PUBLIC, GenericsUtils.nonGeneric(cNode), Parameter.EMPTY_ARRAY, exceptions, block( declS(result, ctorX(cNode)), stmt(callThisX("cloneOrCopyMembers", args(result))), returnS(result))); }