• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.baksmali;
33 
34 import org.antlr.runtime.RecognitionException;
35 import org.junit.Test;
36 
37 import java.io.IOException;
38 
39 public class ImplicitReferenceTest {
40     @Test
testImplicitMethodReferences()41     public void testImplicitMethodReferences() throws IOException, RecognitionException {
42         String source = "" +
43                 ".class public LHelloWorld;\n" +
44                 ".super Ljava/lang/Object;\n" +
45                 ".method public static main([Ljava/lang/String;)V\n" +
46                 "    .registers 1\n" +
47                 "    invoke-static {p0}, LHelloWorld;->toString()V\n" +
48                 "    invoke-static {p0}, LHelloWorld;->V()V\n" +
49                 "    invoke-static {p0}, LHelloWorld;->I()V\n" +
50                 "    return-void\n" +
51                 ".end method";
52 
53         String expected = "" +
54                 ".class public LHelloWorld;\n" +
55                 ".super Ljava/lang/Object;\n" +
56                 "# direct methods\n" +
57                 ".method public static main([Ljava/lang/String;)V\n" +
58                 ".registers 1\n" +
59                 "invoke-static {p0}, toString()V\n" +
60                 "invoke-static {p0}, V()V\n" +
61                 "invoke-static {p0}, I()V\n" +
62                 "return-void\n" +
63                 ".end method\n";
64 
65         baksmaliOptions options = new baksmaliOptions();
66         options.useImplicitReferences = true;
67 
68         BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
69     }
70 
71     @Test
testExplicitMethodReferences()72     public void testExplicitMethodReferences() throws IOException, RecognitionException {
73         String source = "" +
74                 ".class public LHelloWorld;\n" +
75                 ".super Ljava/lang/Object;\n" +
76                 ".method public static main([Ljava/lang/String;)V\n" +
77                 "    .registers 1\n" +
78                 "    invoke-static {p0}, LHelloWorld;->toString()V\n" +
79                 "    invoke-static {p0}, LHelloWorld;->V()V\n" +
80                 "    invoke-static {p0}, LHelloWorld;->I()V\n" +
81                 "    return-void\n" +
82                 ".end method";
83 
84         String expected = "" +
85                 ".class public LHelloWorld;\n" +
86                 ".super Ljava/lang/Object;\n" +
87                 "# direct methods\n" +
88                 ".method public static main([Ljava/lang/String;)V\n" +
89                 "    .registers 1\n" +
90                 "    invoke-static {p0}, LHelloWorld;->toString()V\n" +
91                 "    invoke-static {p0}, LHelloWorld;->V()V\n" +
92                 "    invoke-static {p0}, LHelloWorld;->I()V\n" +
93                 "    return-void\n" +
94                 ".end method\n";
95 
96         baksmaliOptions options = new baksmaliOptions();
97         options.useImplicitReferences = false;
98 
99         BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
100     }
101 
102     @Test
testImplicitMethodLiterals()103     public void testImplicitMethodLiterals() throws IOException, RecognitionException {
104         String source = "" +
105                 ".class public LHelloWorld;\n" +
106                 ".super Ljava/lang/Object;\n" +
107                 ".field public static field1:Ljava/lang/reflect/Method; = LHelloWorld;->toString()V\n" +
108                 ".field public static field2:Ljava/lang/reflect/Method; = LHelloWorld;->V()V\n" +
109                 ".field public static field3:Ljava/lang/reflect/Method; = LHelloWorld;->I()V\n" +
110                 ".field public static field4:Ljava/lang/Class; = I";
111 
112         String expected = "" +
113                 ".class public LHelloWorld;\n" +
114                 ".super Ljava/lang/Object;\n" +
115                 "# static fields\n" +
116                 ".field public static field1:Ljava/lang/reflect/Method; = toString()V\n" +
117                 ".field public static field2:Ljava/lang/reflect/Method; = V()V\n" +
118                 ".field public static field3:Ljava/lang/reflect/Method; = I()V\n" +
119                 ".field public static field4:Ljava/lang/Class; = I\n";
120 
121         baksmaliOptions options = new baksmaliOptions();
122         options.useImplicitReferences = true;
123 
124         BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
125     }
126 
127     @Test
testExplicitMethodLiterals()128     public void testExplicitMethodLiterals() throws IOException, RecognitionException {
129         String source = "" +
130                 ".class public LHelloWorld;\n" +
131                 ".super Ljava/lang/Object;\n" +
132                 ".field public static field1:Ljava/lang/reflect/Method; = LHelloWorld;->toString()V\n" +
133                 ".field public static field2:Ljava/lang/reflect/Method; = LHelloWorld;->V()V\n" +
134                 ".field public static field3:Ljava/lang/reflect/Method; = LHelloWorld;->I()V\n" +
135                 ".field public static field4:Ljava/lang/Class; = I";
136 
137         String expected = "" +
138                 ".class public LHelloWorld;\n" +
139                 ".super Ljava/lang/Object;\n" +
140                 "# static fields\n" +
141                 ".field public static field1:Ljava/lang/reflect/Method; = LHelloWorld;->toString()V\n" +
142                 ".field public static field2:Ljava/lang/reflect/Method; = LHelloWorld;->V()V\n" +
143                 ".field public static field3:Ljava/lang/reflect/Method; = LHelloWorld;->I()V\n" +
144                 ".field public static field4:Ljava/lang/Class; = I\n";
145 
146         baksmaliOptions options = new baksmaliOptions();
147         options.useImplicitReferences = false;
148 
149         BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
150     }
151 
152     @Test
testImplicitFieldReferences()153     public void testImplicitFieldReferences() throws IOException, RecognitionException {
154         String source = "" +
155                 ".class public LHelloWorld;\n" +
156                 ".super Ljava/lang/Object;\n" +
157                 ".method public static main([Ljava/lang/String;)V\n" +
158                 "    .registers 1\n" +
159                 "    sget v0, LHelloWorld;->someField:I\n" +
160                 "    sget v0, LHelloWorld;->I:I\n" +
161                 "    sget v0, LHelloWorld;->V:I\n" +
162                 "    return-void\n" +
163                 ".end method";
164 
165         String expected = "" +
166                 ".class public LHelloWorld;\n" +
167                 ".super Ljava/lang/Object;\n" +
168                 "# direct methods\n" +
169                 ".method public static main([Ljava/lang/String;)V\n" +
170                 "    .registers 1\n" +
171                 "    sget p0, someField:I\n" +
172                 "    sget p0, I:I\n" +
173                 "    sget p0, V:I\n" +
174                 "    return-void\n" +
175                 ".end method\n";
176 
177         baksmaliOptions options = new baksmaliOptions();
178         options.useImplicitReferences = true;
179 
180         BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
181     }
182 
183     @Test
testExplicitFieldReferences()184     public void testExplicitFieldReferences() throws IOException, RecognitionException {
185         String source = "" +
186                 ".class public LHelloWorld;\n" +
187                 ".super Ljava/lang/Object;\n" +
188                 ".method public static main([Ljava/lang/String;)V\n" +
189                 "    .registers 1\n" +
190                 "    sget v0, LHelloWorld;->someField:I\n" +
191                 "    sget v0, LHelloWorld;->I:I\n" +
192                 "    sget v0, LHelloWorld;->V:I\n" +
193                 "    return-void\n" +
194                 ".end method";
195 
196         String expected = "" +
197                 ".class public LHelloWorld;\n" +
198                 ".super Ljava/lang/Object;\n" +
199                 "# direct methods\n" +
200                 ".method public static main([Ljava/lang/String;)V\n" +
201                 "    .registers 1\n" +
202                 "    sget p0, LHelloWorld;->someField:I\n" +
203                 "    sget p0, LHelloWorld;->I:I\n" +
204                 "    sget p0, LHelloWorld;->V:I\n" +
205                 "    return-void\n" +
206                 ".end method\n";
207 
208         baksmaliOptions options = new baksmaliOptions();
209         options.useImplicitReferences = false;
210 
211         BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
212     }
213 
214     @Test
testImplicitFieldLiterals()215     public void testImplicitFieldLiterals() throws IOException, RecognitionException {
216         String source = "" +
217                 ".class public LHelloWorld;\n" +
218                 ".super Ljava/lang/Object;\n" +
219                 ".field public static field1:Ljava/lang/reflect/Field; = LHelloWorld;->someField:I\n" +
220                 ".field public static field2:Ljava/lang/reflect/Field; = LHelloWorld;->V:I\n" +
221                 ".field public static field3:Ljava/lang/reflect/Field; = LHelloWorld;->I:I";
222 
223         String expected = "" +
224                 ".class public LHelloWorld;\n" +
225                 ".super Ljava/lang/Object;\n" +
226                 "# static fields\n" +
227                 ".field public static field1:Ljava/lang/reflect/Field; = someField:I\n" +
228                 ".field public static field2:Ljava/lang/reflect/Field; = V:I\n" +
229                 ".field public static field3:Ljava/lang/reflect/Field; = I:I\n";
230 
231         baksmaliOptions options = new baksmaliOptions();
232         options.useImplicitReferences = true;
233 
234         BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
235     }
236 
237     @Test
testExplicitFieldLiterals()238     public void testExplicitFieldLiterals() throws IOException, RecognitionException {
239         String source = "" +
240                 ".class public LHelloWorld;\n" +
241                 ".super Ljava/lang/Object;\n" +
242                 ".field public static field1:Ljava/lang/reflect/Field; = LHelloWorld;->someField:I\n" +
243                 ".field public static field2:Ljava/lang/reflect/Field; = LHelloWorld;->V:I\n" +
244                 ".field public static field3:Ljava/lang/reflect/Field; = LHelloWorld;->I:I";
245 
246         String expected = "" +
247                 ".class public LHelloWorld;\n" +
248                 ".super Ljava/lang/Object;\n" +
249                 "# static fields\n" +
250                 ".field public static field1:Ljava/lang/reflect/Field; = LHelloWorld;->someField:I\n" +
251                 ".field public static field2:Ljava/lang/reflect/Field; = LHelloWorld;->V:I\n" +
252                 ".field public static field3:Ljava/lang/reflect/Field; = LHelloWorld;->I:I\n";
253 
254         baksmaliOptions options = new baksmaliOptions();
255         options.useImplicitReferences = false;
256 
257         BaksmaliTestUtils.assertSmaliCompiledEquals(source, expected, options);
258     }
259 
260 }
261