use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
the class TemplateASTTransformer method createConstructor.
private void createConstructor(final ClassNode classNode) {
Parameter[] params = new Parameter[] { new Parameter(MarkupTemplateEngine.MARKUPTEMPLATEENGINE_CLASSNODE, "engine"), new Parameter(ClassHelper.MAP_TYPE.getPlainNodeReference(), "model"), new Parameter(ClassHelper.MAP_TYPE.getPlainNodeReference(), "modelTypes"), new Parameter(TEMPLATECONFIG_CLASSNODE, "tplConfig") };
List<Expression> vars = new LinkedList<Expression>();
for (Parameter param : params) {
vars.add(new VariableExpression(param));
}
ExpressionStatement body = new ExpressionStatement(new ConstructorCallExpression(ClassNode.SUPER, new ArgumentListExpression(vars)));
ConstructorNode ctor = new ConstructorNode(Opcodes.ACC_PUBLIC, params, ClassNode.EMPTY_ARRAY, body);
classNode.addConstructor(ctor);
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
the class BaseScriptASTTransformation method changeBaseScriptType.
private void changeBaseScriptType(final AnnotatedNode parent, final ClassNode cNode, final ClassNode baseScriptType) {
if (!cNode.isScriptBody()) {
addError("Annotation " + MY_TYPE_NAME + " can only be used within a Script.", parent);
return;
}
if (!baseScriptType.isScript()) {
addError("Declared type " + baseScriptType + " does not extend groovy.lang.Script class!", parent);
return;
}
cNode.setSuperClass(baseScriptType);
// Method in base script that will contain the script body code.
MethodNode runScriptMethod = ClassHelper.findSAM(baseScriptType);
// If they want to use a name other than than "run", then make the change.
if (isCustomScriptBodyMethod(runScriptMethod)) {
MethodNode defaultMethod = cNode.getDeclaredMethod("run", Parameter.EMPTY_ARRAY);
// The reason is that our transform is getting called more than once sometimes.
if (defaultMethod != null) {
cNode.removeMethod(defaultMethod);
MethodNode methodNode = new MethodNode(runScriptMethod.getName(), runScriptMethod.getModifiers() & ~ACC_ABSTRACT, runScriptMethod.getReturnType(), runScriptMethod.getParameters(), runScriptMethod.getExceptions(), defaultMethod.getCode());
// The AST node metadata has the flag that indicates that this method is a script body.
// It may also be carrying data for other AST transforms.
methodNode.copyNodeMetaData(defaultMethod);
cNode.addMethod(methodNode);
}
}
// the constructors for our script class have already run.
if (cNode.getSuperClass().getDeclaredConstructor(CONTEXT_CTOR_PARAMETERS) == null) {
ConstructorNode orphanedConstructor = cNode.getDeclaredConstructor(CONTEXT_CTOR_PARAMETERS);
cNode.removeConstructor(orphanedConstructor);
}
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
the class TupleConstructorASTTransformation method addMapConstructors.
public static void addMapConstructors(ClassNode cNode, boolean hasNoArg, String message) {
Parameter[] parameters = params(new Parameter(LHMAP_TYPE, "__namedArgs"));
BlockStatement code = new BlockStatement();
VariableExpression namedArgs = varX("__namedArgs");
namedArgs.setAccessedVariable(parameters[0]);
code.addStatement(ifElseS(equalsNullX(namedArgs), illegalArgumentBlock(message), processArgsBlock(cNode, namedArgs)));
ConstructorNode init = new ConstructorNode(ACC_PUBLIC, parameters, ClassNode.EMPTY_ARRAY, code);
cNode.addConstructor(init);
// add a no-arg constructor too
if (!hasNoArg) {
code = new BlockStatement();
code.addStatement(stmt(ctorX(ClassNode.THIS, ctorX(LHMAP_TYPE))));
init = new ConstructorNode(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, code);
cNode.addConstructor(init);
}
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
the class InheritConstructorsASTTransformation method addConstructorUnlessAlreadyExisting.
private void addConstructorUnlessAlreadyExisting(ClassNode classNode, ConstructorNode consNode, boolean copyConstructorAnnotations, boolean copyParameterAnnotations) {
Parameter[] origParams = consNode.getParameters();
if (consNode.isPrivate())
return;
Parameter[] params = new Parameter[origParams.length];
Map<String, ClassNode> genericsSpec = createGenericsSpec(classNode);
extractSuperClassGenerics(classNode, classNode.getSuperClass(), genericsSpec);
List<Expression> theArgs = buildParams(origParams, params, genericsSpec, copyParameterAnnotations);
if (isExisting(classNode, params))
return;
ConstructorNode added = classNode.addConstructor(consNode.getModifiers(), params, consNode.getExceptions(), block(ctorSuperS(args(theArgs))));
if (copyConstructorAnnotations) {
added.addAnnotations(copyAnnotatedNodeAnnotations(consNode, MY_TYPE_NAME));
}
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
the class ImmutableASTTransformation method createConstructorMapCommon.
private void createConstructorMapCommon(ClassNode cNode, BlockStatement body) {
final List<FieldNode> fList = cNode.getFields();
for (FieldNode fNode : fList) {
// public fields will be rejected elsewhere
if (fNode.isPublic())
continue;
// a property
if (cNode.getProperty(fNode.getName()) != null)
continue;
if (fNode.isFinal() && fNode.isStatic())
continue;
// internal field
if (fNode.getName().contains("$") || fNode.isSynthetic())
continue;
if (fNode.isFinal() && fNode.getInitialExpression() != null)
body.addStatement(checkFinalArgNotOverridden(cNode, fNode));
body.addStatement(createConstructorStatementDefault(fNode));
}
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, params(new Parameter(HASHMAP_TYPE, "args")), ClassNode.EMPTY_ARRAY, body));
}
Aggregations