HHH-8962 - Create a set of light reflection classes - initial annotation support
This commit is contained in:
parent
351039553f
commit
ea4812b046
|
@ -135,6 +135,7 @@ public class MetadataBuildingProcess {
|
|||
// prep to start handling binding in earnest
|
||||
final MappingDefaultsImpl mappingDefaults = new MappingDefaultsImpl( options );
|
||||
final JavaTypeDescriptorRepository javaTypeDescriptorRepository = new JavaTypeDescriptorRepositoryImpl(
|
||||
jandexView,
|
||||
options.getTempClassLoader(),
|
||||
options.getServiceRegistry()
|
||||
);
|
||||
|
|
|
@ -24,6 +24,8 @@
|
|||
package org.hibernate.metamodel.reflite.internal;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.metamodel.reflite.spi.ClassDescriptor;
|
||||
import org.hibernate.metamodel.reflite.spi.FieldDescriptor;
|
||||
|
@ -31,6 +33,9 @@ import org.hibernate.metamodel.reflite.spi.InterfaceDescriptor;
|
|||
import org.hibernate.metamodel.reflite.spi.MethodDescriptor;
|
||||
import org.hibernate.metamodel.reflite.spi.Name;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
/**
|
||||
* Implementation of a type descriptor
|
||||
*
|
||||
|
@ -41,6 +46,7 @@ public class ClassDescriptorImpl implements ClassDescriptor {
|
|||
|
||||
private final int modifiers;
|
||||
private final boolean hasDefaultConstructor;
|
||||
private final Map<DotName,AnnotationInstance> annotationMap;
|
||||
|
||||
private ClassDescriptor superType;
|
||||
private Collection<InterfaceDescriptor> interfaces;
|
||||
|
@ -48,10 +54,17 @@ public class ClassDescriptorImpl implements ClassDescriptor {
|
|||
private Collection<FieldDescriptor> fieldDescriptors;
|
||||
private Collection<MethodDescriptor> methodDescriptors;
|
||||
|
||||
public ClassDescriptorImpl(Name name, int modifiers, boolean hasDefaultConstructor) {
|
||||
public ClassDescriptorImpl(
|
||||
Name name,
|
||||
int modifiers,
|
||||
boolean hasDefaultConstructor,
|
||||
Map<DotName,AnnotationInstance> annotationMap) {
|
||||
this.name = name;
|
||||
this.modifiers = modifiers;
|
||||
this.hasDefaultConstructor = hasDefaultConstructor;
|
||||
this.annotationMap = annotationMap != null
|
||||
? annotationMap
|
||||
: Collections.<DotName, AnnotationInstance>emptyMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -74,6 +87,11 @@ public class ClassDescriptorImpl implements ClassDescriptor {
|
|||
return interfaces;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<DotName, AnnotationInstance> getAnnotations() {
|
||||
return annotationMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasDefaultConstructor() {
|
||||
return hasDefaultConstructor;
|
||||
|
|
|
@ -63,6 +63,10 @@ class DotNameAdapter implements Name {
|
|||
return dotName.toString();
|
||||
}
|
||||
|
||||
DotName jandexName() {
|
||||
return dotName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DotNameAdapter{dotName=" + dotName.toString() + '}';
|
||||
|
|
|
@ -23,9 +23,15 @@
|
|||
*/
|
||||
package org.hibernate.metamodel.reflite.internal;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.metamodel.reflite.spi.FieldDescriptor;
|
||||
import org.hibernate.metamodel.reflite.spi.JavaTypeDescriptor;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
|
@ -36,12 +42,21 @@ public class FieldDescriptorImpl implements FieldDescriptor {
|
|||
private final int modifiers;
|
||||
|
||||
private final JavaTypeDescriptor declaringType;
|
||||
private final Map<DotName, AnnotationInstance> annotationMap;
|
||||
|
||||
public FieldDescriptorImpl(String name, JavaTypeDescriptor fieldType, int modifiers, JavaTypeDescriptor declaringType) {
|
||||
public FieldDescriptorImpl(
|
||||
String name,
|
||||
JavaTypeDescriptor fieldType,
|
||||
int modifiers,
|
||||
JavaTypeDescriptor declaringType,
|
||||
Map<DotName, AnnotationInstance> annotationMap) {
|
||||
this.name = name;
|
||||
this.fieldType = fieldType;
|
||||
this.modifiers = modifiers;
|
||||
this.declaringType = declaringType;
|
||||
this.annotationMap = annotationMap != null
|
||||
? annotationMap
|
||||
: Collections.<DotName, AnnotationInstance>emptyMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -64,6 +79,11 @@ public class FieldDescriptorImpl implements FieldDescriptor {
|
|||
return declaringType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<DotName, AnnotationInstance> getAnnotations() {
|
||||
return annotationMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FieldDescriptorImpl{" + declaringType.getName().toString() + '#' + name + '}';
|
||||
|
|
|
@ -24,27 +24,39 @@
|
|||
package org.hibernate.metamodel.reflite.internal;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.metamodel.reflite.spi.FieldDescriptor;
|
||||
import org.hibernate.metamodel.reflite.spi.InterfaceDescriptor;
|
||||
import org.hibernate.metamodel.reflite.spi.MethodDescriptor;
|
||||
import org.hibernate.metamodel.reflite.spi.Name;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
public class InterfaceDescriptorImpl implements InterfaceDescriptor {
|
||||
private final Name name;
|
||||
private final int modifiers;
|
||||
private final Map<DotName, AnnotationInstance> annotationMap;
|
||||
|
||||
private Collection<InterfaceDescriptor> extendedInterfaceTypes;
|
||||
|
||||
private Collection<FieldDescriptor> declaredFields;
|
||||
private Collection<MethodDescriptor> declaredMethods;
|
||||
private Collection<FieldDescriptor> fields;
|
||||
private Collection<MethodDescriptor> methods;
|
||||
|
||||
public InterfaceDescriptorImpl(Name name, int modifiers) {
|
||||
public InterfaceDescriptorImpl(
|
||||
Name name,
|
||||
int modifiers,
|
||||
Map<DotName, AnnotationInstance> annotationMap) {
|
||||
this.name = name;
|
||||
this.modifiers = modifiers;
|
||||
this.annotationMap = annotationMap != null
|
||||
? annotationMap
|
||||
: Collections.<DotName, AnnotationInstance>emptyMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -62,14 +74,19 @@ public class InterfaceDescriptorImpl implements InterfaceDescriptor {
|
|||
return extendedInterfaceTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<DotName, AnnotationInstance> getAnnotations() {
|
||||
return annotationMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<FieldDescriptor> getDeclaredFields() {
|
||||
return declaredFields;
|
||||
return fields;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<MethodDescriptor> getDeclaredMethods() {
|
||||
return declaredMethods;
|
||||
return methods;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -81,11 +98,11 @@ public class InterfaceDescriptorImpl implements InterfaceDescriptor {
|
|||
this.extendedInterfaceTypes = extendedInterfaceTypes;
|
||||
}
|
||||
|
||||
void setDeclaredFields(Collection<FieldDescriptor> declaredFields) {
|
||||
this.declaredFields = declaredFields;
|
||||
void setFields(Collection<FieldDescriptor> fields) {
|
||||
this.fields = fields;
|
||||
}
|
||||
|
||||
void setDeclaredMethods(Collection<MethodDescriptor> declaredMethods) {
|
||||
this.declaredMethods = declaredMethods;
|
||||
void setMethods(Collection<MethodDescriptor> methods) {
|
||||
this.methods = methods;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -31,7 +31,6 @@ import java.util.HashMap;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||
import org.hibernate.boot.registry.classloading.spi.ClassLoadingException;
|
||||
import org.hibernate.internal.util.collections.CollectionHelper;
|
||||
|
@ -45,6 +44,13 @@ import org.hibernate.metamodel.reflite.spi.Name;
|
|||
import org.hibernate.metamodel.reflite.spi.VoidDescriptor;
|
||||
import org.hibernate.service.ServiceRegistry;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.ClassInfo;
|
||||
import org.jboss.jandex.DotName;
|
||||
import org.jboss.jandex.FieldInfo;
|
||||
import org.jboss.jandex.IndexView;
|
||||
import org.jboss.jandex.MethodInfo;
|
||||
import org.jboss.jandex.MethodParameterInfo;
|
||||
import org.jboss.logging.Logger;
|
||||
|
||||
/**
|
||||
|
@ -59,14 +65,22 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
|
||||
private ClassLoader jpaTempClassLoader;
|
||||
private final ClassLoaderService classLoaderService;
|
||||
private final IndexView jandexIndex;
|
||||
|
||||
private Map<Name,JavaTypeDescriptor> typeDescriptorMap = new HashMap<Name, JavaTypeDescriptor>();
|
||||
|
||||
public JavaTypeDescriptorRepositoryImpl(ClassLoader jpaTempClassLoader, ServiceRegistry serviceRegistry) {
|
||||
this( jpaTempClassLoader, serviceRegistry.getService( ClassLoaderService.class ) );
|
||||
public JavaTypeDescriptorRepositoryImpl(
|
||||
IndexView jandexIndex,
|
||||
ClassLoader jpaTempClassLoader,
|
||||
ServiceRegistry serviceRegistry) {
|
||||
this( jandexIndex, jpaTempClassLoader, serviceRegistry.getService( ClassLoaderService.class ) );
|
||||
}
|
||||
|
||||
public JavaTypeDescriptorRepositoryImpl(ClassLoader jpaTempClassLoader, ClassLoaderService classLoaderService) {
|
||||
public JavaTypeDescriptorRepositoryImpl(
|
||||
IndexView jandexIndex,
|
||||
ClassLoader jpaTempClassLoader,
|
||||
ClassLoaderService classLoaderService) {
|
||||
this.jandexIndex = jandexIndex;
|
||||
this.jpaTempClassLoader = jpaTempClassLoader;
|
||||
this.classLoaderService = classLoaderService;
|
||||
}
|
||||
|
@ -76,27 +90,6 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
return new DotNameAdapter( name );
|
||||
}
|
||||
|
||||
private JavaTypeDescriptor arrayOfType(Class type) {
|
||||
if ( type.isArray() ) {
|
||||
return new ArrayDescriptorImpl(
|
||||
buildName( "[" + type.getName() ),
|
||||
type.getModifiers(),
|
||||
arrayOfType( type.getComponentType() )
|
||||
);
|
||||
}
|
||||
|
||||
if ( type.isPrimitive() ) {
|
||||
return Primitives.primitiveArrayDescriptor( type );
|
||||
}
|
||||
else {
|
||||
return new ArrayDescriptorImpl(
|
||||
buildName( "[" + type.getName() ),
|
||||
type.getModifiers(),
|
||||
getType( buildName( type.getName() ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public JavaTypeDescriptor getType(Name typeName) {
|
||||
if ( typeName == null ) {
|
||||
|
@ -164,22 +157,27 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
|
||||
private boolean isSafeClass(Name className) {
|
||||
final String classNameString = className.fullName();
|
||||
// classes in any of these packages are safe to load through the "live" ClassLoader
|
||||
return classNameString.startsWith( "java." )
|
||||
|| classNameString.startsWith( "javax." )
|
||||
|| classNameString.startsWith( "org.hibernate" );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
private JavaTypeDescriptor makeTypeDescriptor(Name typeName, Class clazz) {
|
||||
final JandexPivot jandexPivot = pivotAnnotations( toJandexName( typeName ) );
|
||||
|
||||
if ( clazz.isInterface() ) {
|
||||
final InterfaceDescriptorImpl typeDescriptor = new InterfaceDescriptorImpl( typeName, clazz.getModifiers() );
|
||||
final InterfaceDescriptorImpl typeDescriptor = new InterfaceDescriptorImpl(
|
||||
typeName,
|
||||
clazz.getModifiers(),
|
||||
jandexPivot.typeAnnotations
|
||||
);
|
||||
typeDescriptorMap.put( typeName, typeDescriptor );
|
||||
|
||||
typeDescriptor.setExtendedInterfaceTypes( fromInterfaces( clazz ) );
|
||||
typeDescriptor.setDeclaredFields( fromFields( clazz, typeDescriptor ) );
|
||||
typeDescriptor.setDeclaredMethods( fromMethods( clazz, typeDescriptor ) );
|
||||
typeDescriptor.setExtendedInterfaceTypes( extractInterfaces( clazz ) );
|
||||
typeDescriptor.setFields( extractFields( clazz, typeDescriptor, jandexPivot ) );
|
||||
typeDescriptor.setMethods( extractMethods( clazz, typeDescriptor, jandexPivot ) );
|
||||
|
||||
return typeDescriptor;
|
||||
}
|
||||
|
@ -187,20 +185,127 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
final ClassDescriptorImpl typeDescriptor = new ClassDescriptorImpl(
|
||||
typeName,
|
||||
clazz.getModifiers(),
|
||||
hasDefaultCtor( clazz )
|
||||
hasDefaultCtor( clazz ),
|
||||
jandexPivot.typeAnnotations
|
||||
);
|
||||
typeDescriptorMap.put( typeName, typeDescriptor );
|
||||
|
||||
typeDescriptor.setSuperType( fromSuper( clazz ) );
|
||||
typeDescriptor.setInterfaces( fromInterfaces( clazz ) );
|
||||
typeDescriptor.setFields( fromFields( clazz, typeDescriptor ) );
|
||||
typeDescriptor.setMethods( fromMethods( clazz, typeDescriptor ) );
|
||||
typeDescriptor.setSuperType( extractSuper( clazz ) );
|
||||
typeDescriptor.setInterfaces( extractInterfaces( clazz ) );
|
||||
typeDescriptor.setFields( extractFields( clazz, typeDescriptor, jandexPivot ) );
|
||||
typeDescriptor.setMethods( extractMethods( clazz, typeDescriptor, jandexPivot ) );
|
||||
|
||||
return typeDescriptor;
|
||||
}
|
||||
}
|
||||
|
||||
private ClassDescriptor fromSuper(Class clazz) {
|
||||
private DotName toJandexName(Name typeName) {
|
||||
if ( DotNameAdapter.class.isInstance( typeName ) ) {
|
||||
return ( (DotNameAdapter) typeName ).jandexName();
|
||||
}
|
||||
else {
|
||||
return DotName.createSimple( typeName.fullName() );
|
||||
}
|
||||
}
|
||||
|
||||
private static final JandexPivot NO_JANDEX_PIVOT = new JandexPivot();
|
||||
|
||||
private JandexPivot pivotAnnotations(DotName typeName) {
|
||||
if ( jandexIndex == null ) {
|
||||
return NO_JANDEX_PIVOT;
|
||||
}
|
||||
|
||||
final ClassInfo jandexClassInfo = jandexIndex.getClassByName( typeName );
|
||||
if ( jandexClassInfo == null ) {
|
||||
return NO_JANDEX_PIVOT;
|
||||
}
|
||||
|
||||
final Map<DotName, List<AnnotationInstance>> annotations = jandexClassInfo.annotations();
|
||||
final JandexPivot pivot = new JandexPivot();
|
||||
for ( Map.Entry<DotName, List<AnnotationInstance>> annotationInstances : annotations.entrySet() ) {
|
||||
for ( AnnotationInstance annotationInstance : annotationInstances.getValue() ) {
|
||||
if ( MethodParameterInfo.class.isInstance( annotationInstance.target() ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ( FieldInfo.class.isInstance( annotationInstance.target() ) ) {
|
||||
final FieldInfo fieldInfo = (FieldInfo) annotationInstance.target();
|
||||
Map<DotName,AnnotationInstance> fieldAnnotations = pivot.fieldAnnotations.get( fieldInfo.name() );
|
||||
if ( fieldAnnotations == null ) {
|
||||
fieldAnnotations = new HashMap<DotName, AnnotationInstance>();
|
||||
pivot.fieldAnnotations.put( fieldInfo.name(), fieldAnnotations );
|
||||
fieldAnnotations.put( annotationInstance.name(), annotationInstance );
|
||||
}
|
||||
else {
|
||||
final Object oldEntry = fieldAnnotations.put( annotationInstance.name(), annotationInstance );
|
||||
if ( oldEntry != null ) {
|
||||
log.debugf(
|
||||
"Encountered duplicate annotation [%s] on field [%s]",
|
||||
annotationInstance.name(),
|
||||
fieldInfo.name()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ( MethodInfo.class.isInstance( annotationInstance.target() ) ) {
|
||||
final MethodInfo methodInfo = (MethodInfo) annotationInstance.target();
|
||||
final String methodKey = buildBuildKey( methodInfo );
|
||||
Map<DotName,AnnotationInstance> methodAnnotations = pivot.methodAnnotations.get( methodKey );
|
||||
if ( methodAnnotations == null ) {
|
||||
methodAnnotations = new HashMap<DotName, AnnotationInstance>();
|
||||
pivot.methodAnnotations.put( methodKey, methodAnnotations );
|
||||
methodAnnotations.put( annotationInstance.name(), annotationInstance );
|
||||
}
|
||||
else {
|
||||
final Object oldEntry = methodAnnotations.put( annotationInstance.name(), annotationInstance );
|
||||
if ( oldEntry != null ) {
|
||||
log.debugf(
|
||||
"Encountered duplicate annotation [%s] on method [%s -> %s]",
|
||||
annotationInstance.name(),
|
||||
jandexClassInfo.name(),
|
||||
methodKey
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ( ClassInfo.class.isInstance( annotationInstance.target() ) ) {
|
||||
// todo : validate its the type we are processing?
|
||||
final Object oldEntry = pivot.typeAnnotations.put( annotationInstance.name(), annotationInstance );
|
||||
if ( oldEntry != null ) {
|
||||
log.debugf(
|
||||
"Encountered duplicate annotation [%s] on type [%s]",
|
||||
annotationInstance.name(),
|
||||
jandexClassInfo.name()
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return pivot;
|
||||
}
|
||||
|
||||
private String buildBuildKey(MethodInfo methodInfo) {
|
||||
final StringBuilder buff = new StringBuilder();
|
||||
buff.append( methodInfo.returnType().toString() )
|
||||
.append( ' ' )
|
||||
.append( methodInfo.name() )
|
||||
.append( '(' );
|
||||
for ( int i = 0; i < methodInfo.args().length; i++ ) {
|
||||
if ( i > 0 ) {
|
||||
buff.append( ',' );
|
||||
}
|
||||
buff.append( methodInfo.args()[i].toString() );
|
||||
}
|
||||
|
||||
return buff.append( ')' ).toString();
|
||||
}
|
||||
|
||||
private Collection<AnnotationInstance> getAnnotations(DotName dotName) {
|
||||
return jandexIndex.getAnnotations( dotName );
|
||||
}
|
||||
|
||||
private ClassDescriptor extractSuper(Class clazz) {
|
||||
final Class superclass = clazz.getSuperclass();
|
||||
if ( superclass == null ) {
|
||||
return null;
|
||||
|
@ -209,7 +314,7 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
return (ClassDescriptor) getType( buildName( superclass.getName() ) );
|
||||
}
|
||||
|
||||
private Collection<InterfaceDescriptor> fromInterfaces(Class clazz) {
|
||||
private Collection<InterfaceDescriptor> extractInterfaces(Class clazz) {
|
||||
final Class[] interfaces = clazz.getInterfaces();
|
||||
if ( interfaces == null || interfaces.length <= 0 ) {
|
||||
return Collections.emptyList();
|
||||
|
@ -222,21 +327,44 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
return interfaceTypes;
|
||||
}
|
||||
|
||||
private Collection<FieldDescriptor> fromFields(Class clazz, JavaTypeDescriptor declaringType) {
|
||||
final Field[] fields = clazz.getDeclaredFields();
|
||||
if ( fields == null || fields.length <= 0 ) {
|
||||
private Collection<FieldDescriptor> extractFields(
|
||||
Class clazz,
|
||||
JavaTypeDescriptor declaringType,
|
||||
JandexPivot jandexPivot) {
|
||||
final Field[] declaredFields = clazz.getDeclaredFields();
|
||||
final Field[] fields = clazz.getFields();
|
||||
|
||||
if ( declaredFields.length <= 0 && fields.length <= 0 ) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
final List<FieldDescriptor> fieldDescriptors = CollectionHelper.arrayList( fields.length );
|
||||
for ( Field field : fields ) {
|
||||
final Class fieldType = field.getType();
|
||||
|
||||
for ( Field field : declaredFields ) {
|
||||
fieldDescriptors.add(
|
||||
new FieldDescriptorImpl(
|
||||
field.getName(),
|
||||
toTypeDescriptor( fieldType ),
|
||||
toTypeDescriptor( field.getType() ),
|
||||
field.getModifiers(),
|
||||
declaringType
|
||||
declaringType,
|
||||
jandexPivot.fieldAnnotations.get( field.getName() )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
for ( Field field : fields ) {
|
||||
if ( clazz.equals( field.getDeclaringClass() ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final JavaTypeDescriptor fieldDeclarer = getType( buildName( field.getDeclaringClass().getName() ) );
|
||||
fieldDescriptors.add(
|
||||
new FieldDescriptorImpl(
|
||||
field.getName(),
|
||||
toTypeDescriptor( field.getType() ),
|
||||
field.getModifiers(),
|
||||
fieldDeclarer,
|
||||
jandexPivot.fieldAnnotations.get( field.getName() )
|
||||
)
|
||||
);
|
||||
}
|
||||
|
@ -254,38 +382,96 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
return fieldTypeDescriptor;
|
||||
}
|
||||
|
||||
private Collection<MethodDescriptor> fromMethods(Class clazz, JavaTypeDescriptor declaringType) {
|
||||
final Method[] methods = clazz.getDeclaredMethods();
|
||||
if ( methods == null || methods.length <= 0 ) {
|
||||
private JavaTypeDescriptor arrayOfType(Class type) {
|
||||
if ( type.isArray() ) {
|
||||
return new ArrayDescriptorImpl(
|
||||
buildName( "[" + type.getName() ),
|
||||
type.getModifiers(),
|
||||
arrayOfType( type.getComponentType() )
|
||||
);
|
||||
}
|
||||
|
||||
if ( type.isPrimitive() ) {
|
||||
return Primitives.primitiveArrayDescriptor( type );
|
||||
}
|
||||
else {
|
||||
return new ArrayDescriptorImpl(
|
||||
buildName( "[" + type.getName() ),
|
||||
type.getModifiers(),
|
||||
getType( buildName( type.getName() ) )
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
private Collection<MethodDescriptor> extractMethods(
|
||||
Class clazz,
|
||||
JavaTypeDescriptor declaringType,
|
||||
JandexPivot jandexPivot) {
|
||||
final Method[] declaredMethods = clazz.getDeclaredMethods();
|
||||
final Method[] methods = clazz.getMethods();
|
||||
|
||||
if ( declaredMethods.length <= 0 && methods.length <= 0 ) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
final List<MethodDescriptor> methodDescriptors = CollectionHelper.arrayList( methods.length );
|
||||
for ( Method method : methods ) {
|
||||
final Class[] parameterTypes = method.getParameterTypes();
|
||||
final Collection<JavaTypeDescriptor> argumentTypes;
|
||||
if ( parameterTypes.length == 0 ) {
|
||||
argumentTypes = Collections.emptyList();
|
||||
}
|
||||
else {
|
||||
argumentTypes = CollectionHelper.arrayList( parameterTypes.length );
|
||||
for ( Class parameterType : parameterTypes ) {
|
||||
argumentTypes.add( toTypeDescriptor( parameterType ) );
|
||||
}
|
||||
}
|
||||
methodDescriptors.add(
|
||||
new MethodDescriptorImpl(
|
||||
method.getName(),
|
||||
declaringType,
|
||||
method.getModifiers(),
|
||||
toTypeDescriptor( method.getReturnType() ),
|
||||
argumentTypes
|
||||
)
|
||||
);
|
||||
|
||||
for ( Method method : declaredMethods ) {
|
||||
methodDescriptors.add( fromMethod( method, declaringType, jandexPivot ) );
|
||||
}
|
||||
|
||||
for ( Method method : methods ) {
|
||||
if ( clazz.equals( method.getDeclaringClass() ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
final JavaTypeDescriptor methodDeclarer = getType( buildName( method.getDeclaringClass().getName() ) );
|
||||
methodDescriptors.add( fromMethod( method, methodDeclarer, jandexPivot ) );
|
||||
}
|
||||
|
||||
return methodDescriptors;
|
||||
}
|
||||
|
||||
private MethodDescriptor fromMethod(
|
||||
Method method,
|
||||
JavaTypeDescriptor declaringType,
|
||||
JandexPivot jandexPivot) {
|
||||
final Class[] parameterTypes = method.getParameterTypes();
|
||||
final Collection<JavaTypeDescriptor> argumentTypes;
|
||||
if ( parameterTypes.length == 0 ) {
|
||||
argumentTypes = Collections.emptyList();
|
||||
}
|
||||
else {
|
||||
argumentTypes = CollectionHelper.arrayList( parameterTypes.length );
|
||||
for ( Class parameterType : parameterTypes ) {
|
||||
argumentTypes.add( toTypeDescriptor( parameterType ) );
|
||||
}
|
||||
}
|
||||
|
||||
return new MethodDescriptorImpl(
|
||||
method.getName(),
|
||||
declaringType,
|
||||
method.getModifiers(),
|
||||
toTypeDescriptor( method.getReturnType() ),
|
||||
argumentTypes,
|
||||
jandexPivot.methodAnnotations.get( buildMethodAnnotationsKey( method ) )
|
||||
);
|
||||
}
|
||||
|
||||
private String buildMethodAnnotationsKey(Method method) {
|
||||
StringBuilder buff = new StringBuilder();
|
||||
buff.append( method.getReturnType().getName() )
|
||||
.append( method.getName() )
|
||||
.append( '(' );
|
||||
for ( int i = 0; i < method.getParameterTypes().length; i++ ) {
|
||||
if ( i > 0 ) {
|
||||
buff.append( ',' );
|
||||
}
|
||||
buff.append( method.getParameterTypes()[i].getName() );
|
||||
}
|
||||
return buff.append( ')' ).toString();
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static boolean hasDefaultCtor(Class clazz) {
|
||||
|
@ -297,16 +483,6 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
}
|
||||
}
|
||||
|
||||
public static void main(String... args) {
|
||||
JavaTypeDescriptorRepositoryImpl repo = new JavaTypeDescriptorRepositoryImpl(
|
||||
JavaTypeDescriptorRepositoryImpl.class.getClassLoader(),
|
||||
new BootstrapServiceRegistryBuilder().build()
|
||||
);
|
||||
|
||||
JavaTypeDescriptor td = repo.getType( repo.buildName( JavaTypeDescriptorRepositoryImpl.class.getName() ) );
|
||||
assert ClassDescriptorImpl.class.isInstance( td );
|
||||
}
|
||||
|
||||
private static class NoSuchClassTypeDescriptor implements JavaTypeDescriptor {
|
||||
private final Name name;
|
||||
|
||||
|
@ -334,4 +510,14 @@ public class JavaTypeDescriptorRepositoryImpl implements JavaTypeDescriptorRepos
|
|||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
private static class JandexPivot {
|
||||
private Map<DotName,AnnotationInstance> typeAnnotations
|
||||
= new HashMap<DotName, AnnotationInstance>();
|
||||
private Map<String,Map<DotName,AnnotationInstance>> fieldAnnotations
|
||||
= new HashMap<String, Map<DotName, AnnotationInstance>>();
|
||||
private Map<String,Map<DotName,AnnotationInstance>> methodAnnotations
|
||||
= new HashMap<String, Map<DotName, AnnotationInstance>>();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -24,10 +24,15 @@
|
|||
package org.hibernate.metamodel.reflite.internal;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.hibernate.metamodel.reflite.spi.JavaTypeDescriptor;
|
||||
import org.hibernate.metamodel.reflite.spi.MethodDescriptor;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
/**
|
||||
* @author Steve Ebersole
|
||||
*/
|
||||
|
@ -37,18 +42,23 @@ public class MethodDescriptorImpl implements MethodDescriptor {
|
|||
private final int modifiers;
|
||||
private final JavaTypeDescriptor returnType;
|
||||
private final Collection<JavaTypeDescriptor> parameterTypes;
|
||||
private final Map<DotName, AnnotationInstance> annotationMap;
|
||||
|
||||
public MethodDescriptorImpl(
|
||||
String name,
|
||||
JavaTypeDescriptor declaringType,
|
||||
int modifiers,
|
||||
JavaTypeDescriptor returnType,
|
||||
Collection<JavaTypeDescriptor> parameterTypes) {
|
||||
Collection<JavaTypeDescriptor> parameterTypes,
|
||||
Map<DotName, AnnotationInstance> annotationMap) {
|
||||
this.name = name;
|
||||
this.declaringType = declaringType;
|
||||
this.modifiers = modifiers;
|
||||
this.returnType = returnType;
|
||||
this.parameterTypes = parameterTypes;
|
||||
this.annotationMap = annotationMap != null
|
||||
? annotationMap
|
||||
: Collections.<DotName, AnnotationInstance>emptyMap();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -76,6 +86,11 @@ public class MethodDescriptorImpl implements MethodDescriptor {
|
|||
return parameterTypes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map<DotName, AnnotationInstance> getAnnotations() {
|
||||
return annotationMap;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MethodDescriptorImpl{" + declaringType.getName().toString() + '#' + name + '}';
|
||||
|
|
|
@ -24,6 +24,10 @@
|
|||
package org.hibernate.metamodel.reflite.spi;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
/**
|
||||
* Describes a java type that represents a class definition.
|
||||
|
@ -51,4 +55,11 @@ public interface ClassDescriptor extends JavaTypeDescriptor {
|
|||
* @return The implemented interfaces
|
||||
*/
|
||||
public Collection<InterfaceDescriptor> getInterfaceTypes();
|
||||
|
||||
/**
|
||||
* Get the annotations defined on this type.
|
||||
*
|
||||
* @return The annotations.
|
||||
*/
|
||||
public Map<DotName, AnnotationInstance> getAnnotations();
|
||||
}
|
||||
|
|
|
@ -23,6 +23,11 @@
|
|||
*/
|
||||
package org.hibernate.metamodel.reflite.spi;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
/**
|
||||
* Describes a field in a java type
|
||||
*
|
||||
|
@ -51,4 +56,11 @@ public interface FieldDescriptor {
|
|||
* @return
|
||||
*/
|
||||
public JavaTypeDescriptor getDeclaringType();
|
||||
|
||||
/**
|
||||
* Get the annotations defined on this field.
|
||||
*
|
||||
* @return The annotations.
|
||||
*/
|
||||
public Map<DotName, AnnotationInstance> getAnnotations();
|
||||
}
|
||||
|
|
|
@ -24,6 +24,10 @@
|
|||
package org.hibernate.metamodel.reflite.spi;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
/**
|
||||
* Describes a java type that represents an interface definition.
|
||||
|
@ -37,4 +41,11 @@ public interface InterfaceDescriptor extends JavaTypeDescriptor {
|
|||
* @return The implemented interfaces
|
||||
*/
|
||||
public Collection<InterfaceDescriptor> getExtendedInterfaceTypes();
|
||||
|
||||
/**
|
||||
* Get the annotations defined on this type.
|
||||
*
|
||||
* @return The annotations.
|
||||
*/
|
||||
public Map<DotName, AnnotationInstance> getAnnotations();
|
||||
}
|
||||
|
|
|
@ -24,6 +24,10 @@
|
|||
package org.hibernate.metamodel.reflite.spi;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.jboss.jandex.AnnotationInstance;
|
||||
import org.jboss.jandex.DotName;
|
||||
|
||||
/**
|
||||
* Describes a method in a java type
|
||||
|
@ -65,4 +69,12 @@ public interface MethodDescriptor {
|
|||
* @return The descriptor of the method's argument types
|
||||
*/
|
||||
public Collection<JavaTypeDescriptor> getParameterTypes();
|
||||
|
||||
/**
|
||||
* Get the annotations defined on this method.
|
||||
*
|
||||
* @return The annotations.
|
||||
*/
|
||||
public Map<DotName, AnnotationInstance> getAnnotations();
|
||||
|
||||
}
|
||||
|
|
|
@ -44,7 +44,11 @@ public class RootBindingContextBuilder {
|
|||
final MetadataBuildingProcess.MappingDefaultsImpl mappingDefaults = new MetadataBuildingProcess.MappingDefaultsImpl(
|
||||
options
|
||||
);
|
||||
final JavaTypeDescriptorRepository javaTypeDescriptorRepository = new JavaTypeDescriptorRepositoryImpl( null, serviceRegistry );
|
||||
final JavaTypeDescriptorRepository javaTypeDescriptorRepository = new JavaTypeDescriptorRepositoryImpl(
|
||||
null,
|
||||
null,
|
||||
serviceRegistry
|
||||
);
|
||||
final MetadataBuildingProcess.InFlightMetadataCollectorImpl metadataCollector = new MetadataBuildingProcess.InFlightMetadataCollectorImpl(
|
||||
options,
|
||||
new TypeResolver( basicTypeRegistry, new TypeFactory() )
|
||||
|
|
|
@ -109,6 +109,7 @@ public class SimpleValueBindingTests extends BaseUnitTestCase {
|
|||
}
|
||||
|
||||
private final JavaTypeDescriptorRepository javaTypeDescriptorRepository = new JavaTypeDescriptorRepositoryImpl(
|
||||
null,
|
||||
null,
|
||||
classLoaderService
|
||||
);
|
||||
|
|
|
@ -162,7 +162,6 @@ public class StatsTest extends BaseUnitTestCase {
|
|||
// }
|
||||
|
||||
@Test
|
||||
@FailureExpectedWithNewMetamodel( jiraKey = "HHH-8962" )
|
||||
public void testQueryStatGathering() {
|
||||
SessionFactory sf = buildBaseConfiguration()
|
||||
.setProperty( AvailableSettings.HBM2DDL_AUTO, "create-drop" )
|
||||
|
|
Loading…
Reference in New Issue