Search in sources :

Example 1 with PsiReferenceExpressionImpl

use of com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl in project smali by JesusFreke.

the class SmaliCodeFragmentFactoryTest method assertVariableType.

private void assertVariableType(PsiElement context, String variableName, String expectedType) {
    SmaliCodeFragmentFactory codeFragmentFactory = new SmaliCodeFragmentFactory();
    JavaCodeFragment fragment = codeFragmentFactory.createCodeFragment(new TextWithImportsImpl(CodeFragmentKind.EXPRESSION, variableName), context, getProject());
    Editor editor = createEditor(fragment.getVirtualFile());
    editor.getCaretModel().moveToOffset(1);
    PsiElement element = fragment.findElementAt(0);
    Assert.assertTrue(element.getParent() instanceof PsiReferenceExpressionImpl);
    PsiReferenceExpressionImpl reference = (PsiReferenceExpressionImpl) element.getParent();
    Assert.assertEquals(expectedType, reference.getType().getCanonicalText());
}
Also used : SmaliCodeFragmentFactory(org.jf.smalidea.debugging.SmaliCodeFragmentFactory) TextWithImportsImpl(com.intellij.debugger.engine.evaluation.TextWithImportsImpl) Editor(com.intellij.openapi.editor.Editor) JavaCodeFragment(com.intellij.psi.JavaCodeFragment) PsiReferenceExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PsiReferenceExpressionImpl

use of com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl in project intellij-community by JetBrains.

the class HighlightUtil method checkReferenceToOurInstanceInsideThisOrSuper.

@Nullable
private static HighlightInfo checkReferenceToOurInstanceInsideThisOrSuper(@NotNull final PsiElement expression, @NotNull PsiClass referencedClass, final String resolvedName, @NotNull PsiFile containingFile) {
    if (PsiTreeUtil.getParentOfType(expression, PsiReferenceParameterList.class) != null)
        return null;
    PsiElement element = expression.getParent();
    while (element != null) {
        // check if expression inside super()/this() call
        if (RefactoringChangeUtil.isSuperOrThisMethodCall(element)) {
            PsiElement parentClass = new PsiMatcherImpl(element).parent(PsiMatchers.hasClass(PsiExpressionStatement.class)).parent(PsiMatchers.hasClass(PsiCodeBlock.class)).parent(PsiMatchers.hasClass(PsiMethod.class)).dot(JavaMatchers.isConstructor(true)).parent(PsiMatchers.hasClass(PsiClass.class)).getElement();
            if (parentClass == null) {
                return null;
            }
            // only this class/superclasses instance methods are not allowed to call
            PsiClass aClass = (PsiClass) parentClass;
            if (PsiUtil.isInnerClass(aClass) && referencedClass == aClass.getContainingClass())
                return null;
            // field or method should be declared in this class or super
            if (!InheritanceUtil.isInheritorOrSelf(aClass, referencedClass, true))
                return null;
            // and point to our instance
            if (expression instanceof PsiReferenceExpression && !thisOrSuperReference(((PsiReferenceExpression) expression).getQualifierExpression(), aClass)) {
                return null;
            }
            if (expression instanceof PsiJavaCodeReferenceElement && !aClass.equals(PsiTreeUtil.getParentOfType(expression, PsiClass.class)) && PsiTreeUtil.getParentOfType(expression, PsiTypeElement.class) != null) {
                return null;
            }
            if (expression instanceof PsiJavaCodeReferenceElement && PsiTreeUtil.getParentOfType(expression, PsiClassObjectAccessExpression.class) != null) {
                return null;
            }
            final HighlightInfo highlightInfo = createMemberReferencedError(resolvedName, expression.getTextRange());
            if (expression instanceof PsiReferenceExpression && PsiUtil.isInnerClass(aClass)) {
                final String referenceName = ((PsiReferenceExpression) expression).getReferenceName();
                final PsiClass containingClass = aClass.getContainingClass();
                LOG.assertTrue(containingClass != null);
                final PsiField fieldInContainingClass = containingClass.findFieldByName(referenceName, true);
                if (fieldInContainingClass != null && ((PsiReferenceExpression) expression).getQualifierExpression() == null) {
                    QuickFixAction.registerQuickFixAction(highlightInfo, new QualifyWithThisFix(containingClass, expression));
                }
            }
            return highlightInfo;
        }
        if (element instanceof PsiReferenceExpression) {
            final PsiElement resolve;
            if (element instanceof PsiReferenceExpressionImpl) {
                PsiReferenceExpressionImpl referenceExpression = (PsiReferenceExpressionImpl) element;
                JavaResolveResult[] results = JavaResolveUtil.resolveWithContainingFile(referenceExpression, PsiReferenceExpressionImpl.OurGenericsResolver.INSTANCE, true, false, containingFile);
                resolve = results.length == 1 ? results[0].getElement() : null;
            } else {
                resolve = ((PsiReferenceExpression) element).resolve();
            }
            if (resolve instanceof PsiField && ((PsiField) resolve).hasModifierProperty(PsiModifier.STATIC)) {
                return null;
            }
        }
        element = element.getParent();
        if (element instanceof PsiClass && InheritanceUtil.isInheritorOrSelf((PsiClass) element, referencedClass, true))
            return null;
    }
    return null;
}
Also used : HighlightInfo(com.intellij.codeInsight.daemon.impl.HighlightInfo) PsiReferenceExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl)

Example 3 with PsiReferenceExpressionImpl

use of com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl in project android by JetBrains.

the class PsiCFGDebugUtil method debugOutputPsiReferenceExpression.

private static void debugOutputPsiReferenceExpression(StringBuilder sb, PsiReferenceExpression psiElement) {
    sb.append("PsiReferenceExpression: " + psiElement.hashCode() + "\n");
    PsiElement e = psiElement.resolve();
    sb.append("ResolveType: " + e.getClass().getSimpleName() + " " + e.hashCode() + "\n");
    PsiExpression expr = psiElement.getQualifierExpression();
    if (expr == null) {
        sb.append("Qualifier Expression: " + "null \n");
    } else {
        sb.append("Qualifier Expression: " + expr.getClass().getSimpleName() + "\n");
        sb.append("Qualifier Expression Text: " + expr.getText() + "\n");
    }
    JavaResolveResult[] results = psiElement.multiResolve(false);
    sb.append("MultiResolveResults: " + results.length + "\n");
    for (JavaResolveResult curRes : results) {
        sb.append("Result: " + curRes.getElement().getClass().getSimpleName() + "\t" + curRes.getElement().getText() + "\n");
    }
    sb.append("\n");
    if (psiElement instanceof PsiReferenceExpressionImpl) {
        PsiReferenceExpressionImpl refImpl = (PsiReferenceExpressionImpl) psiElement;
    }
}
Also used : PsiReferenceExpressionImpl(com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl)

Aggregations

PsiReferenceExpressionImpl (com.intellij.psi.impl.source.tree.java.PsiReferenceExpressionImpl)3 HighlightInfo (com.intellij.codeInsight.daemon.impl.HighlightInfo)1 TextWithImportsImpl (com.intellij.debugger.engine.evaluation.TextWithImportsImpl)1 Editor (com.intellij.openapi.editor.Editor)1 JavaCodeFragment (com.intellij.psi.JavaCodeFragment)1 PsiElement (com.intellij.psi.PsiElement)1 SmaliCodeFragmentFactory (org.jf.smalidea.debugging.SmaliCodeFragmentFactory)1