Skip to content
The Chat Archive
claude-opus-4-20250514· Anthropic LMArena Human Preference 140K

how to get the class info in java generics in idea plugin dev

  1. User

    how to get the class info in java generics in idea plugin dev

  2. Assistant

    In IntelliJ IDEA plugin development, getting class information from Java generics can be tricky due to type erasure. Here are the main approaches:

    Using PSI (Program Structure Interface)

    When working with PSI elements in your plugin, you can access generic type information through several methods:

    For Generic Classes/Interfaces

    // Get a PsiClass element
    PsiClass psiClass = ...;
    
    // Get type parameters
    PsiTypeParameter[] typeParameters = psiClass.getTypeParameters();
    
    // Get the generic signature
    if (psiClass instanceof PsiClassImpl) {
        PsiClassStub stub = ((PsiClassImpl) psiClass).getStub();
        if (stub != null) {
            String signature = stub.getQualifiedName();
        }
    }
    

    For Generic Types in Code

    // Working with PsiType
    PsiType psiType = ...;
    
    // Check if it's a generic type
    if (psiType instanceof PsiClassType) {
        PsiClassType classType = (PsiClassType) psiType;
        
        // Get the raw type
        PsiClass resolvedClass = classType.resolve();
        
        // Get type arguments
        PsiType[] typeArguments = classType.getParameters();
        
        // Get substitution map
        PsiSubstitutor substitutor = classType.resolveGenerics().getSubstitutor();
    }
    

    For Method/Field Generic Information

    // For a method
    PsiMethod method = ...;
    PsiTypeParameter[] methodTypeParams = method.getTypeParameters();
    PsiType returnType = method.getReturnType();
    
    // For a field
    PsiField field = ...;
    PsiType fieldType = field.getType();
    
    // Resolve generic types
    if (fieldType instanceof PsiClassType) {
        PsiClassType.ClassResolveResult resolveResult = 
            ((PsiClassType) fieldType).resolveGenerics();
        PsiClass resolvedClass = resolveResult.getElement();
        PsiSubstitutor substitutor = resolveResult.getSubstitutor();
    }
    

    Working with Type Resolution

    For more complex scenarios involving type resolution:

    // Get the type of an expression
    PsiExpression expression = ...;
    PsiType expressionType = expression.getType();
    
    // Resolve with context
    JavaResolveResult[] results = expression.multiResolve(false);
    for (JavaResolveResult result : results) {
        PsiElement element = result.getElement();
        PsiSubstitutor substitutor = result.getSubstitutor();
        
        // Apply substitution to get actual types
        if (element instanceof PsiMethod) {
            PsiMethod method = (PsiMethod) element;
            PsiType actualReturnType = substitutor.substitute(method.getReturnType());
        }
    }
    

    Utility Methods

    IntelliJ provides helpful utilities:

    // Using TypeConversionUtil
    import com.intellij.psi.util.TypeConversionUtil;
    
    PsiType type1 = ...;
    PsiType type2 = ...;
    boolean isAssignable = TypeConversionUtil.isAssignable(type1, type2);
    
    // Using PsiUtil
    import com.intellij.psi.util.PsiUtil;
    
    PsiClass containingClass = PsiUtil.getContainingClass(element);
    PsiType[] superTypes = PsiUtil.getSuperTypes(psiClass);
    

    Example: Getting Generic Info from a Variable Declaration

    public void analyzeGenericVariable(PsiLocalVariable variable) {
        PsiType type = variable.getType();
        
        if (type instanceof PsiClassType) {
            PsiClassType classType = (PsiClassType) type;
            PsiClass psiClass = classType.resolve();
            
            if (psiClass != null) {
                // Get the raw class name
                String className = psiClass.getQualifiedName();
                
                // Get type arguments (e.g., String in List<String>)
                PsiType[] typeArgs = classType.getParameters();
                for (int i = 0; i < typeArgs.length; i++) {
                    PsiType argType = typeArgs[i];
                    System.out.println("Type argument " + i + ": " + 
                        argType.getCanonicalText());
                }
            }
        }
    }
    

    Remember that when working with generics in plugin development, you're analyzing source code structure rather than runtime types, so you have access to full generic information that would normally be erased at runtime.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.