1 /*
2  * Copyright 2015, 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.PsiField;
37 import com.intellij.psi.PsiReference;
38 import com.intellij.testFramework.ResolveTestCase;
39 import org.jf.smalidea.psi.impl.SmaliFieldReference;
40 import org.junit.Assert;
41 
42 public class FieldReferenceTest extends ResolveTestCase {
43     /**
44      * Test a reference to a java field 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                         "    sget-object v0, Ljava/lang/System;->o<ref>ut:Ljava/io/PrintStream;\n" +
52                         "    return-void\n" +
53                         ".end method";
54 
55         SmaliFieldReference fieldReference = (SmaliFieldReference)configureByFileText(text, "blah.smali");
56 
57         Assert.assertNotNull(fieldReference);
58         Assert.assertEquals("out", fieldReference.getName());
59         Assert.assertNotNull(fieldReference.getFieldType());
60         Assert.assertEquals("java.io.PrintStream", fieldReference.getFieldType().getType().getCanonicalText());
61 
62         PsiField resolvedField = fieldReference.resolve();
63         Assert.assertNotNull(resolvedField);
64         Assert.assertEquals("out", resolvedField.getName());
65         Assert.assertNotNull(resolvedField.getContainingClass());
66         Assert.assertEquals("java.lang.System", resolvedField.getContainingClass().getQualifiedName());
67         Assert.assertEquals("java.io.PrintStream", resolvedField.getType().getCanonicalText());
68     }
69 
70     /**
71      * Test a reference to a smali field from a smali class
72      */
testSmaliReferenceFromSmali()73     public void testSmaliReferenceFromSmali() throws Exception {
74         createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;" +
75                 ".field public static blort:I");
76 
77         String text =
78                 ".class public Lmy/pkg/blah; .super Ljava/lang/Object;\n" +
79                         ".method public blah()V\n" +
80                         "    .locals 1\n" +
81                         "    sget v0, Lblarg;->bl<ref>ort:I\n" +
82                         "    return-void\n" +
83                         ".end method";
84 
85         SmaliFieldReference fieldReference = (SmaliFieldReference)configureByFileText(text, "blah.smali");
86 
87         Assert.assertNotNull(fieldReference);
88         Assert.assertEquals("blort", fieldReference.getName());
89         Assert.assertNotNull(fieldReference.getFieldType());
90         Assert.assertEquals("int", fieldReference.getFieldType().getType().getCanonicalText());
91 
92         PsiField resolvedField = fieldReference.resolve();
93         Assert.assertNotNull(resolvedField);
94         Assert.assertEquals("blort", resolvedField.getName());
95         Assert.assertNotNull(resolvedField.getContainingClass());
96         Assert.assertEquals("blarg", resolvedField.getContainingClass().getQualifiedName());
97         Assert.assertEquals("int", resolvedField.getType().getCanonicalText());
98     }
99 
100     /**
101      * Test a reference to a smali field from a java class
102      */
testSmaliReferenceFromJava()103     public void testSmaliReferenceFromJava() throws Exception {
104         createFile("blarg.smali", ".class public Lblarg; .super Ljava/lang/Object;" +
105                 ".field public static blort:I");
106 
107         String text = "public class blah { public static void something() {" +
108                         "blarg.bl<ref>ort = 10;" +
109                         "}}";
110 
111         PsiReference fieldReference = configureByFileText(text, "blah.java");
112 
113         Assert.assertNotNull(fieldReference);
114 
115         PsiField resolvedField = (PsiField)fieldReference.resolve();
116         Assert.assertNotNull(resolvedField);
117         Assert.assertEquals("blort", resolvedField.getName());
118         Assert.assertNotNull(resolvedField.getContainingClass());
119         Assert.assertEquals("blarg", resolvedField.getContainingClass().getQualifiedName());
120         Assert.assertEquals("int", resolvedField.getType().getCanonicalText());
121     }
122 
123     @Override
getTestProjectJdk()124     protected Sdk getTestProjectJdk() {
125         return JavaAwareProjectJdkTableImpl.getInstanceEx().getInternalJdk();
126     }
127 }
128