use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class InitializerStrategy method createBuilderForAnnotatedClass.
private void createBuilderForAnnotatedClass(BuilderASTTransformation transform, ClassNode buildee, AnnotationNode anno, boolean useSetters, boolean allNames) {
List<String> excludes = new ArrayList<String>();
List<String> includes = new ArrayList<String>();
includes.add(Undefined.STRING);
if (!getIncludeExclude(transform, anno, buildee, excludes, includes))
return;
if (includes.size() == 1 && Undefined.isUndefined(includes.get(0)))
includes = null;
List<FieldNode> fields = getFields(transform, anno, buildee);
List<FieldNode> filteredFields = filterFields(fields, includes, excludes, allNames);
if (filteredFields.isEmpty()) {
transform.addError("Error during " + BuilderASTTransformation.MY_TYPE_NAME + " processing: at least one property is required for this strategy", anno);
}
ClassNode builder = createInnerHelperClass(buildee, getBuilderClassName(buildee, anno), filteredFields.size());
addFields(buildee, filteredFields, builder);
buildCommon(buildee, anno, filteredFields, builder);
createBuildeeConstructors(transform, buildee, builder, filteredFields, true, useSetters);
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class InitializerStrategy method createFieldCopy.
private static FieldNode createFieldCopy(ClassNode buildee, FieldNode fNode) {
Map<String, ClassNode> genericsSpec = createGenericsSpec(fNode.getDeclaringClass());
extractSuperClassGenerics(fNode.getType(), buildee, genericsSpec);
ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, fNode.getType());
return new FieldNode(fNode.getName(), fNode.getModifiers(), correctedType, buildee, DEFAULT_INITIAL_VALUE);
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class InitializerStrategy method createBuilderMethodForField.
private MethodNode createBuilderMethodForField(ClassNode builder, List<FieldNode> fields, String prefix, int fieldPos) {
String fieldName = fields.get(fieldPos).getName();
String setterName = getSetterName(prefix, fieldName);
GenericsType[] gtypes = new GenericsType[fields.size()];
List<Expression> argList = new ArrayList<Expression>();
for (int i = 0; i < fields.size(); i++) {
gtypes[i] = i == fieldPos ? new GenericsType(ClassHelper.make(SET.class)) : makePlaceholder(i);
argList.add(i == fieldPos ? propX(varX("this"), constX(fieldName)) : varX(fields.get(i).getName()));
}
ClassNode returnType = makeClassSafeWithGenerics(builder, gtypes);
FieldNode fNode = fields.get(fieldPos);
Map<String, ClassNode> genericsSpec = createGenericsSpec(fNode.getDeclaringClass());
extractSuperClassGenerics(fNode.getType(), builder, genericsSpec);
ClassNode correctedType = correctToGenericsSpecRecurse(genericsSpec, fNode.getType());
return new MethodNode(setterName, ACC_PUBLIC, returnType, params(param(correctedType, fieldName)), NO_EXCEPTIONS, block(stmt(assignX(propX(varX("this"), constX(fieldName)), varX(fieldName, correctedType))), returnS(ctorX(returnType, args(argList)))));
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class AnnotationVisitor method validateEnumConstant.
private boolean validateEnumConstant(Expression exp) {
if (exp instanceof PropertyExpression) {
PropertyExpression pe = (PropertyExpression) exp;
String name = pe.getPropertyAsString();
if (pe.getObjectExpression() instanceof ClassExpression && name != null) {
ClassExpression ce = (ClassExpression) pe.getObjectExpression();
ClassNode type = ce.getType();
if (type.isEnum()) {
boolean ok = false;
try {
FieldNode enumField = type.getDeclaredField(name);
ok = enumField != null && enumField.getType().equals(type);
} catch (Exception ex) {
// ignore
}
if (!ok) {
addError("No enum const " + type.getName() + "." + name, pe);
return false;
}
}
}
}
return true;
}
use of org.codehaus.groovy.ast.FieldNode in project groovy by apache.
the class GeneralUtils method getInstanceNonPropertyFieldNames.
public static List<String> getInstanceNonPropertyFieldNames(ClassNode cNode) {
List<FieldNode> fList = getInstanceNonPropertyFields(cNode);
List<String> result = new ArrayList<String>(fList.size());
for (FieldNode fNode : fList) {
result.add(fNode.getName());
}
return result;
}
Aggregations