HHH-8962 - Create a set of light reflection classes - modifier support

This commit is contained in:
Steve Ebersole 2014-02-13 07:17:03 -06:00
parent b2ab350866
commit 50ff90b3b4
10 changed files with 166 additions and 45 deletions

View File

@ -32,10 +32,12 @@ import org.hibernate.metamodel.reflite.spi.TypeDescriptor;
*/
public class ArrayTypeDescriptorImpl implements ArrayTypeDescriptor {
private final Name name;
private final int modifiers;
private TypeDescriptor componentType;
public ArrayTypeDescriptorImpl(Name name, TypeDescriptor componentType) {
public ArrayTypeDescriptorImpl(Name name, int modifiers, TypeDescriptor componentType) {
this.name = name;
this.modifiers = modifiers;
this.componentType = componentType;
}
@ -44,6 +46,11 @@ public class ArrayTypeDescriptorImpl implements ArrayTypeDescriptor {
return name;
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public TypeDescriptor getComponentTypeDescriptor() {
return componentType;

View File

@ -40,13 +40,15 @@ public class ClassDescriptorImpl implements ClassDescriptor {
private TypeDescriptor superType;
private TypeDescriptor[] interfaces;
private final int modifiers;
private final boolean hasDefaultConstructor;
private FieldDescriptor[] fieldDescriptors;
private MethodDescriptor[] methodDescriptors;
public ClassDescriptorImpl(Name name, boolean hasDefaultConstructor) {
public ClassDescriptorImpl(Name name, int modifiers, boolean hasDefaultConstructor) {
this.name = name;
this.modifiers = modifiers;
this.hasDefaultConstructor = hasDefaultConstructor;
}
@ -55,6 +57,11 @@ public class ClassDescriptorImpl implements ClassDescriptor {
return name;
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public boolean isInterface() {
return false;
@ -115,4 +122,9 @@ public class ClassDescriptorImpl implements ClassDescriptor {
void setMethods(MethodDescriptor[] methodDescriptors) {
this.methodDescriptors = methodDescriptors;
}
@Override
public String toString() {
return "ClassDescriptorImpl{" + name.toString() + '}';
}
}

View File

@ -33,11 +33,14 @@ public class FieldDescriptorImpl implements FieldDescriptor {
private final String name;
private final TypeDescriptor fieldType;
private final int modifiers;
private final TypeDescriptor declaringType;
public FieldDescriptorImpl(String name, TypeDescriptor fieldType, TypeDescriptor declaringType) {
public FieldDescriptorImpl(String name, TypeDescriptor fieldType, int modifiers, TypeDescriptor declaringType) {
this.name = name;
this.fieldType = fieldType;
this.modifiers = modifiers;
this.declaringType = declaringType;
}
@ -51,8 +54,18 @@ public class FieldDescriptorImpl implements FieldDescriptor {
return fieldType;
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public TypeDescriptor getDeclaringType() {
return declaringType;
}
@Override
public String toString() {
return "FieldDescriptorImpl{" + declaringType.getName().toString() + '#' + name + '}';
}
}

View File

@ -34,13 +34,15 @@ import org.hibernate.metamodel.reflite.spi.TypeDescriptor;
*/
public class InterfaceDescriptorImpl implements InterfaceDescriptor {
private final Name name;
private final int modifiers;
private TypeDescriptor[] extendedInterfaceTypes;
private FieldDescriptor[] declaredFields;
private MethodDescriptor[] declaredMethods;
public InterfaceDescriptorImpl(Name name) {
public InterfaceDescriptorImpl(Name name, int modifiers) {
this.name = name;
this.modifiers = modifiers;
}
@Override
@ -48,6 +50,11 @@ public class InterfaceDescriptorImpl implements InterfaceDescriptor {
return name;
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public boolean isInterface() {
return true;
@ -94,4 +101,9 @@ public class InterfaceDescriptorImpl implements InterfaceDescriptor {
void setDeclaredMethods(MethodDescriptor[] declaredMethods) {
this.declaredMethods = declaredMethods;
}
@Override
public String toString() {
return "InterfaceDescriptorImpl{" + name.toString() + '}';
}
}

View File

@ -32,16 +32,19 @@ import org.hibernate.metamodel.reflite.spi.TypeDescriptor;
public class MethodDescriptorImpl implements MethodDescriptor {
private final String name;
private final TypeDescriptor declaringType;
private final int modifiers;
private final TypeDescriptor returnType;
private final TypeDescriptor[] parameterTypes;
public MethodDescriptorImpl(
String name,
TypeDescriptor declaringType,
int modifiers,
TypeDescriptor returnType,
TypeDescriptor[] parameterTypes) {
this.name = name;
this.declaringType = declaringType;
this.modifiers = modifiers;
this.returnType = returnType;
this.parameterTypes = parameterTypes;
}
@ -56,6 +59,11 @@ public class MethodDescriptorImpl implements MethodDescriptor {
return declaringType;
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public TypeDescriptor getReturnType() {
return returnType;
@ -65,4 +73,9 @@ public class MethodDescriptorImpl implements MethodDescriptor {
public TypeDescriptor[] getParameterTypes() {
return parameterTypes;
}
@Override
public String toString() {
return "MethodDescriptorImpl{" + declaringType.getName().toString() + '#' + name + '}';
}
}

View File

@ -38,44 +38,31 @@ public class Primitives {
private final PrimitiveWrapperTypeDescriptor primitiveWrapperType;
private final ArrayTypeDescriptor primitiveArrayType;
private final ArrayTypeDescriptor primitiveWrapperArrayType;
public PrimitiveGroup(Class primitiveClass, Class wrapperClass) {
public PrimitiveGroup(Class primitiveClass, Class primitiveArrayClass, Class wrapperClass) {
assert primitiveClass.isPrimitive();
assert primitiveArrayClass.isArray();
assert !wrapperClass.isPrimitive();
this.primitiveType = new PrimitiveDescriptorImpl( primitiveClass.getName(), this );
this.primitiveWrapperType = new WrapperDescriptorImpl( wrapperClass.getName(), this );
this.primitiveType = new PrimitiveDescriptorImpl( primitiveClass, this );
this.primitiveWrapperType = new WrapperDescriptorImpl( wrapperClass, this );
this.primitiveArrayType = new ArrayTypeDescriptorImpl( null, this.primitiveType );
this.primitiveWrapperArrayType = new ArrayTypeDescriptorImpl( null, this.primitiveWrapperType );
}
public PrimitiveTypeDescriptor getPrimitiveType() {
return primitiveType;
}
public PrimitiveWrapperTypeDescriptor getPrimitiveWrapperType() {
return primitiveWrapperType;
}
public ArrayTypeDescriptor getPrimitiveArrayType() {
return primitiveArrayType;
}
public ArrayTypeDescriptor getPrimitiveWrapperArrayType() {
return primitiveWrapperArrayType;
this.primitiveArrayType = new ArrayTypeDescriptorImpl(
new DotNameAdapter( primitiveArrayClass.getName() ),
primitiveArrayClass.getModifiers(),
this.primitiveType
);
}
}
public static final PrimitiveGroup CHAR = new PrimitiveGroup( char.class, Character.class );
public static final PrimitiveGroup BOOLEAN = new PrimitiveGroup( boolean.class, Boolean.class );
public static final PrimitiveGroup BYTE = new PrimitiveGroup( byte.class, Byte.class );
public static final PrimitiveGroup SHORT = new PrimitiveGroup( short.class, Short.class );
public static final PrimitiveGroup INTEGER = new PrimitiveGroup( int.class, Integer.class );
public static final PrimitiveGroup LONG = new PrimitiveGroup( long.class, Long.class );
public static final PrimitiveGroup FLOAT = new PrimitiveGroup( float.class, Float.class );
public static final PrimitiveGroup DOUBLE = new PrimitiveGroup( double.class, Double.class );
public static final PrimitiveGroup CHAR = new PrimitiveGroup( char.class, char[].class, Character.class );
public static final PrimitiveGroup BOOLEAN = new PrimitiveGroup( boolean.class, boolean[].class, Boolean.class );
public static final PrimitiveGroup BYTE = new PrimitiveGroup( byte.class, byte[].class, Byte.class );
public static final PrimitiveGroup SHORT = new PrimitiveGroup( short.class, short[].class, Short.class );
public static final PrimitiveGroup INTEGER = new PrimitiveGroup( int.class, int[].class, Integer.class );
public static final PrimitiveGroup LONG = new PrimitiveGroup( long.class, long[].class, Long.class );
public static final PrimitiveGroup FLOAT = new PrimitiveGroup( float.class, float[].class, Float.class );
public static final PrimitiveGroup DOUBLE = new PrimitiveGroup( double.class, double[].class, Double.class );
public static TypeDescriptor resolveByName(Name name) {
assert name != null;
@ -85,7 +72,7 @@ public class Primitives {
if ( char.class.getName().equals( typeNameString ) ) {
return CHAR.primitiveType;
}
else if ( Character.class.getName().equals( name ) ) {
else if ( Character.class.getName().equals( typeNameString ) ) {
return CHAR.primitiveWrapperType;
}
else if ( boolean.class.getName().equals( typeNameString ) ) {
@ -134,12 +121,52 @@ public class Primitives {
return null;
}
public static TypeDescriptor primitiveArrayDescriptor(Class type) {
assert type != null;
assert type.isPrimitive();
final String typeNameString = type.getName();
if ( char.class.getName().equals( typeNameString ) ) {
return CHAR.primitiveType;
}
else if ( boolean.class.getName().equals( typeNameString ) ) {
return BOOLEAN.primitiveType;
}
else if ( byte.class.getName().equals( typeNameString ) ) {
return BYTE.primitiveType;
}
else if ( short.class.getName().equals( typeNameString ) ) {
return SHORT.primitiveType;
}
else if ( int.class.getName().equals( typeNameString ) ) {
return INTEGER.primitiveType;
}
else if ( long.class.getName().equals( typeNameString ) ) {
return LONG.primitiveType;
}
else if ( float.class.getName().equals( typeNameString ) ) {
return FLOAT.primitiveType;
}
else if ( double.class.getName().equals( typeNameString ) ) {
return DOUBLE.primitiveType;
}
else if ( double.class.getName().equals( typeNameString ) ) {
return DOUBLE.primitiveWrapperType;
}
return null;
}
private static class PrimitiveDescriptorImpl implements PrimitiveTypeDescriptor {
private final Name name;
private final int modifiers;
private final PrimitiveGroup group;
protected PrimitiveDescriptorImpl(String simpleName, PrimitiveGroup group) {
this.name = new DotNameAdapter( simpleName );
protected PrimitiveDescriptorImpl(Class clazz, PrimitiveGroup group) {
this.name = new DotNameAdapter( clazz.getName() );
this.modifiers = clazz.getModifiers();
this.group = group;
}
@ -148,6 +175,11 @@ public class Primitives {
return name;
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public PrimitiveWrapperTypeDescriptor getWrapperTypeDescriptor() {
return group.primitiveWrapperType;
@ -176,10 +208,12 @@ public class Primitives {
private static class WrapperDescriptorImpl implements PrimitiveWrapperTypeDescriptor {
private final Name name;
private final int modifiers;
private final PrimitiveGroup group;
private WrapperDescriptorImpl(String simpleName, PrimitiveGroup group) {
this.name = new DotNameAdapter( simpleName );
private WrapperDescriptorImpl(Class clazz, PrimitiveGroup group) {
this.name = new DotNameAdapter( clazz.getName() );
this.modifiers = clazz.getModifiers();
this.group = group;
}
@ -188,6 +222,11 @@ public class Primitives {
return name;
}
@Override
public int getModifiers() {
return modifiers;
}
@Override
public PrimitiveTypeDescriptor getPrimitiveTypeDescriptor() {
return group.primitiveType;

View File

@ -69,14 +69,21 @@ public class RepositoryImpl implements Repository {
if ( type.isArray() ) {
return new ArrayTypeDescriptorImpl(
buildName( "[" + type.getName() ),
type.getModifiers(),
arrayOfType( type.getComponentType() )
);
}
return new ArrayTypeDescriptorImpl(
buildName( "[" + type.getName() ),
getType( buildName( type.getName() ) )
);
if ( type.isPrimitive() ) {
return Primitives.primitiveArrayDescriptor( type );
}
else {
return new ArrayTypeDescriptorImpl(
buildName( "[" + type.getName() ),
type.getModifiers(),
getType( buildName( type.getName() ) )
);
}
}
@Override
@ -151,7 +158,7 @@ public class RepositoryImpl implements Repository {
private TypeDescriptor makeTypeDescriptor(Name typeName, Class clazz) {
if ( clazz.isInterface() ) {
final InterfaceDescriptorImpl typeDescriptor = new InterfaceDescriptorImpl( typeName );
final InterfaceDescriptorImpl typeDescriptor = new InterfaceDescriptorImpl( typeName, clazz.getModifiers() );
typeDescriptorMap.put( typeName, typeDescriptor );
typeDescriptor.setExtendedInterfaceTypes( fromInterfaces( clazz ) );
@ -161,7 +168,11 @@ public class RepositoryImpl implements Repository {
return typeDescriptor;
}
else {
final ClassDescriptorImpl typeDescriptor = new ClassDescriptorImpl( typeName, hasDefaultCtor( clazz ) );
final ClassDescriptorImpl typeDescriptor = new ClassDescriptorImpl(
typeName,
clazz.getModifiers(),
hasDefaultCtor( clazz )
);
typeDescriptorMap.put( typeName, typeDescriptor );
typeDescriptor.setSuperType( fromSuper( clazz ) );
@ -211,6 +222,7 @@ public class RepositoryImpl implements Repository {
fieldDescriptors[i] = new FieldDescriptorImpl(
fields[i].getName(),
toTypeDescriptor( fieldType ),
fields[i].getModifiers(),
declaringType
);
}
@ -252,6 +264,7 @@ public class RepositoryImpl implements Repository {
methodDescriptors[i] = new MethodDescriptorImpl(
methods[i].getName(),
declaringType,
methods[i].getModifiers(),
toTypeDescriptor( methods[i].getReturnType() ),
argumentTypes
);
@ -293,6 +306,11 @@ public class RepositoryImpl implements Repository {
return name;
}
@Override
public int getModifiers() {
return Void.class.getModifiers();
}
@Override
public boolean isInterface() {
return false;

View File

@ -43,6 +43,8 @@ public interface FieldDescriptor {
*/
public TypeDescriptor getType();
public int getModifiers();
/**
* The declaring type
*

View File

@ -31,6 +31,9 @@ package org.hibernate.metamodel.reflite.spi;
public interface MethodDescriptor {
public String getName();
public TypeDescriptor getDeclaringType();
public int getModifiers();
public TypeDescriptor getReturnType();
public TypeDescriptor[] getParameterTypes();
}

View File

@ -31,6 +31,8 @@ package org.hibernate.metamodel.reflite.spi;
public interface TypeDescriptor {
public Name getName();
public int getModifiers();
/**
* Is this type an interface (as opposed to a class)?
*