use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
the class AutoCloneASTTransformation method createCloneCopyConstructor.
private void createCloneCopyConstructor(ClassNode cNode, List<FieldNode> list, List<String> excludes) {
if (cNode.getDeclaredConstructors().size() == 0) {
// add no-arg constructor
BlockStatement noArgBody = new BlockStatement();
noArgBody.addStatement(EmptyStatement.INSTANCE);
cNode.addConstructor(ACC_PUBLIC, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, noArgBody);
}
boolean hasThisCons = false;
for (ConstructorNode consNode : cNode.getDeclaredConstructors()) {
Parameter[] parameters = consNode.getParameters();
if (parameters.length == 1 && parameters[0].getType().equals(cNode)) {
hasThisCons = true;
}
}
if (!hasThisCons) {
BlockStatement initBody = new BlockStatement();
Parameter initParam = param(GenericsUtils.nonGeneric(cNode), "other");
final Expression other = varX(initParam);
boolean hasParent = cNode.getSuperClass() != ClassHelper.OBJECT_TYPE;
if (hasParent) {
initBody.addStatement(stmt(ctorX(ClassNode.SUPER, other)));
}
for (FieldNode fieldNode : list) {
String name = fieldNode.getName();
if (excludes.contains(name))
continue;
ClassNode fieldType = fieldNode.getType();
Expression direct = propX(other, name);
Expression to = propX(varX("this"), name);
Statement assignDirect = assignS(to, direct);
Statement assignCloned = assignS(to, castX(fieldType, callCloneDirectX(direct)));
Statement assignClonedDynamic = assignS(to, castX(fieldType, callCloneDynamicX(direct)));
if (isCloneableType(fieldType)) {
initBody.addStatement(assignCloned);
} else if (!possiblyCloneable(fieldType)) {
initBody.addStatement(assignDirect);
} else {
initBody.addStatement(ifElseS(isInstanceOfX(direct, CLONEABLE_TYPE), assignClonedDynamic, assignDirect));
}
}
cNode.addConstructor(ACC_PROTECTED, params(initParam), ClassNode.EMPTY_ARRAY, initBody);
}
ClassNode[] exceptions = { make(CloneNotSupportedException.class) };
cNode.addMethod("clone", ACC_PUBLIC, GenericsUtils.nonGeneric(cNode), Parameter.EMPTY_ARRAY, exceptions, block(stmt(ctorX(cNode, args(varX("this"))))));
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
the class ImmutableASTTransformation method createConstructorOrdered.
private void createConstructorOrdered(ClassNode cNode, List<PropertyNode> list) {
final MapExpression argMap = new MapExpression();
final Parameter[] orderedParams = new Parameter[list.size()];
int index = 0;
for (PropertyNode pNode : list) {
Parameter param = new Parameter(pNode.getField().getType(), pNode.getField().getName());
orderedParams[index++] = param;
argMap.addMapEntryExpression(constX(pNode.getName()), varX(pNode.getName()));
}
final BlockStatement orderedBody = new BlockStatement();
orderedBody.addStatement(stmt(ctorX(ClassNode.THIS, args(castX(HASHMAP_TYPE, argMap)))));
doAddConstructor(cNode, new ConstructorNode(ACC_PUBLIC, orderedParams, ClassNode.EMPTY_ARRAY, orderedBody));
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
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-core by groovy.
the class PackageScopeASTTransformation method visitClassNode.
private void visitClassNode(ClassNode cNode, List<PackageScopeTarget> value) {
String cName = cNode.getName();
if (cNode.isInterface() && value.size() != 1 && value.get(0) != PackageScopeTarget.CLASS) {
addError("Error processing interface '" + cName + "'. " + MY_TYPE_NAME + " not allowed for interfaces except when targeting Class level.", cNode);
}
if (value.contains(groovy.transform.PackageScopeTarget.CLASS)) {
if (cNode.isSyntheticPublic())
revertVisibility(cNode);
else
addError("Can't use " + MY_TYPE_NAME + " for class '" + cNode.getName() + "' which has explicit visibility.", cNode);
}
if (value.contains(groovy.transform.PackageScopeTarget.METHODS)) {
final List<MethodNode> mList = cNode.getMethods();
for (MethodNode mNode : mList) {
if (mNode.isSyntheticPublic())
revertVisibility(mNode);
}
}
if (value.contains(groovy.transform.PackageScopeTarget.CONSTRUCTORS)) {
final List<ConstructorNode> cList = cNode.getDeclaredConstructors();
for (MethodNode mNode : cList) {
if (mNode.isSyntheticPublic())
revertVisibility(mNode);
}
}
if (value.contains(PackageScopeTarget.FIELDS)) {
final List<PropertyNode> pList = cNode.getProperties();
List<PropertyNode> foundProps = new ArrayList<PropertyNode>();
List<String> foundNames = new ArrayList<String>();
for (PropertyNode pNode : pList) {
foundProps.add(pNode);
foundNames.add(pNode.getName());
}
for (PropertyNode pNode : foundProps) {
pList.remove(pNode);
}
final List<FieldNode> fList = cNode.getFields();
for (FieldNode fNode : fList) {
if (foundNames.contains(fNode.getName())) {
revertVisibility(fNode);
}
}
}
}
use of org.codehaus.groovy.ast.ConstructorNode in project groovy-core by groovy.
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.size() != 0) {
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)), new ThrowStatement(ctorX(make(RuntimeException.class), args(constX("Can't instantiate singleton " + classNode.getName() + ". Use " + classNode.getName() + "." + propertyName))))));
classNode.addConstructor(new ConstructorNode(ACC_PRIVATE, body));
}
}
Aggregations