com.intellij.psi.impl.source.PsiClassReferenceType Java Examples

The following examples show how to use com.intellij.psi.impl.source.PsiClassReferenceType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CamelDocumentationProvider.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
private boolean isPsiMethodCamelLanguage(PsiMethod method) {
    PsiType type = method.getReturnType();
    if (type != null && type instanceof PsiClassReferenceType) {
        PsiClassReferenceType clazz = (PsiClassReferenceType) type;
        PsiClass resolved = clazz.resolve();
        if (resolved != null) {
            boolean language = getCamelIdeaUtils().isCamelExpressionOrLanguage(resolved);
            // try parent using some weird/nasty stub stuff which is how complex IDEA AST
            // is when its parsing the Camel route builder
            if (!language) {
                PsiElement elem = resolved.getParent();
                if (elem instanceof PsiTypeParameterList) {
                    elem = elem.getParent();
                }
                if (elem instanceof PsiClass) {
                    language = getCamelIdeaUtils().isCamelExpressionOrLanguage((PsiClass) elem);
                }
            }
            return language;
        }
    }

    return false;
}
 
Example #2
Source File: InflateViewAnnotator.java    From idea-android-studio-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Nullable
public static InflateContainer matchInflate(PsiLocalVariable psiLocalVariable) {
    PsiType psiType = psiLocalVariable.getType();
    if(psiType instanceof PsiClassReferenceType) {
        PsiMethodCallExpression psiMethodCallExpression = PsiTreeUtil.findChildOfType(psiLocalVariable, PsiMethodCallExpression.class);
        if(psiMethodCallExpression != null) {
            PsiMethod psiMethod = psiMethodCallExpression.resolveMethod();

            // @TODO: replace "inflate"; resolve method and check nethod calls
            if(psiMethod != null && psiMethod.getName().equals("inflate")) {
                PsiExpression[] expressions = psiMethodCallExpression.getArgumentList().getExpressions();
                if(expressions.length > 0 && expressions[0] instanceof PsiReferenceExpression) {
                    PsiFile xmlFile = AndroidUtils.findXmlResource((PsiReferenceExpression) expressions[0]);
                    if(xmlFile != null) {
                        return new InflateContainer(xmlFile, ((PsiLocalVariable) psiLocalVariable));
                    }
                }
            }
        }
    }

    return null;
}
 
Example #3
Source File: GenericHelper.java    From java2typescript with Apache License 2.0 6 votes vote down vote up
private static String processDiamondOperator(PsiElement element, TranslationContext ctx) {
    String result;
    // Java 7 diamond operator not possible in TypeScript
    if (element instanceof PsiField) {
        PsiField field = (PsiField) element;
        result = process((PsiClassReferenceType) field.getType(), ctx, true);
    } else if (element instanceof PsiLocalVariable) {
        System.out.println("DIAMOND LOCALVAR> " + ((PsiLocalVariable) element).getType());
        PsiLocalVariable localVar = (PsiLocalVariable) element;
        result = process((PsiClassReferenceType) localVar.getType(), ctx, true);
    } else if (element instanceof PsiAssignmentExpression) {
        PsiAssignmentExpression assign = (PsiAssignmentExpression) element;
        result = process((PsiClassReferenceType) assign.getLExpression().getType(), ctx, true);
    } else {
        System.out.println("DIAMOND > "+element);
        result = "<any>";
    }
    return result;
}
 
Example #4
Source File: BuilderInfo.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public Optional<PsiType> getObtainViaFieldVariableType() {
  PsiVariable psiVariable = variableInClass;

  if (StringUtil.isNotEmpty(viaFieldName)) {
    final PsiField fieldByName = getPsiClass().findFieldByName(viaFieldName, false);
    if (fieldByName != null) {
      psiVariable = fieldByName;
    }
  }

  final PsiType psiVariableType = psiVariable.getType();

  if (psiVariableType instanceof PsiClassReferenceType) {
    final PsiClass resolvedPsiVariableClass = ((PsiClassReferenceType) psiVariableType).resolve();
    if (resolvedPsiVariableClass instanceof PsiTypeParameter) {
      return Optional.of(psiVariableType);
    }
  }
  return Optional.empty();
}
 
