1 /* |
|
2 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 * |
|
23 */ |
|
24 |
|
25 package com.sun.classanalyzer; |
|
26 |
|
27 import com.sun.classanalyzer.Klass.Method; |
|
28 |
|
29 /** |
|
30 * |
|
31 * @author mchung |
|
32 */ |
|
33 public class ResolutionInfo implements Comparable<ResolutionInfo> { |
|
34 |
|
35 enum Type { |
|
36 |
|
37 REFLECTION("reflection", true), |
|
38 NATIVE("native", true), |
|
39 INTERFACE("interface", false), |
|
40 SUPER("super", false), |
|
41 EXPLICIT("explicit", false), |
|
42 VERIFICATION("verification", false), |
|
43 METHODTRACE("method trace", true), |
|
44 CONSTANT_POOL("constant pool", true), |
|
45 CHECKED_EXCEPTION("throws", true), |
|
46 METHOD("method", true), |
|
47 FIELD("field", true), |
|
48 EXTENDS("extends", true), |
|
49 IMPLEMENTS("implements", true), |
|
50 NOINFO("No info", false); |
|
51 |
|
52 private final String name; |
|
53 private final boolean hasInfo; |
|
54 |
|
55 private Type(String name, boolean hasInfo) { |
|
56 this.name = name; |
|
57 this.hasInfo = hasInfo; |
|
58 } |
|
59 |
|
60 public String getName() { |
|
61 return name; |
|
62 } |
|
63 |
|
64 public boolean hasInfo() { |
|
65 return hasInfo; |
|
66 } |
|
67 |
|
68 public static Type getType(String s) { |
|
69 if (s.isEmpty()) { |
|
70 return NOINFO; |
|
71 } |
|
72 for (Type t : values()) { |
|
73 if (s.equals(t.name)) { |
|
74 return t; |
|
75 } |
|
76 } |
|
77 // Need to fix the VM output to add "native" |
|
78 // throw new IllegalArgumentException("Invalid ResolutionInfo.type \"" + s + "\""); |
|
79 System.out.println("WARNING: Invalid ResolutionInfo.type \"" + s + "\""); |
|
80 return null; |
|
81 } |
|
82 } |
|
83 final Klass fromClass; |
|
84 final Method method; |
|
85 final Klass toClass; |
|
86 final int linenumber; |
|
87 final Type type; |
|
88 final String info; |
|
89 private boolean isPublic = false; |
|
90 |
|
91 private ResolutionInfo(Klass from, Klass to, int linenumber, Type type, String info) { |
|
92 this.fromClass = from; |
|
93 this.method = null; |
|
94 this.toClass = to; |
|
95 this.linenumber = linenumber; |
|
96 this.type = type; |
|
97 this.info = info; |
|
98 } |
|
99 |
|
100 private ResolutionInfo(Klass from, Method m, Klass to, int linenumber, Type type) { |
|
101 this.fromClass = from; |
|
102 this.method = m; |
|
103 this.toClass = to; |
|
104 this.linenumber = linenumber; |
|
105 this.type = type; |
|
106 this.info = m.toString(); |
|
107 } |
|
108 |
|
109 public boolean isPublic() { |
|
110 return isPublic; |
|
111 } |
|
112 |
|
113 public void setPublicAccess(boolean value) { |
|
114 isPublic = value; |
|
115 } |
|
116 static ResolutionInfo resolved(Klass from, Klass to) { |
|
117 return new ResolutionInfo(from, to, 0, Type.NOINFO, ""); |
|
118 } |
|
119 |
|
120 static ResolutionInfo resolved(Klass from, Klass to, int linenumber) { |
|
121 return new ResolutionInfo(from, to, linenumber, Type.NOINFO, ""); |
|
122 } |
|
123 |
|
124 static ResolutionInfo resolved(Klass from, Klass to, int linenumber, String reason) { |
|
125 String[] ss = reason.split("\\s+"); |
|
126 Type type; |
|
127 String info; |
|
128 if (linenumber == -1) { |
|
129 type = Type.NATIVE; |
|
130 info = ss[0]; // native method name |
|
131 } else { |
|
132 info = ss.length == 2 ? ss[1] : ""; |
|
133 type = Type.getType(ss[0]); |
|
134 if (type == null) { |
|
135 if (reason.isEmpty()) { |
|
136 throw new IllegalArgumentException("Invalid type: " + reason + " (" + ss[0] + ")" + ss.length); |
|
137 } |
|
138 // assume it's native |
|
139 type = Type.NATIVE; |
|
140 info = reason.isEmpty() ? ss[0] : reason; |
|
141 } |
|
142 } |
|
143 |
|
144 return new ResolutionInfo(from, to, linenumber, type, info); |
|
145 } |
|
146 |
|
147 static ResolutionInfo resolved(Klass from, Klass to, Method callee) { |
|
148 return new ResolutionInfo(from, callee, to, 0, Type.METHODTRACE); |
|
149 } |
|
150 |
|
151 static ResolutionInfo resolvedConstantPool(Klass from, Klass to, int index) { |
|
152 return new ResolutionInfo(from, to, 0, Type.CONSTANT_POOL, "#" + index); |
|
153 } |
|
154 |
|
155 static ResolutionInfo resolvedField(Klass from, Klass to, String fieldname) { |
|
156 return new ResolutionInfo(from, to, 0, Type.FIELD, fieldname); |
|
157 } |
|
158 |
|
159 static ResolutionInfo resolvedMethodSignature(Klass from, Klass to, Method m) { |
|
160 return new ResolutionInfo(from, m, to, 0, Type.METHOD); |
|
161 } |
|
162 |
|
163 static ResolutionInfo resolvedCheckedException(Klass from, Klass to, Method m) { |
|
164 return new ResolutionInfo(from, m, to, 0, Type.CHECKED_EXCEPTION); |
|
165 } |
|
166 |
|
167 static ResolutionInfo resolvedExtends(Klass from, Klass to) { |
|
168 String info = from.getClassName() + " implements " + to.getClassName(); |
|
169 return new ResolutionInfo(from, to, 0, Type.EXTENDS, info); |
|
170 } |
|
171 |
|
172 static ResolutionInfo resolvedImplements(Klass from, Klass to) { |
|
173 String info = from.getClassName() + " implements " + to.getClassName(); |
|
174 return new ResolutionInfo(from, to, 0, Type.IMPLEMENTS, info); |
|
175 } |
|
176 |
|
177 @Override |
|
178 public int compareTo(ResolutionInfo ri) { |
|
179 if (this.fromClass == ri.fromClass && |
|
180 this.toClass == ri.toClass && |
|
181 this.linenumber == ri.linenumber && |
|
182 this.type == ri.type && |
|
183 this.info.equals(ri.info)) { |
|
184 return 0; |
|
185 } else if (this.fromClass == ri.fromClass) { |
|
186 if (this.linenumber > ri.linenumber) { |
|
187 return 1; |
|
188 } else if (this.linenumber < ri.linenumber) { |
|
189 return -1; |
|
190 } else if (this.type != ri.type) { |
|
191 return this.type.getName().compareTo(ri.type.getName()); |
|
192 } else if (this.toClass != ri.toClass) { |
|
193 return this.toClass.compareTo(ri.toClass); |
|
194 } else { |
|
195 return this.info.compareTo(ri.info); |
|
196 } |
|
197 } else { |
|
198 return this.fromClass.compareTo(ri.fromClass); |
|
199 } |
|
200 } |
|
201 } |
|