use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class AntlrParserPlugin method constructorDef.
protected void constructorDef(AST constructorDef) {
List<AnnotationNode> annotations = new ArrayList<AnnotationNode>();
AST node = constructorDef.getFirstChild();
int modifiers = Opcodes.ACC_PUBLIC;
if (isType(MODIFIERS, node)) {
modifiers = modifiers(node, annotations, modifiers);
checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_STATIC, "static");
checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_FINAL, "final");
checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_ABSTRACT, "abstract");
checkNoInvalidModifier(constructorDef, "Constructor", modifiers, Opcodes.ACC_NATIVE, "native");
node = node.getNextSibling();
}
assertNodeType(PARAMETERS, node);
Parameter[] parameters = parameters(node);
if (parameters == null)
parameters = Parameter.EMPTY_ARRAY;
node = node.getNextSibling();
ClassNode[] exceptions = ClassNode.EMPTY_ARRAY;
if (isType(LITERAL_throws, node)) {
AST throwsNode = node.getFirstChild();
List<ClassNode> exceptionList = new ArrayList<ClassNode>();
throwsList(throwsNode, exceptionList);
exceptions = exceptionList.toArray(exceptions);
node = node.getNextSibling();
}
assertNodeType(SLIST, node);
boolean syntheticPublic = ((modifiers & Opcodes.ACC_SYNTHETIC) != 0);
modifiers &= ~Opcodes.ACC_SYNTHETIC;
ConstructorNode constructorNode = classNode.addConstructor(modifiers, parameters, exceptions, null);
MethodNode oldMethod = methodNode;
methodNode = constructorNode;
Statement code = statementList(node);
methodNode = oldMethod;
constructorNode.setCode(code);
constructorNode.setSyntheticPublic(syntheticPublic);
constructorNode.addAnnotations(annotations);
configureAST(constructorNode, constructorDef);
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class StaticTypeCheckingVisitor method typeCheckMapConstructor.
protected MethodNode typeCheckMapConstructor(final ConstructorCallExpression call, final ClassNode receiver, final Expression arguments) {
MethodNode node = null;
if (arguments instanceof TupleExpression) {
TupleExpression texp = (TupleExpression) arguments;
List<Expression> expressions = texp.getExpressions();
if (expressions.size() == 1) {
Expression expression = expressions.get(0);
if (expression instanceof MapExpression) {
MapExpression argList = (MapExpression) expression;
checkGroovyConstructorMap(call, receiver, argList);
node = new ConstructorNode(Opcodes.ACC_PUBLIC, new Parameter[] { new Parameter(MAP_TYPE, "map") }, ClassNode.EMPTY_ARRAY, GENERATED_EMPTY_STATEMENT);
node.setDeclaringClass(receiver);
}
}
}
return node;
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class StaticTypeCheckingVisitor method checkGroovyStyleConstructor.
/**
* Checks that a constructor style expression is valid regarding the number of arguments and the argument types.
*
* @param node the class node for which we will try to find a matching constructor
* @param arguments the constructor arguments
*/
protected MethodNode checkGroovyStyleConstructor(final ClassNode node, final ClassNode[] arguments, final ASTNode source) {
if (node.equals(ClassHelper.OBJECT_TYPE) || node.equals(ClassHelper.DYNAMIC_TYPE)) {
// in that case, we are facing a list constructor assigned to a def or object
return null;
}
List<ConstructorNode> constructors = node.getDeclaredConstructors();
if (constructors.isEmpty() && arguments.length == 0) {
return null;
}
List<MethodNode> constructorList = findMethod(node, "<init>", arguments);
if (constructorList.isEmpty()) {
if (isBeingCompiled(node) && arguments.length == 1 && LINKEDHASHMAP_CLASSNODE.equals(arguments[0])) {
// there will be a default hash map constructor added later
ConstructorNode cn = new ConstructorNode(Opcodes.ACC_PUBLIC, new Parameter[] { new Parameter(LINKEDHASHMAP_CLASSNODE, "args") }, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE);
return cn;
} else {
addStaticTypeError("No matching constructor found: " + node + toMethodParametersString("<init>", arguments), source);
return null;
}
} else if (constructorList.size() > 1) {
addStaticTypeError("Ambiguous constructor call " + node + toMethodParametersString("<init>", arguments), source);
return null;
}
return constructorList.get(0);
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class StaticInvocationWriter method writeSpecialConstructorCall.
@Override
public void writeSpecialConstructorCall(final ConstructorCallExpression call) {
MethodNode mn = call.getNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET);
if (mn == null) {
super.writeSpecialConstructorCall(call);
return;
}
controller.getCompileStack().pushInSpecialConstructorCall();
ConstructorNode cn;
if (mn instanceof ConstructorNode) {
cn = (ConstructorNode) mn;
} else {
cn = new ConstructorNode(mn.getModifiers(), mn.getParameters(), mn.getExceptions(), mn.getCode());
cn.setDeclaringClass(mn.getDeclaringClass());
}
// load "this"
controller.getMethodVisitor().visitVarInsn(ALOAD, 0);
String ownerDescriptor = BytecodeHelper.getClassInternalName(cn.getDeclaringClass());
TupleExpression args = makeArgumentList(call.getArguments());
int before = controller.getOperandStack().getStackLength();
loadArguments(args.getExpressions(), cn.getParameters());
finnishConstructorCall(cn, ownerDescriptor, controller.getOperandStack().getStackLength() - before);
// on a special call, there's no object on stack
controller.getOperandStack().remove(1);
controller.getCompileStack().pop();
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class MapConstructorASTTransformation method createConstructor.
public static void createConstructor(ClassNode cNode, boolean includeFields, boolean includeProperties, boolean includeSuperProperties, boolean useSetters, List<String> excludes, List<String> includes, ClosureExpression pre, ClosureExpression post, SourceUnit source, boolean allNames) {
List<ConstructorNode> constructors = cNode.getDeclaredConstructors();
boolean foundEmpty = constructors.size() == 1 && constructors.get(0).getFirstStatement() == null;
// HACK: JavaStubGenerator could have snuck in a constructor we don't want
if (foundEmpty)
constructors.remove(0);
List<FieldNode> superList = new ArrayList<FieldNode>();
if (includeSuperProperties) {
superList.addAll(getSuperPropertyFields(cNode.getSuperClass()));
}
List<FieldNode> list = new ArrayList<FieldNode>();
if (includeProperties) {
list.addAll(getInstancePropertyFields(cNode));
}
if (includeFields) {
list.addAll(getInstanceNonPropertyFields(cNode));
}
Parameter map = param(MAP_TYPE, "args");
final BlockStatement body = new BlockStatement();
ClassCodeExpressionTransformer transformer = makeMapTypedArgsTransformer();
if (pre != null) {
ClosureExpression transformed = (ClosureExpression) transformer.transform(pre);
copyStatementsWithSuperAdjustment(transformed, body);
}
final BlockStatement inner = new BlockStatement();
for (FieldNode fNode : superList) {
String name = fNode.getName();
if (shouldSkip(name, excludes, includes, allNames))
continue;
assignField(useSetters, map, inner, name);
}
for (FieldNode fNode : list) {
String name = fNode.getName();
if (shouldSkip(name, excludes, includes, allNames))
continue;
assignField(useSetters, map, inner, name);
}
body.addStatement(ifS(notNullX(varX("args")), inner));
if (post != null) {
ClosureExpression transformed = (ClosureExpression) transformer.transform(post);
body.addStatement(transformed.getCode());
}
cNode.addConstructor(new ConstructorNode(ACC_PUBLIC, params(map), ClassNode.EMPTY_ARRAY, body));
}
Aggregations