Search in sources :

Example 6 with LightClassReference

use of com.intellij.psi.impl.light.LightClassReference in project intellij-community by JetBrains.

the class PsiImplUtil method buildTypeFromTypeString.

public static PsiType buildTypeFromTypeString(@NotNull final String typeName, @NotNull final PsiElement context, @NotNull final PsiFile psiFile) {
    final PsiManager psiManager = psiFile.getManager();
    if (typeName.indexOf('<') != -1 || typeName.indexOf('[') != -1 || typeName.indexOf('.') == -1) {
        try {
            return JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory().createTypeFromText(typeName, context);
        }// invalid syntax will produce unresolved class type
         catch (Exception ignored) {
        }
    }
    PsiClass aClass = JavaPsiFacade.getInstance(psiManager.getProject()).findClass(typeName, context.getResolveScope());
    PsiType resultType;
    if (aClass == null) {
        final LightClassReference ref = new LightClassReference(psiManager, PsiNameHelper.getShortClassName(typeName), typeName, PsiSubstitutor.EMPTY, psiFile);
        resultType = new PsiClassReferenceType(ref, null);
    } else {
        PsiElementFactory factory = JavaPsiFacade.getInstance(psiManager.getProject()).getElementFactory();
        PsiSubstitutor substitutor = factory.createRawSubstitutor(aClass);
        resultType = factory.createType(aClass, substitutor);
    }
    return resultType;
}
Also used : LightClassReference(com.intellij.psi.impl.light.LightClassReference) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiClassReferenceType(com.intellij.psi.impl.source.PsiClassReferenceType)

Aggregations

LightClassReference (com.intellij.psi.impl.light.LightClassReference)6 NotNull (org.jetbrains.annotations.NotNull)3 PsiClassReferenceType (com.intellij.psi.impl.source.PsiClassReferenceType)2 GroovyResolveResult (org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult)2 PsiClass (com.intellij.psi.PsiClass)1 PsiClassType (com.intellij.psi.PsiClassType)1 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)1 PsiManagerEx (com.intellij.psi.impl.PsiManagerEx)1 PsiImmediateClassType (com.intellij.psi.impl.source.PsiImmediateClassType)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 Nullable (org.jetbrains.annotations.Nullable)1