From 50ff90b3b45db6774411b2b3de750527cdcf1db0 Mon Sep 17 00:00:00 2001 From: Steve Ebersole Date: Thu, 13 Feb 2014 07:17:03 -0600 Subject: [PATCH] HHH-8962 - Create a set of light reflection classes - modifier support --- .../internal/ArrayTypeDescriptorImpl.java | 9 +- .../reflite/internal/ClassDescriptorImpl.java | 14 ++- .../reflite/internal/FieldDescriptorImpl.java | 15 ++- .../internal/InterfaceDescriptorImpl.java | 14 ++- .../internal/MethodDescriptorImpl.java | 13 +++ .../reflite/internal/Primitives.java | 109 ++++++++++++------ .../reflite/internal/RepositoryImpl.java | 30 ++++- .../reflite/spi/FieldDescriptor.java | 2 + .../reflite/spi/MethodDescriptor.java | 3 + .../metamodel/reflite/spi/TypeDescriptor.java | 2 + 10 files changed, 166 insertions(+), 45 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/ArrayTypeDescriptorImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/ArrayTypeDescriptorImpl.java index f9f5f7a476..f3e890f89b 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/ArrayTypeDescriptorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/ArrayTypeDescriptorImpl.java @@ -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; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/ClassDescriptorImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/ClassDescriptorImpl.java index dad25f1b47..095c3ed61f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/ClassDescriptorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/ClassDescriptorImpl.java @@ -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() + '}'; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/FieldDescriptorImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/FieldDescriptorImpl.java index f69aa5409a..58e6efde72 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/FieldDescriptorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/FieldDescriptorImpl.java @@ -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 + '}'; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/InterfaceDescriptorImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/InterfaceDescriptorImpl.java index 057ca4e599..db61063220 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/InterfaceDescriptorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/InterfaceDescriptorImpl.java @@ -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() + '}'; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/MethodDescriptorImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/MethodDescriptorImpl.java index 6ef4240a1b..fd632934b9 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/MethodDescriptorImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/MethodDescriptorImpl.java @@ -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 + '}'; + } } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/Primitives.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/Primitives.java index cf107133d3..09f7e972b1 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/Primitives.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/Primitives.java @@ -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; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/RepositoryImpl.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/RepositoryImpl.java index cf554a2724..7ea2c4fceb 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/RepositoryImpl.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/internal/RepositoryImpl.java @@ -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; diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/FieldDescriptor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/FieldDescriptor.java index 488108d2de..e8329a697f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/FieldDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/FieldDescriptor.java @@ -43,6 +43,8 @@ public interface FieldDescriptor { */ public TypeDescriptor getType(); + public int getModifiers(); + /** * The declaring type * diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/MethodDescriptor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/MethodDescriptor.java index f5a092a792..5464a4f1e2 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/MethodDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/MethodDescriptor.java @@ -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(); } diff --git a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/TypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/TypeDescriptor.java index 148e24aa1b..3f8de4fc7f 100644 --- a/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/TypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/metamodel/reflite/spi/TypeDescriptor.java @@ -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)? *