compilationUnitServices.getEnclosingTypeName(),
compilationUnitServices.getImports(), field.getFieldType());
final Type initType = JavaParserUtils.getResolvedName(
compilationUnitServices.getEnclosingTypeName(),
field.getFieldType(), compilationUnitServices);
final ClassOrInterfaceType finalType = JavaParserUtils
.getClassOrInterfaceType(initType);
final FieldDeclaration newField = ASTHelper.createFieldDeclaration(
JavaParserUtils.getJavaParserModifier(field.getModifier()),
initType, field.getFieldName().getSymbolName());
// Add parameterized types for the field type (not initializer)
if (field.getFieldType().getParameters().size() > 0) {
final List<Type> fieldTypeArgs = new ArrayList<Type>();
finalType.setTypeArgs(fieldTypeArgs);
for (final JavaType parameter : field.getFieldType()
.getParameters()) {
fieldTypeArgs.add(JavaParserUtils.importParametersForType(
compilationUnitServices.getEnclosingTypeName(),
compilationUnitServices.getImports(), parameter));
}
}
final List<VariableDeclarator> vars = newField.getVariables();
Validate.notEmpty(vars,
"Expected ASTHelper to have provided a single VariableDeclarator");
Validate.isTrue(vars.size() == 1,
"Expected ASTHelper to have provided a single VariableDeclarator");
final VariableDeclarator vd = vars.iterator().next();
if (StringUtils.isNotBlank(field.getFieldInitializer())) {
// There is an initializer.
// We need to make a fake field that we can have JavaParser parse.
// Easiest way to do that is to build a simple source class
// containing the required field and re-parse it.
final StringBuilder sb = new StringBuilder();
sb.append("class TemporaryClass {\n");
sb.append(" private " + field.getFieldType() + " "
+ field.getFieldName() + " = "
+ field.getFieldInitializer() + ";\n");
sb.append("}\n");
final ByteArrayInputStream bais = new ByteArrayInputStream(sb
.toString().getBytes());
CompilationUnit ci;
try {
ci = JavaParser.parse(bais);
}
catch (final IOException e) {
throw new IllegalStateException(
"Illegal state: Unable to parse input stream", e);
}
catch (final ParseException pe) {
throw new IllegalStateException(
"Illegal state: JavaParser did not parse correctly", pe);
}
final List<TypeDeclaration> types = ci.getTypes();
if (types == null || types.size() != 1) {
throw new IllegalArgumentException("Field member invalid");
}
final TypeDeclaration td = types.get(0);
final List<BodyDeclaration> bodyDeclarations = td.getMembers();
if (bodyDeclarations == null || bodyDeclarations.size() != 1) {
throw new IllegalStateException(
"Illegal state: JavaParser did not return body declarations correctly");
}
final BodyDeclaration bd = bodyDeclarations.get(0);
if (!(bd instanceof FieldDeclaration)) {
throw new IllegalStateException(
"Illegal state: JavaParser did not return a field declaration correctly");
}
final FieldDeclaration fd = (FieldDeclaration) bd;
if (fd.getVariables() == null || fd.getVariables().size() != 1) {
throw new IllegalStateException(
"Illegal state: JavaParser did not return a field declaration correctly");
}
final Expression init = fd.getVariables().get(0).getInit();
// Resolve imports (ROO-1505)
if (init instanceof ObjectCreationExpr) {
final ObjectCreationExpr ocr = (ObjectCreationExpr) init;
final JavaType typeToImport = JavaParserUtils.getJavaTypeNow(
compilationUnitServices, ocr.getType(), null);
final NameExpr nameExpr = JavaParserUtils.importTypeIfRequired(
compilationUnitServices.getEnclosingTypeName(),
compilationUnitServices.getImports(), typeToImport);
final ClassOrInterfaceType classOrInterfaceType = JavaParserUtils
.getClassOrInterfaceType(nameExpr);
ocr.setType(classOrInterfaceType);
if (typeToImport.getParameters().size() > 0) {
final List<Type> initTypeArgs = new ArrayList<Type>();
finalType.setTypeArgs(initTypeArgs);
for (final JavaType parameter : typeToImport
.getParameters()) {
initTypeArgs.add(JavaParserUtils
.importParametersForType(
compilationUnitServices
.getEnclosingTypeName(),
compilationUnitServices.getImports(),
parameter));
}
classOrInterfaceType.setTypeArgs(initTypeArgs);
}
}
vd.setInit(init);
}