Example #5
Source File: SuggestionServiceImpl.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
private PsiClass findClass(final PsiType type) {
    if (PsiClass.class.isInstance(type)) {
        return PsiClass.class.cast(type);
    }
    if (PsiClassReferenceType.class.isInstance(type)) {
        return PsiClassReferenceType.class.cast(type).resolve();
    }
    return null;
}
 
Example #6
Source File: JavaClassUtils.java    From camel-idea-plugin with Apache License 2.0 5 votes vote down vote up
public PsiClass resolveClassReference(@NotNull PsiReference reference) {
    final PsiElement resolveElement = reference.resolve();

    if (resolveElement instanceof PsiClass) {
        return (PsiClass) resolveElement;
    } else if (resolveElement instanceof PsiField) {
        final PsiType psiType = PsiUtil.getTypeByPsiElement(resolveElement);
        if (psiType != null) {
            return ((PsiClassReferenceType) psiType).resolve();
        }
    }

    return null;
}
 
Example #7
Source File: FieldTranslator.java    From java2typescript with Apache License 2.0 5 votes vote down vote up
private static void translateClassField(PsiField element, TranslationContext ctx, DocMeta docMeta) {
    PsiModifierList modifierList = element.getModifierList();
    if (modifierList != null && modifierList.hasModifierProperty("private")) {
        ctx.print("private ");
    } else {
        ctx.print("public ");
    }
    if (modifierList != null && modifierList.hasModifierProperty("static")) {
        ctx.append("static ");
    }
    ctx.append(element.getName()).append(": ");

    if (element.hasInitializer() && (element.getInitializer() instanceof PsiLambdaExpression)) {
        if (element.getType() instanceof PsiClassReferenceType) {
            try {
                MethodTranslator.translateToLambdaType(((PsiClassReferenceType) element.getType()).rawType().resolve().getMethods()[0], ctx, docMeta);
            } catch (Exception e){
                ((PsiClassReferenceType) element.getType()).rawType()
                        .resolve();
                e.printStackTrace();
            }
        } else {
            System.err.println("FieldTranslator:: Type not instance of PsiClassReferenceType. Could not translate lambda expression. (" + element.getType().getClass().getName() + ")");
        }

    } else {
        ctx.append(TypeHelper.printType(element.getType(), ctx));
    }

    if (element.hasInitializer()) {
        ctx.append(" = ");
        ExpressionTranslator.translate(element.getInitializer(), ctx);
        ctx.append(";\n");
    } else {
        ctx.append(";\n");
    }
}
 
Example #8
Source File: DelombokHandler.java    From lombok-intellij-plugin with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@NotNull
private String getTypeWithParameter(@NotNull PsiClassType psiClassType) {
  if (psiClassType instanceof PsiClassReferenceType) {
    return ((PsiClassReferenceType) psiClassType).getReference().getText();
  }
  return psiClassType.getName();
}
 
Example #9
Source File: EnumerationSerializerFactory.java    From android-parcelable-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public TypeSerializer getSerializer(PsiType psiType) {
    if (psiType instanceof PsiClassReferenceType && ((PsiClassReferenceType) psiType).resolve().isEnum()) {
        return mSerializer;
    }

    return null;
}
 
Example #10
Source File: GenericListSerializer.java    From android-parcelable-intellij-plugin with Apache License 2.0 5 votes vote down vote up
@NotNull
private String getGenericType(SerializableValue field) {
    String genericType = "";
    try {
        PsiType[] parameters = ((PsiClassReferenceType) field.getType()).getParameters();
        if (parameters.length > 0) {
            genericType = parameters[0].getCanonicalText();
        }
    } catch (Exception ignored) {
    }
    return genericType;
}
 
