METAGEN-67 Formatting

This commit is contained in:
Hardy Ferentschik 2012-01-17 11:55:18 +01:00 committed by Strong Liu
parent eb4521b1e9
commit 170d2c9bff
4 changed files with 435 additions and 451 deletions

View File

@ -14,8 +14,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.hibernate.jpamodelgen.util; package org.hibernate.jpamodelgen.util;
import java.util.ArrayList; import java.util.ArrayList;

View File

@ -14,8 +14,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.hibernate.jpamodelgen.util; package org.hibernate.jpamodelgen.util;
import java.io.Serializable; import java.io.Serializable;

View File

@ -14,8 +14,6 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.hibernate.jpamodelgen.util; package org.hibernate.jpamodelgen.util;
/** /**

View File

@ -1,445 +1,435 @@
/* /*
* JBoss, Home of Professional Open Source * JBoss, Home of Professional Open Source
* Copyright 2010, Red Hat Middleware LLC, and individual contributors * Copyright 2010, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a * by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors. * full listing of individual contributors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.hibernate.jpamodelgen.util;
package org.hibernate.jpamodelgen.util; import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.lang.annotation.Annotation; import java.util.HashMap;
import java.util.ArrayList; import java.util.List;
import java.util.HashMap; import java.util.Map;
import java.util.List; import javax.lang.model.element.AnnotationMirror;
import java.util.Map; import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.Element;
import javax.lang.model.element.AnnotationValue; import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Element; import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.ElementKind; import javax.lang.model.element.TypeElement;
import javax.lang.model.element.ExecutableElement; import javax.lang.model.type.DeclaredType;
import javax.lang.model.element.TypeElement; import javax.lang.model.type.ExecutableType;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeKind;
import javax.lang.model.type.ExecutableType; import javax.lang.model.type.TypeMirror;
import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeVariable;
import javax.lang.model.type.TypeMirror; import javax.lang.model.util.ElementFilter;
import javax.lang.model.type.TypeVariable; import javax.lang.model.util.Elements;
import javax.lang.model.util.ElementFilter; import javax.lang.model.util.SimpleTypeVisitor6;
import javax.lang.model.util.Elements; import javax.persistence.Access;
import javax.lang.model.util.SimpleTypeVisitor6; import javax.persistence.AccessType;
import javax.persistence.Access; import javax.persistence.Embeddable;
import javax.persistence.AccessType; import javax.persistence.EmbeddedId;
import javax.persistence.Embeddable; import javax.persistence.Entity;
import javax.persistence.EmbeddedId; import javax.persistence.Id;
import javax.persistence.Entity; import javax.persistence.MappedSuperclass;
import javax.persistence.Id; import javax.tools.Diagnostic;
import javax.persistence.MappedSuperclass;
import javax.tools.Diagnostic; import org.hibernate.jpamodelgen.AccessTypeInformation;
import org.hibernate.jpamodelgen.Context;
import org.hibernate.jpamodelgen.AccessTypeInformation; import org.hibernate.jpamodelgen.MetaModelGenerationException;
import org.hibernate.jpamodelgen.Context;
import org.hibernate.jpamodelgen.MetaModelGenerationException; /**
* Utility class.
/** *
* Utility class. * @author Max Andersen
* * @author Hardy Ferentschik
* @author Max Andersen * @author Emmanuel Bernard
* @author Hardy Ferentschik */
* @author Emmanuel Bernard public final class TypeUtils {
*/
public final class TypeUtils { public static final String DEFAULT_ANNOTATION_PARAMETER_NAME = "value";
private static final Map<String, String> PRIMITIVES = new HashMap<String, String>();
public static final String DEFAULT_ANNOTATION_PARAMETER_NAME = "value";
private static final Map<String, String> PRIMITIVES = new HashMap<String, String>(); static {
PRIMITIVES.put( "char", "Character" );
static {
PRIMITIVES.put( "char", "Character" ); PRIMITIVES.put( "byte", "Byte" );
PRIMITIVES.put( "short", "Short" );
PRIMITIVES.put( "byte", "Byte" ); PRIMITIVES.put( "int", "Integer" );
PRIMITIVES.put( "short", "Short" ); PRIMITIVES.put( "long", "Long" );
PRIMITIVES.put( "int", "Integer" );
PRIMITIVES.put( "long", "Long" ); PRIMITIVES.put( "boolean", "Boolean" );
PRIMITIVES.put( "boolean", "Boolean" ); PRIMITIVES.put( "float", "Float" );
PRIMITIVES.put( "double", "Double" );
PRIMITIVES.put( "float", "Float" ); }
PRIMITIVES.put( "double", "Double" );
private TypeUtils() {
} }
private TypeUtils() { public static String toTypeString(TypeMirror type) {
} if ( type.getKind().isPrimitive() ) {
return PRIMITIVES.get( type.toString() );
public static String toTypeString(TypeMirror type) { }
if ( type.getKind().isPrimitive() ) { return type.toString();
return PRIMITIVES.get( type.toString() ); }
}
return type.toString(); public static TypeElement getSuperclassTypeElement(TypeElement element) {
} final TypeMirror superClass = element.getSuperclass();
//superclass of Object is of NoType which returns some other kind
public static TypeElement getSuperclassTypeElement(TypeElement element) { if ( superClass.getKind() == TypeKind.DECLARED ) {
final TypeMirror superClass = element.getSuperclass(); //F..king Ch...t Have those people used their horrible APIs even once?
//superclass of Object is of NoType which returns some other kind final Element superClassElement = ( (DeclaredType) superClass ).asElement();
if ( superClass.getKind() == TypeKind.DECLARED ) { return (TypeElement) superClassElement;
//F..king Ch...t Have those people used their horrible APIs even once? }
final Element superClassElement = ( (DeclaredType) superClass ).asElement(); else {
return (TypeElement) superClassElement; return null;
} }
else { }
return null;
} public static String extractClosestRealTypeAsString(TypeMirror type, Context context) {
} if ( type instanceof TypeVariable ) {
final TypeMirror compositeUpperBound = ( (TypeVariable) type ).getUpperBound();
public static String extractClosestRealTypeAsString(TypeMirror type, Context context) { return extractClosestRealTypeAsString( compositeUpperBound, context );
if ( type instanceof TypeVariable ) { }
final TypeMirror compositeUpperBound = ( (TypeVariable) type ).getUpperBound(); else {
return extractClosestRealTypeAsString( compositeUpperBound, context ); return context.getTypeUtils().erasure( type ).toString();
} }
else { }
return context.getTypeUtils().erasure( type ).toString();
} public static boolean containsAnnotation(Element element, Class<?>... annotations) {
} assert element != null;
assert annotations != null;
public static boolean containsAnnotation(Element element, Class<?>... annotations) {
assert element != null; List<String> annotationClassNames = new ArrayList<String>();
assert annotations != null; for ( Class<?> clazz : annotations ) {
annotationClassNames.add( clazz.getName() );
List<String> annotationClassNames = new ArrayList<String>(); }
for ( Class<?> clazz : annotations ) {
annotationClassNames.add( clazz.getName() ); List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
} for ( AnnotationMirror mirror : annotationMirrors ) {
if ( annotationClassNames.contains( mirror.getAnnotationType().toString() ) ) {
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); return true;
for ( AnnotationMirror mirror : annotationMirrors ) { }
if ( annotationClassNames.contains( mirror.getAnnotationType().toString() ) ) { }
return true; return false;
} }
}
return false; /**
} * Returns {@code true} if the provided annotation type is of the same type as the provided class, {@code false} otherwise.
* This method uses the string class names for comparison. See also
/** * <a href="http://www.retep.org/2009/02/getting-class-values-from-annotations.html">getting-class-values-from-annotations</a>.
* Returns {@code true} if the provided annotation type is of the same type as the provided class, {@code false} otherwise. *
* This method uses the string class names for comparison. See also {@link http://www.retep.org/2009/02/getting-class-values-from-annotations.html}. * @param annotationMirror The annotation mirror
* * @param clazz the class name to check against
* @param annotationMirror The annotation mirror *
* @param clazz the class name to check against * @return {@code true} if the provided annotation type is of the same type as the provided class, {@code false} otherwise.
* */
* @return {@code true} if the provided annotation type is of the same type as the provided class, {@code false} otherwise. public static boolean isAnnotationMirrorOfType(AnnotationMirror annotationMirror, Class<? extends Annotation> clazz) {
*/ assert clazz != null;
public static boolean isAnnotationMirrorOfType(AnnotationMirror annotationMirror, Class<? extends Annotation> clazz) { return isAnnotationMirrorOfType( annotationMirror, clazz.getName() );
assert clazz != null; }
return isAnnotationMirrorOfType( annotationMirror, clazz.getName() );
} /**
* Returns {@code true} if the provided annotation type is of the same type as the provided class, {@code false} otherwise.
/** * This method uses the string class names for comparison. See also
* Returns {@code true} if the provided annotation type is of the same type as the provided class, {@code false} otherwise. * <a href="http://www.retep.org/2009/02/getting-class-values-from-annotations.html">getting-class-values-from-annotations</a>.
* This method uses the string class names for comparison. See also {@link http://www.retep.org/2009/02/getting-class-values-from-annotations.html}. *
* * @param annotationMirror The annotation mirror
* @param annotationMirror The annotation mirror * @param fqcn the fully qualified class name to check against
* @param fqcn the fully qualified class name to check against *
* * @return {@code true} if the provided annotation type is of the same type as the provided class, {@code false} otherwise.
* @return {@code true} if the provided annotation type is of the same type as the provided class, {@code false} otherwise. */
*/ public static boolean isAnnotationMirrorOfType(AnnotationMirror annotationMirror, String fqcn) {
public static boolean isAnnotationMirrorOfType(AnnotationMirror annotationMirror, String fqcn) { assert annotationMirror != null;
assert annotationMirror != null; assert fqcn != null;
assert fqcn != null; String annotationClassName = annotationMirror.getAnnotationType().toString();
String annotationClassName = annotationMirror.getAnnotationType().toString();
return annotationClassName.equals( fqcn );
return annotationClassName.equals( fqcn ); }
}
/**
public static boolean isTypeElementOfType(TypeElement element, Class<?> clazz) { * Checks whether the {@code Element} hosts the annotation with the given fully qualified class name.
assert element != null; *
assert clazz != null; * @param element the element to check for the hosted annotation
String elementClassName = element.getQualifiedName().toString(); * @param fqcn the fully qualified class name of the annotation to check for
String className = clazz.getName(); *
* @return the annotation mirror for the specified annotation class from the {@code Element} or {@code null} in case
return elementClassName.equals( className ); * the {@code TypeElement} does not host the specified annotation.
} */
public static AnnotationMirror getAnnotationMirror(Element element, String fqcn) {
/** assert element != null;
* Checks whether the {@code Element} hosts the annotation with the given fully qualified class name. assert fqcn != null;
*
* @param element the element to check for the hosted annotation AnnotationMirror mirror = null;
* @param fqcn the fully qualified class name of the annotation to check for for ( AnnotationMirror am : element.getAnnotationMirrors() ) {
* if ( isAnnotationMirrorOfType( am, fqcn ) ) {
* @return the annotation mirror for the specified annotation class from the {@code Element} or {@code null} in case mirror = am;
* the {@code TypeElement} does not host the specified annotation. break;
*/ }
public static AnnotationMirror getAnnotationMirror(Element element, String fqcn) { }
assert element != null; return mirror;
assert fqcn != null; }
AnnotationMirror mirror = null; /**
for ( AnnotationMirror am : element.getAnnotationMirrors() ) { * Checks whether the {@code Element} hosts an annotation of the specified class.
if ( isAnnotationMirrorOfType( am, fqcn ) ) { *
mirror = am; * @param element the element to check for the hosted annotation
break; * @param clazz the annotation class to check for
} *
} * @return the annotation mirror for the specified annotation class from the {@code Element} or {@code null} in case
return mirror; * the {@code TypeElement} does not host the specified annotation.
} */
public static AnnotationMirror getAnnotationMirror(Element element, Class<? extends Annotation> clazz) {
/** assert clazz != null;
* Checks whether the {@code Element} hosts an annotation of the specified class. return getAnnotationMirror( element, clazz.getName() );
* }
* @param element the element to check for the hosted annotation
* @param clazz the annotation class to check for public static Object getAnnotationValue(AnnotationMirror annotationMirror, String parameterValue) {
* assert annotationMirror != null;
* @return the annotation mirror for the specified annotation class from the {@code Element} or {@code null} in case assert parameterValue != null;
* the {@code TypeElement} does not host the specified annotation.
*/ Object returnValue = null;
public static AnnotationMirror getAnnotationMirror(Element element, Class<? extends Annotation> clazz) { for ( Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues()
assert clazz != null; .entrySet() ) {
return getAnnotationMirror( element, clazz.getName() ); if ( parameterValue.equals( entry.getKey().getSimpleName().toString() ) ) {
} returnValue = entry.getValue().getValue();
break;
public static Object getAnnotationValue(AnnotationMirror annotationMirror, String parameterValue) { }
assert annotationMirror != null; }
assert parameterValue != null; return returnValue;
}
Object returnValue = null;
for ( Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : annotationMirror.getElementValues() public static void determineAccessTypeForHierarchy(TypeElement searchedElement, Context context) {
.entrySet() ) { String fqcn = searchedElement.getQualifiedName().toString();
if ( parameterValue.equals( entry.getKey().getSimpleName().toString() ) ) { context.logMessage( Diagnostic.Kind.OTHER, "Determining access type for " + fqcn );
returnValue = entry.getValue().getValue(); AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( fqcn );
break;
} if ( accessTypeInfo != null && accessTypeInfo.isAccessTypeResolved() ) {
} context.logMessage(
return returnValue; Diagnostic.Kind.OTHER,
} "AccessType for " + searchedElement.toString() + " found in cache: " + accessTypeInfo
);
public static void determineAccessTypeForHierarchy(TypeElement searchedElement, Context context) { return;
String fqcn = searchedElement.getQualifiedName().toString(); }
context.logMessage( Diagnostic.Kind.OTHER, "Determining access type for " + fqcn );
AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( fqcn ); // check for explicit access type
AccessType forcedAccessType = determineAnnotationSpecifiedAccessType( searchedElement );
if ( accessTypeInfo != null && accessTypeInfo.isAccessTypeResolved() ) { if ( forcedAccessType != null ) {
context.logMessage( context.logMessage(
Diagnostic.Kind.OTHER, Diagnostic.Kind.OTHER, "Explicit access type on " + searchedElement + ":" + forcedAccessType
"AccessType for " + searchedElement.toString() + " found in cache: " + accessTypeInfo );
); accessTypeInfo = new AccessTypeInformation( fqcn, forcedAccessType, null );
return; context.addAccessTypeInformation( fqcn, accessTypeInfo );
} updateEmbeddableAccessType( searchedElement, context, forcedAccessType );
return;
// check for explicit access type }
AccessType forcedAccessType = determineAnnotationSpecifiedAccessType( searchedElement );
if ( forcedAccessType != null ) { // need to find the default access type for this class
context.logMessage( // let's check first if this entity is the root of the class hierarchy and defines an id. If so the
Diagnostic.Kind.OTHER, "Explicit access type on " + searchedElement + ":" + forcedAccessType // placement of the id annotation determines the access type
); AccessType defaultAccessType = getAccessTypeInCaseElementIsRoot( searchedElement, context );
accessTypeInfo = new AccessTypeInformation( fqcn, forcedAccessType, null ); if ( defaultAccessType != null ) {
context.addAccessTypeInformation( fqcn, accessTypeInfo ); accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType );
updateEmbeddableAccessType( searchedElement, context, forcedAccessType ); context.addAccessTypeInformation( fqcn, accessTypeInfo );
return; updateEmbeddableAccessType( searchedElement, context, defaultAccessType );
} return;
}
// need to find the default access type for this class
// let's check first if this entity is the root of the class hierarchy and defines an id. If so the // if we end up here we need to recursively look for superclasses
// placement of the id annotation determines the access type defaultAccessType = getDefaultAccessForHierarchy( searchedElement, context );
AccessType defaultAccessType = getAccessTypeInCaseElementIsRoot( searchedElement, context ); if ( defaultAccessType == null ) {
if ( defaultAccessType != null ) { defaultAccessType = AccessType.PROPERTY;
accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType ); }
context.addAccessTypeInformation( fqcn, accessTypeInfo ); accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType );
updateEmbeddableAccessType( searchedElement, context, defaultAccessType ); context.addAccessTypeInformation( fqcn, accessTypeInfo );
return; }
}
public static TypeMirror getCollectionElementType(DeclaredType t, String fqNameOfReturnedType, String explicitTargetEntityName, Context context) {
// if we end up here we need to recursively look for superclasses TypeMirror collectionElementType;
defaultAccessType = getDefaultAccessForHierarchy( searchedElement, context ); if ( explicitTargetEntityName != null ) {
if ( defaultAccessType == null ) { Elements elements = context.getElementUtils();
defaultAccessType = AccessType.PROPERTY; TypeElement element = elements.getTypeElement( explicitTargetEntityName );
} collectionElementType = element.asType();
accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType ); }
context.addAccessTypeInformation( fqcn, accessTypeInfo ); else {
} List<? extends TypeMirror> typeArguments = t.getTypeArguments();
if ( typeArguments.size() == 0 ) {
public static TypeMirror getCollectionElementType(DeclaredType t, String fqNameOfReturnedType, String explicitTargetEntityName, Context context) { throw new MetaModelGenerationException( "Unable to determine collection type" );
TypeMirror collectionElementType; }
if ( explicitTargetEntityName != null ) { else if ( Map.class.getCanonicalName().equals( fqNameOfReturnedType ) ) {
Elements elements = context.getElementUtils(); collectionElementType = t.getTypeArguments().get( 1 );
TypeElement element = elements.getTypeElement( explicitTargetEntityName ); }
collectionElementType = element.asType(); else {
} collectionElementType = t.getTypeArguments().get( 0 );
else { }
List<? extends TypeMirror> typeArguments = t.getTypeArguments(); }
if ( typeArguments.size() == 0 ) { return collectionElementType;
throw new MetaModelGenerationException( "Unable to determine collection type" ); }
}
else if ( Map.class.getCanonicalName().equals( fqNameOfReturnedType ) ) { private static void updateEmbeddableAccessType(TypeElement element, Context context, AccessType defaultAccessType) {
collectionElementType = t.getTypeArguments().get( 1 ); List<? extends Element> fieldsOfClass = ElementFilter.fieldsIn( element.getEnclosedElements() );
} for ( Element field : fieldsOfClass ) {
else { updateEmbeddableAccessTypeForMember( context, defaultAccessType, field );
collectionElementType = t.getTypeArguments().get( 0 ); }
}
} List<? extends Element> methodOfClass = ElementFilter.methodsIn( element.getEnclosedElements() );
return collectionElementType; for ( Element method : methodOfClass ) {
} updateEmbeddableAccessTypeForMember( context, defaultAccessType, method );
}
private static void updateEmbeddableAccessType(TypeElement element, Context context, AccessType defaultAccessType) { }
List<? extends Element> fieldsOfClass = ElementFilter.fieldsIn( element.getEnclosedElements() );
for ( Element field : fieldsOfClass ) { private static void updateEmbeddableAccessTypeForMember(Context context, AccessType defaultAccessType, Element member) {
updateEmbeddableAccessTypeForMember( context, defaultAccessType, field ); EmbeddedAttributeVisitor visitor = new EmbeddedAttributeVisitor( context );
} String embeddedClassName = member.asType().accept( visitor, member );
if ( embeddedClassName != null ) {
List<? extends Element> methodOfClass = ElementFilter.methodsIn( element.getEnclosedElements() ); AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( embeddedClassName );
for ( Element method : methodOfClass ) { if ( accessTypeInfo == null ) {
updateEmbeddableAccessTypeForMember( context, defaultAccessType, method ); accessTypeInfo = new AccessTypeInformation( embeddedClassName, null, defaultAccessType );
} context.addAccessTypeInformation( embeddedClassName, accessTypeInfo );
} }
else {
private static void updateEmbeddableAccessTypeForMember(Context context, AccessType defaultAccessType, Element member) { accessTypeInfo.setDefaultAccessType( defaultAccessType );
EmbeddedAttributeVisitor visitor = new EmbeddedAttributeVisitor( context ); }
String embeddedClassName = member.asType().accept( visitor, member ); }
if ( embeddedClassName != null ) { }
AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( embeddedClassName );
if ( accessTypeInfo == null ) { private static AccessType getDefaultAccessForHierarchy(TypeElement element, Context context) {
accessTypeInfo = new AccessTypeInformation( embeddedClassName, null, defaultAccessType ); AccessType defaultAccessType = null;
context.addAccessTypeInformation( embeddedClassName, accessTypeInfo ); TypeElement superClass = element;
} do {
else { superClass = TypeUtils.getSuperclassTypeElement( superClass );
accessTypeInfo.setDefaultAccessType( defaultAccessType ); if ( superClass != null ) {
} String fqcn = superClass.getQualifiedName().toString();
} AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( fqcn );
} if ( accessTypeInfo != null && accessTypeInfo.getDefaultAccessType() != null ) {
return accessTypeInfo.getDefaultAccessType();
private static AccessType getDefaultAccessForHierarchy(TypeElement element, Context context) { }
AccessType defaultAccessType = null; if ( TypeUtils.containsAnnotation( superClass, Entity.class, MappedSuperclass.class ) ) {
TypeElement superClass = element; defaultAccessType = getAccessTypeInCaseElementIsRoot( superClass, context );
do { if ( defaultAccessType != null ) {
superClass = TypeUtils.getSuperclassTypeElement( superClass ); accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType );
if ( superClass != null ) { context.addAccessTypeInformation( fqcn, accessTypeInfo );
String fqcn = superClass.getQualifiedName().toString(); defaultAccessType = accessTypeInfo.getAccessType();
AccessTypeInformation accessTypeInfo = context.getAccessTypeInfo( fqcn ); }
if ( accessTypeInfo != null && accessTypeInfo.getDefaultAccessType() != null ) { else {
return accessTypeInfo.getDefaultAccessType(); defaultAccessType = getDefaultAccessForHierarchy( superClass, context );
} }
if ( TypeUtils.containsAnnotation( superClass, Entity.class, MappedSuperclass.class ) ) { }
defaultAccessType = getAccessTypeInCaseElementIsRoot( superClass, context ); }
if ( defaultAccessType != null ) { }
accessTypeInfo = new AccessTypeInformation( fqcn, null, defaultAccessType ); while ( superClass != null );
context.addAccessTypeInformation( fqcn, accessTypeInfo ); return defaultAccessType;
defaultAccessType = accessTypeInfo.getAccessType(); }
}
else { private static AccessType getAccessTypeInCaseElementIsRoot(TypeElement searchedElement, Context context) {
defaultAccessType = getDefaultAccessForHierarchy( superClass, context ); AccessType defaultAccessType = null;
} List<? extends Element> myMembers = searchedElement.getEnclosedElements();
} for ( Element subElement : myMembers ) {
} List<? extends AnnotationMirror> entityAnnotations =
} context.getElementUtils().getAllAnnotationMirrors( subElement );
while ( superClass != null ); for ( Object entityAnnotation : entityAnnotations ) {
return defaultAccessType; AnnotationMirror annotationMirror = (AnnotationMirror) entityAnnotation;
} if ( isIdAnnotation( annotationMirror ) ) {
defaultAccessType = getAccessTypeOfIdAnnotation( subElement );
private static AccessType getAccessTypeInCaseElementIsRoot(TypeElement searchedElement, Context context) { break;
AccessType defaultAccessType = null; }
List<? extends Element> myMembers = searchedElement.getEnclosedElements(); }
for ( Element subElement : myMembers ) { }
List<? extends AnnotationMirror> entityAnnotations = return defaultAccessType;
context.getElementUtils().getAllAnnotationMirrors( subElement ); }
for ( Object entityAnnotation : entityAnnotations ) {
AnnotationMirror annotationMirror = (AnnotationMirror) entityAnnotation; private static AccessType getAccessTypeOfIdAnnotation(Element element) {
if ( isIdAnnotation( annotationMirror ) ) { AccessType accessType = null;
defaultAccessType = getAccessTypeOfIdAnnotation( subElement ); final ElementKind kind = element.getKind();
break; if ( kind == ElementKind.FIELD || kind == ElementKind.METHOD ) {
} accessType = kind == ElementKind.FIELD ? AccessType.FIELD : AccessType.PROPERTY;
} }
} return accessType;
return defaultAccessType; }
}
private static boolean isIdAnnotation(AnnotationMirror annotationMirror) {
private static AccessType getAccessTypeOfIdAnnotation(Element element) { return TypeUtils.isAnnotationMirrorOfType( annotationMirror, Id.class )
AccessType accessType = null; || TypeUtils.isAnnotationMirrorOfType( annotationMirror, EmbeddedId.class );
final ElementKind kind = element.getKind(); }
if ( kind == ElementKind.FIELD || kind == ElementKind.METHOD ) {
accessType = kind == ElementKind.FIELD ? AccessType.FIELD : AccessType.PROPERTY; public static AccessType determineAnnotationSpecifiedAccessType(Element element) {
} final AnnotationMirror accessAnnotationMirror = TypeUtils.getAnnotationMirror( element, Access.class );
return accessType; AccessType forcedAccessType = null;
} if ( accessAnnotationMirror != null ) {
Element accessElement = (Element) TypeUtils.getAnnotationValue(
private static boolean isIdAnnotation(AnnotationMirror annotationMirror) { accessAnnotationMirror,
return TypeUtils.isAnnotationMirrorOfType( annotationMirror, Id.class ) DEFAULT_ANNOTATION_PARAMETER_NAME
|| TypeUtils.isAnnotationMirrorOfType( annotationMirror, EmbeddedId.class ); );
} if ( accessElement.getKind().equals( ElementKind.ENUM_CONSTANT ) ) {
if ( accessElement.getSimpleName().toString().equals( AccessType.PROPERTY.toString() ) ) {
public static AccessType determineAnnotationSpecifiedAccessType(Element element) { forcedAccessType = AccessType.PROPERTY;
final AnnotationMirror accessAnnotationMirror = TypeUtils.getAnnotationMirror( element, Access.class ); }
AccessType forcedAccessType = null; else if ( accessElement.getSimpleName().toString().equals( AccessType.FIELD.toString() ) ) {
if ( accessAnnotationMirror != null ) { forcedAccessType = AccessType.FIELD;
Element accessElement = (Element) TypeUtils.getAnnotationValue( }
accessAnnotationMirror, }
DEFAULT_ANNOTATION_PARAMETER_NAME }
); return forcedAccessType;
if ( accessElement.getKind().equals( ElementKind.ENUM_CONSTANT ) ) { }
if ( accessElement.getSimpleName().toString().equals( AccessType.PROPERTY.toString() ) ) {
forcedAccessType = AccessType.PROPERTY; public static ElementKind getElementKindForAccessType(AccessType accessType) {
} if ( AccessType.FIELD.equals( accessType ) ) {
else if ( accessElement.getSimpleName().toString().equals( AccessType.FIELD.toString() ) ) { return ElementKind.FIELD;
forcedAccessType = AccessType.FIELD; }
} else {
} return ElementKind.METHOD;
} }
return forcedAccessType; }
}
public static String getKeyType(DeclaredType t, Context context) {
public static ElementKind getElementKindForAccessType(AccessType accessType) { List<? extends TypeMirror> typeArguments = t.getTypeArguments();
if ( AccessType.FIELD.equals( accessType ) ) { if ( typeArguments.size() == 0 ) {
return ElementKind.FIELD; context.logMessage( Diagnostic.Kind.ERROR, "Unable to determine type argument for " + t );
} }
else { return extractClosestRealTypeAsString( typeArguments.get( 0 ), context );
return ElementKind.METHOD; }
}
} static class EmbeddedAttributeVisitor extends SimpleTypeVisitor6<String, Element> {
private Context context;
public static String getKeyType(DeclaredType t, Context context) {
List<? extends TypeMirror> typeArguments = t.getTypeArguments(); EmbeddedAttributeVisitor(Context context) {
if ( typeArguments.size() == 0 ) { this.context = context;
context.logMessage( Diagnostic.Kind.ERROR, "Unable to determine type argument for " + t ); }
}
return extractClosestRealTypeAsString( typeArguments.get( 0 ), context ); @Override
} public String visitDeclared(DeclaredType declaredType, Element element) {
TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType );
static class EmbeddedAttributeVisitor extends SimpleTypeVisitor6<String, Element> { String fqNameOfReturnType = null;
private Context context; if ( containsAnnotation( returnedElement, Embeddable.class ) ) {
fqNameOfReturnType = returnedElement.getQualifiedName().toString();
EmbeddedAttributeVisitor(Context context) { }
this.context = context; return fqNameOfReturnType;
} }
@Override @Override
public String visitDeclared(DeclaredType declaredType, Element element) { public String visitExecutable(ExecutableType t, Element p) {
TypeElement returnedElement = (TypeElement) context.getTypeUtils().asElement( declaredType ); if ( !p.getKind().equals( ElementKind.METHOD ) ) {
String fqNameOfReturnType = null; return null;
if ( containsAnnotation( returnedElement, Embeddable.class ) ) { }
fqNameOfReturnType = returnedElement.getQualifiedName().toString();
} String string = p.getSimpleName().toString();
return fqNameOfReturnType; if ( !StringUtil.isPropertyName( string ) ) {
} return null;
}
@Override
public String visitExecutable(ExecutableType t, Element p) { TypeMirror returnType = t.getReturnType();
if ( !p.getKind().equals( ElementKind.METHOD ) ) { return returnType.accept( this, p );
return null; }
} }
}
String string = p.getSimpleName().toString();
if ( !StringUtil.isPropertyName( string ) ) {
return null;
}
TypeMirror returnType = t.getReturnType();
return returnType.accept( this, p );
}
}
}