1 /*
2  * Copyright 2014, Google Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 package org.jf.smalidea;
33 
34 import com.intellij.openapi.projectRoots.Sdk;
35 import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl;
36 import com.intellij.psi.PsiMethod;
37 import com.intellij.psi.PsiReference;
38 import com.intellij.testFramework.ResolveTestCase;
39 import org.jf.smalidea.psi.impl.SmaliMethodReference;
40 import org.junit.Assert;
41 
42 public class MethodReferenceTest extends ResolveTestCase {
43     /**
44      * Test a reference to a java method from a smali class
45      */
testJavaReferenceFromSmali()46     public void testJavaReferenceFromSmali() throws Exception {
47         String text =
48                 ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" +
49                         ".method public blah()V\n" +
50                         "    .locals 1\n" +
51 
52                         "    invoke-static {}, Ljava/lang/System;->nano<ref>Time()J\n" +
53                         "    return-void\n" +
54                         ".end method";
55 
56         SmaliMethodReference methodReference = (SmaliMethodReference)configureByFileText(text, "blah.smali");
57 
58         Assert.assertNotNull(methodReference);
59         Assert.assertEquals("nanoTime", methodReference.getName());
60 
61         PsiMethod resolvedMethod = (PsiMethod)methodReference.resolve();
62         Assert.assertNotNull(resolvedMethod);
63         Assert.assertEquals("nanoTime", resolvedMethod.getName());
64         Assert.assertNotNull(resolvedMethod.getContainingClass());
65         Assert.assertEquals("java.lang.System", resolvedMethod.getContainingClass().getQualifiedName());
66         Assert.assertEquals(0, resolvedMethod.getParameterList().getParametersCount());
67         Assert.assertNotNull(resolvedMethod.getReturnType());
68         Assert.assertEquals("long", resolvedMethod.getReturnType().getCanonicalText());
69     }
70 
71     /**
72      * Test a reference to a smali method from a smali class
73      */
testSmaliReferenceFromSmali()74     public void testSmaliReferenceFromSmali() throws Exception {
75         createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;" +
76                 ".method public static blort(ILjava/lang/String;)V\n" +
77                 "    .locals 0\n" +
78                 "    return-void\n" +
79                 ".end method\n");
80 
81         String text =
82                 ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" +
83                         ".method public blah2()V\n" +
84                         "    .locals 0\n" +
85                         "    invoke-static {}, Lblarg;->bl<ref>ort(ILjava/lang/String;)V\n" +
86                         "    return-void\n" +
87                         ".end method";
88 
89         SmaliMethodReference methodReference = (SmaliMethodReference)configureByFileText(text, "blah.smali");
90 
91         Assert.assertNotNull(methodReference);
92         Assert.assertEquals("blort", methodReference.getName());
93 
94         PsiMethod resolvedMethod = (PsiMethod)methodReference.resolve();
95         Assert.assertNotNull(resolvedMethod);
96         Assert.assertEquals("blort", resolvedMethod.getName());
97         Assert.assertNotNull(resolvedMethod.getContainingClass());
98         Assert.assertEquals("blarg", resolvedMethod.getContainingClass().getQualifiedName());
99         Assert.assertEquals(2, resolvedMethod.getParameterList().getParametersCount());
100         Assert.assertEquals("int", resolvedMethod.getParameterList().getParameters()[0].getType().getCanonicalText());
101         Assert.assertEquals("java.lang.String",
102                 resolvedMethod.getParameterList().getParameters()[1].getType().getCanonicalText());
103         Assert.assertNotNull(resolvedMethod.getReturnType());
104         Assert.assertEquals("void", resolvedMethod.getReturnType().getCanonicalText());
105     }
106 
107     /**
108      * Test a reference to a smali method from a java class
109      */
testSmaliReferenceFromJava()110     public void testSmaliReferenceFromJava() throws Exception {
111         createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;" +
112                 ".method public static blort(ILjava/lang/String;)V\n" +
113                 "    .locals 0\n" +
114                 "    return-void\n" +
115                 ".end method\n");
116 
117 
118         String text = "public class blah { public static void something() {" +
119                         "blarg.bl<ref>ort(10, \"bob\");" +
120                         "}}";
121 
122         PsiReference methodReference = configureByFileText(text, "blah.java");
123 
124         Assert.assertNotNull(methodReference);
125 
126         PsiMethod resolvedMethod = (PsiMethod)methodReference.resolve();
127         Assert.assertNotNull(resolvedMethod);
128         Assert.assertEquals("blort", resolvedMethod.getName());
129         Assert.assertNotNull(resolvedMethod.getContainingClass());
130         Assert.assertEquals("blarg", resolvedMethod.getContainingClass().getQualifiedName());
131         Assert.assertEquals(2, resolvedMethod.getParameterList().getParametersCount());
132         Assert.assertEquals("int", resolvedMethod.getParameterList().getParameters()[0].getType().getCanonicalText());
133         Assert.assertEquals("java.lang.String",
134                 resolvedMethod.getParameterList().getParameters()[1].getType().getCanonicalText());
135         Assert.assertNotNull(resolvedMethod.getReturnType());
136         Assert.assertEquals("void", resolvedMethod.getReturnType().getCanonicalText());
137     }
138 
139     @Override
getTestProjectJdk()140     protected Sdk getTestProjectJdk() {
141         return JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
142     }
143 }
144