Skip to content

Instantly share code, notes, and snippets.

@strongant
Created August 28, 2024 02:08
Show Gist options
  • Save strongant/c9f8f34bc7d1d1bf00dcb3798eb6b7fb to your computer and use it in GitHub Desktop.
Save strongant/c9f8f34bc7d1d1bf00dcb3798eb6b7fb to your computer and use it in GitHub Desktop.
classgraph get class info
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();
// ...
}
}
}
@strongant
Copy link
Author

try (ScanResult scanResult = new ClassGraph().enableAllInfo()
        .acceptPackages(packageName).scan()) {
    ClassInfo classInfo = scanResult.getClassInfo(className);
    MethodInfoList methodInfoList = classInfo.getMethodInfo(methodName);
    MethodInfo methodInfo = methodInfoList.get(0);  // First method named {methodName}
    MethodTypeSignature methodTypeSignature = methodInfo
            .getTypeSignatureOrTypeDescriptor();
    TypeSignature resultTypeSignature = methodTypeSignature.getResultType();
    if (resultTypeSignature instanceof ArrayTypeSignature) {
        ArrayTypeSignature arrayTypeSignature =
                (ArrayTypeSignature) resultTypeSignature;
        System.out.println("Method " + methodInfo.getName() + " returns a "
                + arrayTypeSignature.getNumDimensions() + "-dimensional array of "
                + arrayTypeSignature.getElementTypeSignature());
    } else if (resultTypeSignature instanceof BaseTypeSignature) {
        BaseTypeSignature baseTypeSignature =
                (BaseTypeSignature) resultTypeSignature;
        System.out.println("Method " + methodInfo.getName() + " returns "
                + baseTypeSignature.getTypeStr());
    } else if (resultTypeSignature instanceof ClassRefTypeSignature) {
        ClassRefTypeSignature classRefTypeSignature =
                (ClassRefTypeSignature) resultTypeSignature;
        System.out.println("Method " + methodInfo.getName() + " returns "
                + classRefTypeSignature.getClassInfo().getName());
    } else if (resultTypeSignature instanceof TypeVariableSignature) {
        TypeVariableSignature typeVariableSignature =
                (TypeVariableSignature) resultTypeSignature;
        // Attempt to resolve type variable
        TypeParameter typeParameter = typeVariableSignature.resolve();
        System.out.println("Method " + methodInfo.getName()
                + " returns type variable " + typeVariableSignature.getName()
                + ", which resolves to " + typeParameter);
    }
}

@strongant
Copy link
Author

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