*/
protected void generateCanonicalModelClass(MetadataClass metadataClass, Element element, PersistenceUnit persistenceUnit) throws IOException {
Writer writer = null;
try {
ClassAccessor accessor = persistenceUnit.getClassAccessor(metadataClass);
String qualifiedName = accessor.getAccessibleObjectName();
String className = getName(qualifiedName);
String classPackage = getPackage(qualifiedName);
String qualifiedCanonicalName = persistenceUnit.getQualifiedCanonicalName(qualifiedName);
String canonicalName = getName(qualifiedCanonicalName);
String canonicalpackage = getPackage(qualifiedCanonicalName);
JavaFileObject file = processingEnv.getFiler().createSourceFile(qualifiedCanonicalName, element);
writer = file.openWriter();
// Print the package if we have one.
if (! canonicalpackage.equals("")) {
writer.append("package " + canonicalpackage + ";\n\n");
}
// Go through the accessor list, ignoring any transient accessors
// to build our attributes and import list.
ArrayList<String> attributes = new ArrayList<String>();
HashMap<String, String> imports = new HashMap<String, String>();
// Import the model class if the canonical class is generated elsewhere.
if (! classPackage.equals(canonicalpackage)) {
imports.put(className, qualifiedName);
}
for (MappingAccessor mappingAccessor : accessor.getDescriptor().getMappingAccessors()) {
if (! mappingAccessor.isTransient()) {
MetadataAnnotatedElement annotatedElement = mappingAccessor.getAnnotatedElement();
// Must go through the mapping accessor for the raw class
// since it may be a virtual mapping accessor with an
// attribute type.
MetadataClass rawClass = mappingAccessor.getRawClass();
// NOTE: order of checking is important.
String attributeType;
String types = className;
if (mappingAccessor.isBasic()) {
types = types + ", " + getUnqualifiedType(getBoxedType(annotatedElement, rawClass), imports);
attributeType = AttributeType.SingularAttribute.name();
imports.put(attributeType, "javax.persistence.metamodel.SingularAttribute");
} else {
if (rawClass.isList()) {
attributeType = AttributeType.ListAttribute.name();
imports.put(attributeType, "javax.persistence.metamodel.ListAttribute");
} else if (rawClass.isSet()) {
attributeType = AttributeType.SetAttribute.name();
imports.put(attributeType, "javax.persistence.metamodel.SetAttribute");
} else if (rawClass.isMap()) {
attributeType = AttributeType.MapAttribute.name();
imports.put(attributeType, "javax.persistence.metamodel.MapAttribute");
} else if (rawClass.isCollection()) {
attributeType = AttributeType.CollectionAttribute.name();
imports.put(attributeType, "javax.persistence.metamodel.CollectionAttribute");
} else {
attributeType = AttributeType.SingularAttribute.name();
imports.put(attributeType, "javax.persistence.metamodel.SingularAttribute");
}
if (mappingAccessor.isMapAccessor()) {
if (mappingAccessor.isMappedKeyMapAccessor()) {
MetadataClass mapKeyClass = ((MappedKeyMapAccessor) mappingAccessor).getMapKeyClass();
types = types + ", " + getUnqualifiedType(mapKeyClass.getName(), imports) + ", " + getUnqualifiedType(mappingAccessor.getReferenceClassName(), imports);
} else {
String mapKeyType;
if (annotatedElement.isGenericCollectionType()) {
// Grab the map key class from the generic.
mapKeyType = annotatedElement.getGenericType().get(1);
} else {
if (mappingAccessor.getReferenceDescriptor().hasIdAccessor()) {
// Grab the id type from the reference descriptor, now there's a handle!
MappingAccessor idAccessor = mappingAccessor.getReferenceDescriptor().getIdAccessors().values().iterator().next();
mapKeyType = idAccessor.getReferenceClassName();
} else {
// We don't know at this point so just use the catch all default.
mapKeyType = TypeVisitor.GENERIC_TYPE;
}
}
types = types + ", " + getUnqualifiedType(mapKeyType, imports) + ", " + getUnqualifiedType(mappingAccessor.getReferenceClassName(), imports);
}
} else {
types = types + ", " + getUnqualifiedType(mappingAccessor.getReferenceClassName(), imports);
}
}
// Add the mapping attribute to the list of attributes for this class.
attributes.add(" public static volatile " + attributeType + "<" + types + "> " + annotatedElement.getAttributeName() + ";\n");
}
}
// Will import the parent as well if needed.
String parent = writeImportStatements(imports, accessor, writer, persistenceUnit, canonicalpackage);
// Write out the generation annotations.
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
writer.append("@Generated(value=\"EclipseLink-" + Version.getVersion() + ".v" + Version.getBuildDate() + "-r" + Version.getBuildRevision() + "\", date=\"" + sdf.format(date) + "\")\n");
writer.append("@StaticMetamodel(" + className + ".class)\n");
int modifier = accessor.getAccessibleObject().getModifiers();
writer.append(java.lang.reflect.Modifier.toString(modifier) + " class " + canonicalName);
if (parent == null) {
writer.append(" { \n\n");
} else {