Example #11
Source File: AbstractMethodFragment.java    From IntelliJDeodorant with MIT License 4 votes vote down vote up
void processMethodInvocations(List<PsiExpression> methodInvocations) {
    for (PsiExpression expression : methodInvocations) {
        if (expression instanceof PsiMethodCallExpression) {
            PsiMethodCallExpression methodInvocation = (PsiMethodCallExpression) expression;
            PsiMethod resolveMethod = methodInvocation.resolveMethod();
            String originClassName = "";

            if (resolveMethod != null && resolveMethod.getContainingClass() != null) {
                originClassName = resolveMethod.getContainingClass().getQualifiedName();
            }

            if (resolveMethod == null || methodInvocation.getMethodExpression().getQualifierExpression() != null) {
                PsiReferenceExpression qualifierExpression = getFirstQualifierInAChain(methodInvocation);

                if (qualifierExpression == null) {
                    PsiMethod resolvedMethod = resolveMethod(methodInvocation);
                    if (resolvedMethod != null && resolvedMethod.getContainingClass() != null) {
                        boolean isStatic = resolvedMethod.hasModifierProperty(PsiModifier.STATIC);

                        if (originClassName == null || originClassName.equals("")) {
                            originClassName = resolvedMethod.getContainingClass().getQualifiedName();
                        }

                        processMethodInvocation(methodInvocation, originClassName, isStatic);
                    }
                } else {
                    if (originClassName == null || originClassName.equals("")) {
                        PsiElement resolvedElement = qualifierExpression.resolve();
                        if (resolvedElement instanceof PsiVariable) {
                            PsiType resolvedQualifierType = ((PsiVariable) resolvedElement).getType();

                            if (resolvedQualifierType instanceof PsiClassReferenceType) {
                                PsiClass resolvedClass = ((PsiClassReferenceType) resolvedQualifierType).resolve();
                                if (resolvedClass != null) {
                                    originClassName = resolvedClass.getQualifiedName();
                                }
                            }
                        }
                    }

                    if (originClassName != null && !originClassName.equals("")) {
                        processMethodInvocation(methodInvocation, originClassName, false);
                    }
                }
            } else {
                boolean isMethodStatic = resolveMethod.hasModifierProperty(PsiModifier.STATIC);
                if (resolveMethod.getContainingClass() != null) {
                    if (originClassName == null || originClassName.equals("")) {
                        originClassName = resolveMethod.getContainingClass().getQualifiedName();
                    }
                }
                processMethodInvocation(methodInvocation, originClassName, isMethodStatic);
            }
        }
    }
}
 
Example #12
Source File: GenericHelper.java    From java2typescript with Apache License 2.0 4 votes vote down vote up
public static String process(PsiClassReferenceType classRefType, TranslationContext ctx, boolean withGenerics) {
    String result = "";
    PsiJavaCodeReferenceElement ref = classRefType.getReference();
    if (ref.getText().endsWith("<>")) {
        result = processDiamondOperator(ref.getParent().getParent(), ctx);
    } else {
        ArrayList<String> genericParameterNames = ctx.getGenericParameterNames();
        if (classRefType.getParameterCount() > 0) {
            String[] generics = new String[classRefType.getParameterCount()];
            PsiType[] genericTypes = classRefType.getParameters();
            for (int i = 0; i < genericTypes.length; i++) {
                if (genericTypes[i] instanceof PsiWildcardType) {
                    if (((PsiWildcardType) genericTypes[i]).getBound() != null) {
                        if (classRefType.getReference().getParent().getParent() instanceof PsiParameter) {
                            PsiParameter param = (PsiParameter) classRefType.getReference().getParent().getParent();
                            PsiParameterList paramList = (PsiParameterList) param.getParent();
                            generics[i] = genericParameterNames.get(paramList.getParameterIndex(param));
                        }
                    } else {
                        generics[i] = "any";
                    }
                } else {
                    generics[i] = TypeHelper.printType(genericTypes[i], ctx, true, false);
                }
            }
            result += "<" + String.join(", ", generics) + ">";
        } else if (withGenerics) {
            PsiClass clazz = classRefType.resolve();
            if (clazz != null) {
                if (clazz.getTypeParameters().length > 0) {
                    String[] genericParams = new String[clazz.getTypeParameters().length];
                    for (int i=0; i < clazz.getTypeParameters().length; i++) {
                        genericParams[i] = "any";
                    }
                    result = "<" + String.join(", ", genericParams) + ">";
                }
            } else {
                if (ref.getText().endsWith("<>")) {
                    result = processDiamondOperator(ref.getParent().getParent(), ctx);
                }
            }
        } else {
            if (classRefType.getPresentableText().contains("<")) {
                result += "<any>";
            }
        }
    }

    return result;
}
 
