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 @@
*/ */
public class ArrayTypeDescriptorImpl implements ArrayTypeDescriptor { public class ArrayTypeDescriptorImpl implements ArrayTypeDescriptor {
private final Name name; private final Name name;
private final int modifiers;
private TypeDescriptor componentType; private TypeDescriptor componentType;
public ArrayTypeDescriptorImpl(Name name, TypeDescriptor componentType) { public ArrayTypeDescriptorImpl(Name name, int modifiers, TypeDescriptor componentType) {
this.name = name; this.name = name;
this.modifiers = modifiers;
this.componentType = componentType; this.componentType = componentType;
} }
@ -44,6 +46,11 @@ public Name getName() {
return name; return name;
} }
@Override
public int getModifiers() {
return modifiers;
}
@Override @Override
public TypeDescriptor getComponentTypeDescriptor() { public TypeDescriptor getComponentTypeDescriptor() {
return componentType; return componentType;

View File

@ -40,13 +40,15 @@ public class ClassDescriptorImpl implements ClassDescriptor {
private TypeDescriptor superType; private TypeDescriptor superType;
private TypeDescriptor[] interfaces; private TypeDescriptor[] interfaces;
private final int modifiers;
private final boolean hasDefaultConstructor; private final boolean hasDefaultConstructor;
private FieldDescriptor[] fieldDescriptors; private FieldDescriptor[] fieldDescriptors;
private MethodDescriptor[] methodDescriptors; private MethodDescriptor[] methodDescriptors;
public ClassDescriptorImpl(Name name, boolean hasDefaultConstructor) { public ClassDescriptorImpl(Name name, int modifiers, boolean hasDefaultConstructor) {
this.name = name; this.name = name;
this.modifiers = modifiers;
this.hasDefaultConstructor = hasDefaultConstructor; this.hasDefaultConstructor = hasDefaultConstructor;
} }
@ -55,6 +57,11 @@ public Name getName() {
return name; return name;
} }
@Override
public int getModifiers() {
return modifiers;
}
@Override @Override
public boolean isInterface() { public boolean isInterface() {
return false; return false;
@ -115,4 +122,9 @@ void setFields(FieldDescriptor[] fieldDescriptors) {
void setMethods(MethodDescriptor[] methodDescriptors) { void setMethods(MethodDescriptor[] methodDescriptors) {
this.methodDescriptors = 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 String name;
private final TypeDescriptor fieldType; private final TypeDescriptor fieldType;
private final int modifiers;
private final TypeDescriptor declaringType; 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.name = name;
this.fieldType = fieldType; this.fieldType = fieldType;
this.modifiers = modifiers;
this.declaringType = declaringType; this.declaringType = declaringType;
} }
@ -51,8 +54,18 @@ public TypeDescriptor getType() {
return fieldType; return fieldType;
} }
@Override
public int getModifiers() {
return modifiers;
}
@Override @Override
public TypeDescriptor getDeclaringType() { public TypeDescriptor getDeclaringType() {
return declaringType; return declaringType;
} }
@Override
public String toString() {
return "FieldDescriptorImpl{" + declaringType.getName().toString() + '#' + name + '}';
}
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -31,6 +31,9 @@
public interface MethodDescriptor { public interface MethodDescriptor {
public String getName(); public String getName();
public TypeDescriptor getDeclaringType(); public TypeDescriptor getDeclaringType();
public int getModifiers();
public TypeDescriptor getReturnType(); public TypeDescriptor getReturnType();
public TypeDescriptor[] getParameterTypes(); public TypeDescriptor[] getParameterTypes();
} }

View File

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