/** * 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; }
public void visitConstructor(ConstructorNode node) { visitParameters(node, node.getParameters()); String methodType = BytecodeHelper.getMethodDescriptor(ClassHelper.VOID_TYPE, node.getParameters()); mv = cv.visitMethod(node.getModifiers(), "<init>", methodType, null, null); mv.visitTypeInsn(NEW, "java/lang/RuntimeException"); mv.visitInsn(DUP); mv.visitLdcInsn("not intended for execution"); mv.visitMethodInsn( INVOKESPECIAL, "java/lang/RuntimeException", "<init>", "(Ljava/lang/String;)V", false); mv.visitInsn(ATHROW); mv.visitMaxs(0, 0); }
private void doAddConstructor(final ClassNode cNode, final ConstructorNode constructorNode) { cNode.addConstructor(constructorNode); // GROOVY-5814: Immutable is not compatible with @CompileStatic Parameter argsParam = null; for (Parameter p : constructorNode.getParameters()) { if ("args".equals(p.getName())) { argsParam = p; break; } } if (argsParam != null) { final Parameter arg = argsParam; ClassCodeVisitorSupport variableExpressionFix = new ClassCodeVisitorSupport() { @Override protected SourceUnit getSourceUnit() { return cNode.getModule().getContext(); } @Override public void visitVariableExpression(final VariableExpression expression) { super.visitVariableExpression(expression); if ("args".equals(expression.getName())) { expression.setAccessedVariable(arg); } } }; variableExpressionFix.visitConstructor(constructorNode); } }
private Parameter[] selectAccessibleConstructorFromSuper(ConstructorNode node) { ClassNode type = node.getDeclaringClass(); ClassNode superType = type.getSuperClass(); for (ConstructorNode c : superType.getDeclaredConstructors()) { // Only look at things we can actually call if (c.isPublic() || c.isProtected()) { return c.getParameters(); } } // fall back for parameterless constructor if (superType.isPrimaryClassNode()) { return Parameter.EMPTY_ARRAY; } return null; }
private ClassNode getConstructorArgumentType(Expression arg, ConstructorNode node) { if (!(arg instanceof VariableExpression)) return arg.getType(); VariableExpression vexp = (VariableExpression) arg; String name = vexp.getName(); for (Parameter param : node.getParameters()) { if (param.getName().equals(name)) { return param.getType(); } } return vexp.getType(); }
/** Add map and no-arg constructor or mirror those of the superclass (i.e. base enum). */ private void addImplicitConstructors(ClassNode enumClass, boolean aic) { if (aic) { ClassNode sn = enumClass.getSuperClass(); List<ConstructorNode> sctors = new ArrayList<ConstructorNode>(sn.getDeclaredConstructors()); if (sctors.size() == 0) { addMapConstructors(enumClass, false); } else { for (ConstructorNode constructorNode : sctors) { ConstructorNode init = new ConstructorNode( Opcodes.ACC_PUBLIC, constructorNode.getParameters(), ClassNode.EMPTY_ARRAY, new BlockStatement()); enumClass.addConstructor(init); } } } else { addMapConstructors(enumClass, false); } }
private ConstructorCallExpression getConstructorCallExpression(ConstructorNode constructorNode) { Statement code = constructorNode.getCode(); if (!(code instanceof BlockStatement)) return null; BlockStatement block = (BlockStatement) code; List stats = block.getStatements(); if (stats == null || stats.size() == 0) return null; Statement stat = (Statement) stats.get(0); if (!(stat instanceof ExpressionStatement)) return null; Expression expr = ((ExpressionStatement) stat).getExpression(); if (!(expr instanceof ConstructorCallExpression)) return null; return (ConstructorCallExpression) expr; }
/** * If constructor does not define a call to super, then transform constructor to get String,int * parameters at beginning and add call super(String,int). */ private void transformConstructor(ConstructorNode ctor, boolean isAic) { boolean chainedThisConstructorCall = false; ConstructorCallExpression cce = null; if (ctor.firstStatementIsSpecialConstructorCall()) { Statement code = ctor.getFirstStatement(); cce = (ConstructorCallExpression) ((ExpressionStatement) code).getExpression(); if (cce.isSuperCall()) return; // must be call to this(...) chainedThisConstructorCall = true; } // we need to add parameters Parameter[] oldP = ctor.getParameters(); Parameter[] newP = new Parameter[oldP.length + 2]; String stringParameterName = getUniqueVariableName("__str", ctor.getCode()); newP[0] = new Parameter(ClassHelper.STRING_TYPE, stringParameterName); String intParameterName = getUniqueVariableName("__int", ctor.getCode()); newP[1] = new Parameter(ClassHelper.int_TYPE, intParameterName); System.arraycopy(oldP, 0, newP, 2, oldP.length); ctor.setParameters(newP); VariableExpression stringVariable = new VariableExpression(newP[0]); VariableExpression intVariable = new VariableExpression(newP[1]); if (chainedThisConstructorCall) { TupleExpression args = (TupleExpression) cce.getArguments(); List<Expression> argsExprs = args.getExpressions(); argsExprs.add(0, stringVariable); argsExprs.add(1, intVariable); } else { // add a super call List<Expression> args = new ArrayList<Expression>(); args.add(stringVariable); args.add(intVariable); if (isAic) { for (Parameter parameter : oldP) { args.add(new VariableExpression(parameter.getName())); } } cce = new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(args)); BlockStatement code = new BlockStatement(); code.addStatement(new ExpressionStatement(cce)); Statement oldCode = ctor.getCode(); if (oldCode != null) code.addStatement(oldCode); ctor.setCode(code); } }
public void visitConstructor(ConstructorNode node) { inConstructor = true; inStaticConstructor = node.isStaticConstructor(); checkGenericsUsage(node, node.getParameters()); super.visitConstructor(node); }
public Object newConstructor(int line, int column, String className, List args) { ConstructorNode node = new ConstructorNode(className); node.setConstructorItems(args); setPosition(node, line, column); return node; }
public void addConstructor(ConstructorNode node) { node.setDeclaringClass(this); final ClassNode r = redirect(); if (r.constructors == null) r.constructors = new ArrayList<ConstructorNode>(); r.constructors.add(node); }