Created
August 28, 2024 02:08
-
-
Save strongant/c9f8f34bc7d1d1bf00dcb3798eb6b7fb to your computer and use it in GitHub Desktop.
classgraph get class info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
try (ScanResult scanResult = new ClassGraph().enableAllInfo().acceptPackages("com.xyz") | |
.scan()) { | |
ClassInfo form = scanResult.getClassInfo("com.xyz.Form"); | |
if (form != null) { | |
MethodInfoList formMethods = form.getMethodInfo(); | |
for (MethodInfo mi : formMethods) { | |
String methodName = mi.getName(); | |
MethodParameterInfo[] mpi = mi.getParameterInfo(); | |
for (int i = 0; i < mpi.length; i++) { | |
String parameterName = mpi[i].getName(); | |
TypeSignature parameterType = | |
mpi[i].getTypeSignatureOrTypeDescriptor(); | |
// ... | |
} | |
} | |
FieldInfoList formFields = form.getFieldInfo(); | |
for (FieldInfo fi : formFields) { | |
String fieldName = fi.getName(); | |
TypeSignature fieldType = fi.getTypeSignatureOrTypeDescriptor(); | |
// ... | |
} | |
AnnotationInfoList formAnnotations = form.getAnnotationInfo(); | |
for (AnnotationInfo ai : formAnnotations) { | |
String annotationName = ai.getName(); | |
List<AnnotationParameterValue> annotationParamVals = | |
ai.getParameterValues(); | |
// ... | |
} | |
} | |
} |
Author
strongant
commented
Aug 28, 2024
public class TestReadingTypeArgs {
static class A<X> {
}
static abstract class B {
abstract A<Integer> a();
}
public static void main(String[] args) {
try (ScanResult scanResult = new ClassGraph()
.acceptPackages(TestReadingTypeArgs.class.getPackage().getName())
.enableAllInfo().scan()) {
ClassInfo bClass = scanResult.getClassInfo(B.class.getName());
MethodInfo aMethodInfo = bClass.getMethodInfo("a").get(0);
MethodTypeSignature aType = aMethodInfo.getTypeSignatureOrTypeDescriptor();
TypeSignature aResultType = aType.getResultType();
ClassRefTypeSignature aResultTypeConcrete = (ClassRefTypeSignature) aResultType;
String aTypeBaseClassName = aResultTypeConcrete.getBaseClassName();
List<TypeArgument> aTypeArgs = aResultTypeConcrete.getTypeArguments();
TypeArgument aTypeArg0 = aTypeArgs.get(0);
String aTypeArg0BaseClassName =
((ClassRefTypeSignature) aTypeArg0.getTypeSignature()).getBaseClassName();
System.out.println("Method a() returns type " + aTypeBaseClassName
+ " with argument " + aTypeArg0BaseClassName);
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment