cu.setImports(baseImports);
return cu;
}
public static CompilationUnit generateAbstractInformer(InformerInfos informerInfos, Collection<String> qualifiedEnums, Map<String, Class> resolvedInformers) {
CompilationUnit cu = new CompilationUnit();
// set the package
cu.setPackage(new PackageDeclaration(ASTHelper.createNameExpr(informerInfos.classPackage)));
// Finally add imports (they may have been "improved" by informers generation)
List<ImportDeclaration> baseImports = informerInfos.imports;
// Extracting effective imports
Collection<String> imports = new LinkedList<String>();
// Add current package to import for resolution (but do not forget to remove it before writing the effective imports)
imports.add(informerInfos.classPackage);
imports.add("java.lang");
for (ImportDeclaration d : baseImports) {
imports.add(d.getName().toString());
}
// create the type declaration
ClassOrInterfaceDeclaration type = new ClassOrInterfaceDeclaration(ModifierSet.PUBLIC, true, informerInfos.getAbstractInformerName());
if(informerInfos.superClassName!=null && !"Object".equals(informerInfos.superClassName)) {
List<ClassOrInterfaceType> extended = new LinkedList<ClassOrInterfaceType>();
extended.add(new ClassOrInterfaceType(InformerInfos.buildAbstractInformerName(informerInfos.superClassName)));
type.setExtends(extended);
}
type.setJavaDoc(new JavadocComment("\n" +
"Informer method container for {@link " + informerInfos.className + "}\n" +
"This interface is to be used only by gaedo code. Its only role is to provide consistent method hierarchy.\n" +
"As a consequence, refering to it directly has not the slightest interest and should never been done in user code." +
"@author InformerMojos\n"));
List<AnnotationExpr> annotations = new LinkedList<AnnotationExpr>();
// Constructing generated annotation value
List<MemberValuePair> parameters = new LinkedList<MemberValuePair>();
parameters.add(new MemberValuePair("date", new StringLiteralExpr(
javax.xml.bind.DatatypeConverter.printDateTime(GregorianCalendar.getInstance()))));
parameters.add(new MemberValuePair("comments", new StringLiteralExpr("generated by gaedo-informer-generator")));
List<Expression> values = new LinkedList<Expression>();
values.add(new StringLiteralExpr(informerInfos.getQualifiedClassName()));
parameters.add(new MemberValuePair("value",
new ArrayInitializerExpr(values)));
NormalAnnotationExpr generated = new NormalAnnotationExpr(ASTHelper.createNameExpr(Generated.class.getName()), parameters);
annotations.add(generated);
type.setAnnotations(annotations);
ASTHelper.addTypeDeclaration(cu, type);
for (PropertyInfos infos : informerInfos.properties) {
// create a method
MethodDeclaration method = new MethodDeclaration(ModifierSet.PUBLIC, ASTHelper.VOID_TYPE, "get" + Utils.uppercaseFirst(infos.name));
String informerTypeFor = InformerTypeFinder.getInformerTypeFor(resolvedInformers, qualifiedEnums, imports, infos);
method.setType(new ClassOrInterfaceType(informerTypeFor));
method.setJavaDoc(new JavadocComment(infos.generateJavadoc(informerInfos, informerTypeFor)));
ASTHelper.addMember(type, method);
}
imports.remove(informerInfos.classPackage);
imports.remove("java.lang");
baseImports = new LinkedList<ImportDeclaration>();
for(String name : imports) {
baseImports.add(new ImportDeclaration(ASTHelper.createNameExpr(name), false, false));
}
baseImports.add(new ImportDeclaration(ASTHelper.createNameExpr(Informer.class.getCanonicalName()), false, false));
baseImports.add(new ImportDeclaration(ASTHelper.createNameExpr(ObjectFieldInformer.class.getPackage().getName()), false, true));
cu.setImports(baseImports);
return cu;
}