Example #13
Source File: TypeHelper.java    From java2typescript with Apache License 2.0 4 votes vote down vote up
public static String printType(PsiType element, TranslationContext ctx, boolean withGenericParams, boolean avoidNativeOptim) {
    String result = element.getPresentableText();

    if (result.equals("Throwable") || result.endsWith("Exception")) {
        return "Error";
    }
    if (result.equals("Pattern")) {
        return "RegExp";
    }
    if (objects.contains(result) || classes.contains(result)) {
        return "any";
    } else if (primitiveNumbers.contains(result) || objectNumbers.contains(result)) {
        return "number";
    } else if (strings.contains(result)) {
        return "string";
    } else if (booleans.contains(result)) {
        return "boolean";
    }
    /*
    if (ctx.NATIVE_ARRAY && !avoidNativeOptim && element.getArrayDimensions() == 1) {
        return printArrayBaseType(element);
    }*/
    if (element instanceof PsiPrimitiveType) {
        if (result.equals("null")) {
            System.err.println("TypeHelper::printType -> Result null with elem:" + element.toString());
        }
        return result;
    } else if (element instanceof PsiArrayType) {
        PsiArrayType typedElement = (PsiArrayType) element;
        String partialResult = printArrayBaseType(typedElement);
        if (partialResult != null) {
            result = "";
            for (int i = 1; i < typedElement.getArrayDimensions(); i++) {
                result += "Array<";
            }
            result += partialResult;
            for (int i = 1; i < typedElement.getArrayDimensions(); i++) {
                result += ">";
            }
        } else {
            partialResult = printType(typedElement.getComponentType(), ctx, withGenericParams, avoidNativeOptim);
            if (withGenericParams) {
                result = partialResult + "[]";
            } else {
                result = partialResult;
            }
        }
        return result;
    } else if (element instanceof PsiClassReferenceType) {
        PsiClassReferenceType elementClassRefType = ((PsiClassReferenceType) element);
        PsiClass resolvedClass = elementClassRefType.resolve();

        if (resolvedClass != null) {
            if (resolvedClass.getQualifiedName() == null) {
                result = resolvedClass.getName();
            } else {
                result = resolvedClass.getQualifiedName();
                result += GenericHelper.process(elementClassRefType, ctx, withGenericParams);
            }
        } else {
            if (((PsiClassReferenceType) element).getClassName().startsWith("Class")) {
                // "Class" concept does not exists in TypeScript => any
                result = "any";
            } else {
                String tryJavaUtil = javaTypes.get(elementClassRefType.getClassName());
                if (tryJavaUtil != null) {
                    ctx.needsJava(tryJavaUtil);
                    result = tryJavaUtil;
                } else {
                    result = elementClassRefType.getReference().getQualifiedName();
                }
                result += GenericHelper.process(elementClassRefType, ctx, withGenericParams);
            }
        }
    } else if (element instanceof PsiWildcardType) {
        PsiType bound = ((PsiWildcardType) element).getBound();
        if (bound != null) {
            result = "T";
        } else {
            result = "any";
        }
    } else {
        System.out.println("TypeHelper: unhandled type -> " + element);
    }

    if (result.equals("null")) {
        // this is kind of desperate but well...
        result = element.getPresentableText();
    }

    return ctx.packageTransform(result);
}