mirror of
https://github.com/revanced/smali.git
synced 2025-05-03 08:04:28 +02:00
Add support for annotation element references
This adds support for both find usages and "go to definition" functionality on an annotation element.
This commit is contained in:
parent
44a0c9091f
commit
ddc7c35e1c
@ -31,12 +31,17 @@
|
|||||||
|
|
||||||
package org.jf.smalidea.psi.impl;
|
package org.jf.smalidea.psi.impl;
|
||||||
|
|
||||||
import com.intellij.psi.PsiIdentifier;
|
import com.intellij.openapi.util.TextRange;
|
||||||
|
import com.intellij.psi.*;
|
||||||
import com.intellij.psi.tree.IElementType;
|
import com.intellij.psi.tree.IElementType;
|
||||||
|
import com.intellij.util.ArrayUtil;
|
||||||
|
import com.intellij.util.IncorrectOperationException;
|
||||||
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
import org.jf.smalidea.psi.SmaliCompositeElementFactory;
|
import org.jf.smalidea.psi.SmaliCompositeElementFactory;
|
||||||
import org.jf.smalidea.psi.SmaliElementTypes;
|
import org.jf.smalidea.psi.SmaliElementTypes;
|
||||||
|
|
||||||
public class SmaliAnnotationElementName extends SmaliCompositeElement implements PsiIdentifier {
|
public class SmaliAnnotationElementName extends SmaliCompositeElement implements PsiIdentifier, PsiReference {
|
||||||
public static final SmaliCompositeElementFactory FACTORY = new SmaliCompositeElementFactory() {
|
public static final SmaliCompositeElementFactory FACTORY = new SmaliCompositeElementFactory() {
|
||||||
@Override public SmaliCompositeElement createElement() {
|
@Override public SmaliCompositeElement createElement() {
|
||||||
return new SmaliAnnotationElementName();
|
return new SmaliAnnotationElementName();
|
||||||
@ -54,4 +59,77 @@ public class SmaliAnnotationElementName extends SmaliCompositeElement implements
|
|||||||
@Override public String getName() {
|
@Override public String getName() {
|
||||||
return getText();
|
return getText();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public SmaliAnnotation getContainingAnnotation() {
|
||||||
|
return findAncestorByClass(SmaliAnnotation.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public PsiElement bindToElement(PsiElement element) throws IncorrectOperationException {
|
||||||
|
//TODO: implement this if needed
|
||||||
|
throw new IncorrectOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public PsiElement getElement() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public TextRange getRangeInElement() {
|
||||||
|
return new TextRange(0, getTextLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable @Override public PsiElement resolve() {
|
||||||
|
SmaliAnnotation smaliAnnotation = getContainingAnnotation();
|
||||||
|
if (smaliAnnotation == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
String annotationType = smaliAnnotation.getQualifiedName();
|
||||||
|
if (annotationType == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject());
|
||||||
|
PsiClass annotationClass = facade.findClass(annotationType, getResolveScope());
|
||||||
|
if (annotationClass == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (PsiMethod method : annotationClass.findMethodsByName(getName(), true)) {
|
||||||
|
if (method.getParameterList().getParametersCount() == 0) {
|
||||||
|
return method;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull @Override public String getCanonicalText() {
|
||||||
|
// TODO: return a full method reference here?
|
||||||
|
String name = getName();
|
||||||
|
if (name == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
|
||||||
|
//TODO: implement this
|
||||||
|
throw new IncorrectOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public boolean isReferenceTo(PsiElement element) {
|
||||||
|
return resolve() == element;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull @Override public Object[] getVariants() {
|
||||||
|
return ArrayUtil.EMPTY_OBJECT_ARRAY;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public boolean isSoft() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override public PsiReference getReference() {
|
||||||
|
return this;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,104 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2015, Google Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are
|
||||||
|
* met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above
|
||||||
|
* copyright notice, this list of conditions and the following disclaimer
|
||||||
|
* in the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* * Neither the name of Google Inc. nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jf.smalidea;
|
||||||
|
|
||||||
|
import com.intellij.psi.*;
|
||||||
|
import com.intellij.testFramework.ResolveTestCase;
|
||||||
|
import org.junit.Assert;
|
||||||
|
|
||||||
|
public class AnnotationElementNameReferenceTest extends ResolveTestCase {
|
||||||
|
public void testSmaliReferenceFromSmali() throws Exception {
|
||||||
|
createFile("AnnotationWithValues.smali", "" +
|
||||||
|
".class public abstract interface annotation LAnnotationWithValues;\n" +
|
||||||
|
".super Ljava/lang/Object;\n" +
|
||||||
|
".implements Ljava/lang/annotation/Annotation;\n" +
|
||||||
|
"\n" +
|
||||||
|
".method public abstract intValue()I\n" +
|
||||||
|
".end method");
|
||||||
|
|
||||||
|
PsiReference reference = configureByFileText("" +
|
||||||
|
".class public Lblah;\n" +
|
||||||
|
".super Ljava/lang/Object;\n" +
|
||||||
|
".annotation runtime LAnnotationWithValues;\n" +
|
||||||
|
" int<ref>Value = 123\n" +
|
||||||
|
".end annotation", "blah.smali");
|
||||||
|
|
||||||
|
PsiElement resolved = reference.resolve();
|
||||||
|
Assert.assertNotNull(resolved);
|
||||||
|
Assert.assertTrue(resolved instanceof PsiAnnotationMethod);
|
||||||
|
Assert.assertEquals("intValue", ((PsiAnnotationMethod)resolved).getName());
|
||||||
|
Assert.assertEquals("AnnotationWithValues",
|
||||||
|
((PsiAnnotationMethod)resolved).getContainingClass().getQualifiedName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJavaReferenceFromSmali() throws Exception {
|
||||||
|
createFile("AnnotationWithValues.java", "" +
|
||||||
|
"public @interface AnnotationWithValues {\n" +
|
||||||
|
" int intValue();\n" +
|
||||||
|
"}");
|
||||||
|
|
||||||
|
PsiReference reference = configureByFileText("" +
|
||||||
|
".class public Lblah;\n" +
|
||||||
|
".super Ljava/lang/Object;\n" +
|
||||||
|
".annotation runtime LAnnotationWithValues;\n" +
|
||||||
|
" int<ref>Value = 123\n" +
|
||||||
|
".end annotation", "blah.smali");
|
||||||
|
|
||||||
|
PsiElement resolved = reference.resolve();
|
||||||
|
Assert.assertNotNull(resolved);
|
||||||
|
Assert.assertTrue(resolved instanceof PsiAnnotationMethod);
|
||||||
|
Assert.assertEquals("intValue", ((PsiAnnotationMethod)resolved).getName());
|
||||||
|
Assert.assertEquals("AnnotationWithValues",
|
||||||
|
((PsiAnnotationMethod)resolved).getContainingClass().getQualifiedName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSmaliReferenceFromJava() throws Exception {
|
||||||
|
createFile("AnnotationWithValues.smali", "" +
|
||||||
|
".class public abstract interface annotation LAnnotationWithValues;\n" +
|
||||||
|
".super Ljava/lang/Object;\n" +
|
||||||
|
".implements Ljava/lang/annotation/Annotation;\n" +
|
||||||
|
"\n" +
|
||||||
|
".method public abstract intValue()I\n" +
|
||||||
|
".end method");
|
||||||
|
|
||||||
|
PsiReference reference = configureByFileText("" +
|
||||||
|
"@AnnotationWithValues(int<ref>Value=123)\n" +
|
||||||
|
"public class blah {}", "blah.java");
|
||||||
|
|
||||||
|
PsiElement resolved = reference.resolve();
|
||||||
|
Assert.assertNotNull(resolved);
|
||||||
|
Assert.assertTrue(resolved instanceof PsiAnnotationMethod);
|
||||||
|
Assert.assertEquals("intValue", ((PsiAnnotationMethod)resolved).getName());
|
||||||
|
Assert.assertEquals("AnnotationWithValues",
|
||||||
|
((PsiAnnotationMethod)resolved).getContainingClass().getQualifiedName());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2015, Google Inc.
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are
|
||||||
|
* met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above
|
||||||
|
* copyright notice, this list of conditions and the following disclaimer
|
||||||
|
* in the documentation and/or other materials provided with the
|
||||||
|
* distribution.
|
||||||
|
* * Neither the name of Google Inc. nor the names of its
|
||||||
|
* contributors may be used to endorse or promote products derived from
|
||||||
|
* this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.jf.smalidea.findUsages;
|
||||||
|
|
||||||
|
public class FindAnnotationElementUsagesTest extends FindUsagesTest {
|
||||||
|
public void testSmaliUsageInSmaliFile() throws Exception {
|
||||||
|
addFile("AnnotationWithValues.smali", "" +
|
||||||
|
".class public abstract interface annotation LAnnotationWithValues;\n" +
|
||||||
|
".super Ljava/lang/Object;\n" +
|
||||||
|
".implements Ljava/lang/annotation/Annotation;\n" +
|
||||||
|
"\n" +
|
||||||
|
".method public abstract intValue()I\n" +
|
||||||
|
".end method\n" +
|
||||||
|
".annotation system Ldalvik/annotation/AnnotationDefault;\n" +
|
||||||
|
" value = .subannotation LAnnotationWithValues;\n" +
|
||||||
|
" int<usage>Value = 4\n" +
|
||||||
|
" .end subannotation\n" +
|
||||||
|
".end annotation");
|
||||||
|
addFile("blarg.smali", "" +
|
||||||
|
".class public Lblah;\n" +
|
||||||
|
".super Ljava/lang/Object;\n" +
|
||||||
|
".annotation runtime LAnnotationWithValues;\n" +
|
||||||
|
" int<usage><ref>Value = 123\n" +
|
||||||
|
".end annotation");
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testJavaUsageInSmaliFile() throws Exception {
|
||||||
|
addFile("AnnotationWithValues.java", "" +
|
||||||
|
"\n" +
|
||||||
|
"public @interface AnnotationWithValues {\n" +
|
||||||
|
" int int<ref>Value() default 4;\n" +
|
||||||
|
"}");
|
||||||
|
addFile("blarg.smali", "" +
|
||||||
|
".class public Lblah;\n" +
|
||||||
|
".super Ljava/lang/Object;\n" +
|
||||||
|
".annotation runtime LAnnotationWithValues;\n" +
|
||||||
|
" int<usage>Value = 123\n" +
|
||||||
|
".end annotation");
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSmaliUsageInJavaFile() throws Exception {
|
||||||
|
addFile("AnnotationWithValues.smali", "" +
|
||||||
|
".class public abstract interface annotation LAnnotationWithValues;\n" +
|
||||||
|
".super Ljava/lang/Object;\n" +
|
||||||
|
".implements Ljava/lang/annotation/Annotation;\n" +
|
||||||
|
"\n" +
|
||||||
|
".method public abstract intValue()I\n" +
|
||||||
|
".end method\n" +
|
||||||
|
".annotation system Ldalvik/annotation/AnnotationDefault;\n" +
|
||||||
|
" value = .subannotation LAnnotationWithValues;\n" +
|
||||||
|
" int<usage><ref>Value = 4\n" +
|
||||||
|
" .end subannotation\n" +
|
||||||
|
".end annotation");
|
||||||
|
addFile("blarg.java", "" +
|
||||||
|
"\n" +
|
||||||
|
"@AnnotationWithValues(int<usage>Value=123)\n" +
|
||||||
|
"public class blarg {}");
|
||||||
|
doTest();
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user