use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class SingletonASTTransformation method createConstructor.
private void createConstructor(ClassNode classNode, FieldNode field, String propertyName, boolean isStrict) {
final List<ConstructorNode> cNodes = classNode.getDeclaredConstructors();
ConstructorNode foundNoArg = null;
for (ConstructorNode cNode : cNodes) {
final Parameter[] parameters = cNode.getParameters();
if (parameters == null || parameters.length == 0) {
foundNoArg = cNode;
break;
}
}
if (isStrict && !cNodes.isEmpty()) {
for (ConstructorNode cNode : cNodes) {
addError("@Singleton didn't expect to find one or more additional constructors: remove constructor(s) or set strict=false", cNode);
}
}
if (foundNoArg == null) {
final BlockStatement body = new BlockStatement();
body.addStatement(ifS(notNullX(varX(field)), throwS(ctorX(make(RuntimeException.class), args(constX("Can't instantiate singleton " + classNode.getName() + ". Use " + classNode.getName() + "." + propertyName))))));
classNode.addConstructor(new ConstructorNode(ACC_PRIVATE, body));
}
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
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 by apache.
the class ImmutableASTTransformation method validateConstructors.
private boolean validateConstructors(ClassNode cNode) {
List<ConstructorNode> declaredConstructors = cNode.getDeclaredConstructors();
for (ConstructorNode constructorNode : declaredConstructors) {
// allow constructors added by other transforms if flagged as safe
Object nodeMetaData = constructorNode.getNodeMetaData(IMMUTABLE_SAFE_FLAG);
if (nodeMetaData != null && ((Boolean) nodeMetaData)) {
continue;
}
// TODO: allow constructors which only call provided constructor?
addError("Explicit constructors not allowed for " + MY_TYPE_NAME + " class: " + cNode.getNameWithoutPackage(), constructorNode);
return false;
}
return true;
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy by apache.
the class ImmutableASTTransformation method createNoArgConstructor.
private static void createNoArgConstructor(ClassNode cNode) {
Statement body = stmt(ctorX(ClassNode.THIS, args(new MapExpression())));
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, body));
}
Aggregations