Skip to content

Commit

Permalink
#18 - Add MemberDetails#getDeclaringType()
Browse files Browse the repository at this point in the history
  • Loading branch information
sebersole committed Jan 22, 2024
1 parent 78e7a45 commit d37c032
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@
public class DynamicFieldDetails extends AbstractAnnotationTarget implements FieldDetails, MutableMemberDetails {
private final String name;
private final ClassDetails type;
private final ClassDetails declaringType;
private final int modifierFlags;

public DynamicFieldDetails(
String name,
ClassDetails type,
ClassDetails declaringType,
int modifierFlags,
SourceModelBuildingContext buildingContext) {
super( buildingContext );
this.name = name;
this.type = type;
this.declaringType = declaringType;
this.modifierFlags = modifierFlags;
}

Expand All @@ -40,6 +43,11 @@ public ClassDetails getType() {
return type;
}

@Override
public ClassDetails getDeclaringType() {
return declaringType;
}

@Override
public int getModifiers() {
return modifierFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class DynamicMethodDetails extends AbstractAnnotationTarget implements MethodDetails, MutableMemberDetails {
private final String name;
private final ClassDetails type;
private final ClassDetails declaringType;
private final MethodKind methodKind;
private final int modifierFlags;

Expand All @@ -29,6 +30,7 @@ public class DynamicMethodDetails extends AbstractAnnotationTarget implements Me
public DynamicMethodDetails(
String name,
ClassDetails type,
ClassDetails declaringType,
MethodKind methodKind,
int modifierFlags,
ClassDetails returnType,
Expand All @@ -37,6 +39,7 @@ public DynamicMethodDetails(
super( buildingContext );
this.name = name;
this.type = type;
this.declaringType = declaringType;
this.methodKind = methodKind;
this.modifierFlags = modifierFlags;
this.returnType = returnType;
Expand All @@ -58,6 +61,11 @@ public ClassDetails getType() {
return type;
}

@Override
public ClassDetails getDeclaringType() {
return declaringType;
}

@Override
public int getModifiers() {
return modifierFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,19 @@ public class MapModeFieldDetails extends AbstractAnnotationTarget implements Fie
private final String name;
private final ClassDetails type;
private final int modifierFlags;
private final ClassDetails declaringType;

public MapModeFieldDetails(String name, ClassDetails type, int modifierFlags, SourceModelBuildingContext buildingContext) {
public MapModeFieldDetails(
String name,
ClassDetails type,
int modifierFlags,
ClassDetails declaringType,
SourceModelBuildingContext buildingContext) {
super( buildingContext );
this.name = name;
this.type = type;
this.modifierFlags = modifierFlags;
this.declaringType = declaringType;
}

@Override
Expand All @@ -38,6 +45,11 @@ public ClassDetails getType() {
return type;
}

@Override
public ClassDetails getDeclaringType() {
return declaringType;
}

@Override
public int getModifiers() {
return modifierFlags;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ public static Class<?> resolvePrimitiveClass(String className) {

public static JandexMethodDetails buildMethodDetails(
MethodInfo method,
ClassDetails declaringType,
SourceModelBuildingContext buildingContext) {

if ( method.parametersCount() == 0 ) {
// could be a getter
final Type returnType = method.returnType();
Expand All @@ -135,6 +135,7 @@ public static JandexMethodDetails buildMethodDetails(
method,
MethodDetails.MethodKind.GETTER,
buildingContext.getClassDetailsRegistry().resolveClassDetails( returnType.name().toString() ),
declaringType,
buildingContext
);
}
Expand All @@ -145,6 +146,7 @@ else if ( isBoolean( returnType ) && ( methodName.startsWith( "is" )
method,
MethodDetails.MethodKind.GETTER,
buildingContext.getClassDetailsRegistry().resolveClassDetails( returnType.name().toString() ),
declaringType,
buildingContext
);
}
Expand All @@ -158,6 +160,7 @@ else if ( isBoolean( returnType ) && ( methodName.startsWith( "is" )
method,
MethodDetails.MethodKind.SETTER,
buildingContext.getClassDetailsRegistry().resolveClassDetails( method.parameterType( 0 ).name().toString() ),
declaringType,
buildingContext
);
}
Expand All @@ -166,6 +169,7 @@ else if ( isBoolean( returnType ) && ( methodName.startsWith( "is" )
method,
MethodDetails.MethodKind.OTHER,
null,
declaringType,
buildingContext
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private List<FieldDetails> resolveFields() {
final List<FieldInfo> fieldsInfoList = classInfo.fields();
final List<FieldDetails> result = new ArrayList<>( fieldsInfoList.size() );
for ( FieldInfo fieldInfo : fieldsInfoList ) {
result.add( new JandexFieldDetails( fieldInfo, getBuildingContext() ) );
result.add( new JandexFieldDetails( fieldInfo, this, getBuildingContext() ) );
}
return result;
}
Expand All @@ -153,7 +153,7 @@ private List<RecordComponentDetails> resolveRecordComponents() {
final List<RecordComponentInfo> componentInfoList = classInfo.recordComponents();
final List<RecordComponentDetails> result = CollectionHelper.arrayList( componentInfoList.size() );
for ( RecordComponentInfo componentInfo : componentInfoList ) {
result.add( new JandexRecordComponentDetails( componentInfo, getBuildingContext() ) );
result.add( new JandexRecordComponentDetails( componentInfo, this, getBuildingContext() ) );
}
return result;
}
Expand All @@ -173,7 +173,7 @@ private List<MethodDetails> resolveMethods() {
if ( methodInfo.isConstructor() || "<clinit>".equals( methodInfo.name() ) ) {
continue;
}
result.add( JandexBuilders.buildMethodDetails( methodInfo, getBuildingContext() ) );
result.add( JandexBuilders.buildMethodDetails( methodInfo, this, getBuildingContext() ) );
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
import org.jboss.jandex.AnnotationTarget;
import org.jboss.jandex.FieldInfo;

import static org.hibernate.models.internal.ModifierUtils.isPersistableField;

/**
* @author Steve Ebersole
*/
public class JandexFieldDetails extends AbstractAnnotationTarget implements FieldDetails, MutableMemberDetails {
private final FieldInfo fieldInfo;
private final ClassDetails type;
private final ClassDetails declaringType;

public JandexFieldDetails(
FieldInfo fieldInfo,
ClassDetails declaringType,
SourceModelBuildingContext buildingContext) {
super( buildingContext );
this.fieldInfo = fieldInfo;
this.declaringType = declaringType;
this.type = buildingContext.getClassDetailsRegistry().resolveClassDetails( fieldInfo.type().name().toString() );
}

Expand All @@ -46,6 +47,11 @@ public ClassDetails getType() {
return type;
}

@Override
public ClassDetails getDeclaringType() {
return declaringType;
}

@Override
public int getModifiers() {
return fieldInfo.flags();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class JandexMethodDetails extends AbstractAnnotationTarget implements Met
private final MethodInfo methodInfo;
private final MethodKind methodKind;
private final ClassDetails type;
private final ClassDetails declaringType;

private final ClassDetails returnType;
private final List<ClassDetails> argumentTypes;
Expand All @@ -34,11 +35,13 @@ public JandexMethodDetails(
MethodInfo methodInfo,
MethodKind methodKind,
ClassDetails type,
ClassDetails declaringType,
SourceModelBuildingContext buildingContext) {
super( buildingContext );
this.methodInfo = methodInfo;
this.methodKind = methodKind;
this.type = type;
this.declaringType = declaringType;

final ClassDetailsRegistry classDetailsRegistry = buildingContext.getClassDetailsRegistry();
this.returnType = classDetailsRegistry.resolveClassDetails( methodInfo.returnType().name().toString() );
Expand Down Expand Up @@ -69,6 +72,11 @@ public ClassDetails getType() {
return type;
}

@Override
public ClassDetails getDeclaringType() {
return declaringType;
}

@Override
public int getModifiers() {
return methodInfo.flags();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@
public class JandexRecordComponentDetails extends AbstractAnnotationTarget implements RecordComponentDetails, MutableMemberDetails {
private final RecordComponentInfo recordComponentInfo;
private final ClassDetails type;
private final ClassDetails declaringType;

public JandexRecordComponentDetails(
RecordComponentInfo recordComponentInfo,
ClassDetails declaringType,
SourceModelBuildingContext buildingContext) {
super( buildingContext );
this.recordComponentInfo = recordComponentInfo;
this.declaringType = declaringType;
this.type = buildingContext.getClassDetailsRegistry().resolveClassDetails( recordComponentInfo.type().name().toString() );
}

Expand All @@ -44,6 +47,11 @@ public ClassDetails getType() {
return type;
}

@Override
public ClassDetails getDeclaringType() {
return declaringType;
}

@Override
public int getModifiers() {
return recordComponentInfo.accessor().flags();
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/org/hibernate/models/internal/jdk/JdkBuilders.java
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,21 @@ public static JdkClassDetails buildClassDetailsStatic(Class<?> javaClass, Source
return new JdkClassDetails( javaClass, buildingContext );
}

public static JdkMethodDetails buildMethodDetails(Method method, SourceModelBuildingContext buildingContext) {
public static JdkMethodDetails buildMethodDetails(
Method method,
ClassDetails declaringType,
SourceModelBuildingContext buildingContext) {
if ( method.getParameterCount() == 0 ) {
// could be a getter
final Class<?> returnType = method.getReturnType();
if ( !isVoid( returnType )
&& !ModifierUtils.isStatic( method.getModifiers() ) ) {
final String methodName = method.getName();
if ( methodName.startsWith( "get" ) ) {
return buildGetterDetails( method, returnType, buildingContext );
return buildGetterDetails( method, returnType, declaringType, buildingContext );
}
else if ( isBoolean( returnType ) && methodName.startsWith( "is" ) ) {
return buildGetterDetails( method, returnType, buildingContext );
return buildGetterDetails( method, returnType, declaringType, buildingContext );
}
}
}
Expand All @@ -158,34 +161,38 @@ else if ( isBoolean( returnType ) && methodName.startsWith( "is" ) ) {
&& isVoid( method.getReturnType() )
&& !ModifierUtils.isStatic( method.getModifiers() )
&& method.getName().startsWith( "set" ) ) {
return buildSetterDetails( method, method.getParameterTypes()[0], buildingContext );
return buildSetterDetails( method, method.getParameterTypes()[0], declaringType, buildingContext );
}

return new JdkMethodDetails( method, MethodDetails.MethodKind.OTHER, null, buildingContext );
return new JdkMethodDetails( method, MethodDetails.MethodKind.OTHER, null, declaringType, buildingContext );
}

public static JdkMethodDetails buildGetterDetails(
Method method,
Class<?> type,
ClassDetails declaringType,
SourceModelBuildingContext buildingContext) {
assert type != null;
return new JdkMethodDetails(
method,
MethodDetails.MethodKind.GETTER,
buildingContext.getClassDetailsRegistry().resolveClassDetails( type.getName() ),
declaringType,
buildingContext
);
}

public static JdkMethodDetails buildSetterDetails(
Method method,
Class<?> type,
ClassDetails declaringType,
SourceModelBuildingContext buildingContext) {
assert type != null;
return new JdkMethodDetails(
method,
MethodDetails.MethodKind.SETTER,
buildingContext.getClassDetailsRegistry().resolveClassDetails( type.getName() ),
declaringType,
buildingContext
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public List<FieldDetails> getFields() {
this.fields = CollectionHelper.arrayList( reflectionFields.length );
for ( int i = 0; i < reflectionFields.length; i++ ) {
final Field reflectionField = reflectionFields[i];
fields.add( new JdkFieldDetails( reflectionField, getBuildingContext() ) );
fields.add( new JdkFieldDetails( reflectionField, this, getBuildingContext() ) );
}
}
return fields;
Expand All @@ -162,7 +162,7 @@ public List<MethodDetails> getMethods() {
final Method[] reflectionMethods = managedClass.getDeclaredMethods();
this.methods = CollectionHelper.arrayList( reflectionMethods.length );
for ( int i = 0; i < reflectionMethods.length; i++ ) {
this.methods.add( buildMethodDetails( reflectionMethods[i], getBuildingContext() ) );
this.methods.add( buildMethodDetails( reflectionMethods[i], this, getBuildingContext() ) );
}
}
return methods;
Expand All @@ -182,7 +182,7 @@ public List<RecordComponentDetails> getRecordComponents() {
final RecordComponent[] jdkRecordComponents = managedClass.getRecordComponents();
recordComponents = CollectionHelper.arrayList( jdkRecordComponents.length );
for ( int i = 0; i < jdkRecordComponents.length; i++ ) {
recordComponents.add( new JdkRecordComponentDetails( jdkRecordComponents[i], getBuildingContext() ) );
recordComponents.add( new JdkRecordComponentDetails( jdkRecordComponents[i], this, getBuildingContext() ) );
}
}
return recordComponents;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
*/
public class JdkFieldDetails extends AbstractAnnotationTarget implements FieldDetails, MutableMemberDetails {
private final Field field;
private final JdkClassDetails declaringType;
private final ClassDetails type;

public JdkFieldDetails(Field field, SourceModelBuildingContext buildingContext) {
public JdkFieldDetails(Field field, JdkClassDetails declaringType, SourceModelBuildingContext buildingContext) {
super( field::getAnnotations, buildingContext );
this.field = field;
this.declaringType = declaringType;
this.type = buildingContext.getClassDetailsRegistry().resolveClassDetails(
field.getType().getName(),
(n) -> JdkBuilders.buildClassDetailsStatic( field.getType(), getBuildingContext() )
Expand All @@ -40,6 +42,11 @@ public ClassDetails getType() {
return type;
}

@Override
public ClassDetails getDeclaringType() {
return declaringType;
}

@Override
public int getModifiers() {
return field.getModifiers();
Expand Down
Loading

0 comments on commit d37c032

Please sign in to comment.