1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package art;
18 
19 import java.lang.ref.*;
20 import java.lang.reflect.*;
21 import java.lang.invoke.*;
22 import java.util.*;
23 
24 public class Test1983 {
runNonCts()25   public static void runNonCts() throws Exception {
26     Redefinition.setTestConfiguration(Redefinition.Config.COMMON_REDEFINE);
27     doTest();
28     doTestNonCts();
29   }
run()30   public static void run() throws Exception {
31     Redefinition.setTestConfiguration(Redefinition.Config.COMMON_REDEFINE);
32     doTest();
33   }
34 
Check(Class[] klasses)35   public static void Check(Class[] klasses) {
36     for (Class k : klasses) {
37       try {
38         boolean res = Redefinition.isStructurallyModifiable(k);
39         System.out.println("Is Structurally modifiable " + k + " " + res);
40       } catch (Exception e) {
41         System.out.println("Got exception " + e + " during check modifiablity of " + k);
42         e.printStackTrace(System.out);
43       }
44     }
45   }
46 
47   public static class WithVirtuals {
48     public Object o;
foobar()49     public void foobar() {}
50   }
51   public static class NoVirtuals extends WithVirtuals {
52     public static Object o;
foo()53     public static void foo() {}
54   }
55   public static class SubWithVirtuals extends NoVirtuals {
56     public Object j;
bar()57     public void bar() {}
58   }
59 
doTest()60   public static void doTest() throws Exception {
61     Class[] mirrord_classes = new Class[] {
62       AccessibleObject.class,
63       CallSite.class,
64       // ClassExt is not on the compile classpath.
65       Class.forName("dalvik.system.ClassExt"),
66       ClassLoader.class,
67       Class.class,
68       Constructor.class,
69       // DexCache is not on the compile classpath
70       Class.forName("java.lang.DexCache"),
71       // EmulatedStackFrame is not on the compile classpath
72       Class.forName("dalvik.system.EmulatedStackFrame"),
73       Executable.class,
74       Field.class,
75       // @hide on CTS
76       Class.forName("java.lang.ref.FinalizerReference"),
77       MethodHandle.class,
78       MethodHandles.Lookup.class,
79       MethodType.class,
80       Method.class,
81       Object.class,
82       Proxy.class,
83       Reference.class,
84       StackTraceElement.class,
85       String.class,
86       Thread.class,
87       Throwable.class,
88       // @hide on CTS
89       Class.forName("java.lang.invoke.VarHandle"),
90       // TODO all the var handle types.
91       // @hide on CTS
92       Class.forName("java.lang.invoke.FieldVarHandle"),
93     };
94     System.out.println("Checking mirror'd classes");
95     Check(mirrord_classes);
96     // The results of some of these will change as we improve structural class redefinition. Any
97     // that are true should always remain so though.
98     Class[] non_mirrord_classes = new Class[] {
99       new Object[0].getClass(),
100       NoVirtuals.class,
101       WithVirtuals.class,
102       SubWithVirtuals.class,
103     };
104     System.out.println("Checking non-mirror'd classes");
105     Check(non_mirrord_classes);
106   }
107 
doTestNonCts()108   public static void doTestNonCts() throws Exception {
109     System.out.println("Checking non-mirror'd classes (non-cts)");
110     Class[] non_mirrord_classes = new Class[] {
111       ArrayList.class,
112       Objects.class,
113       Arrays.class,
114       Integer.class,
115       Number.class,
116       MethodHandles.class,
117     };
118     Check(non_mirrord_classes);
119   }
120 }
121