1 /*
2 * Copyright (c) 2003, 2014, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.reflect.annotation;
27
28 import java.io.ObjectInputStream;
29 import java.lang.annotation.*;
30 import java.lang.reflect.*;
31 import java.io.Serializable;
32 import java.util.*;
33 import java.security.AccessController;
34 import java.security.PrivilegedAction;
35
36 /**
37 * InvocationHandler for dynamic proxy implementation of Annotation.
38 *
39 * @author Josh Bloch
40 * @since 1.5
41 */
42 class AnnotationInvocationHandler implements InvocationHandler, Serializable {
43 private static final long serialVersionUID = 6182022883658399397L;
44 private final Class<? extends Annotation> type;
45 private final Map<String, Object> memberValues;
46
47 AnnotationInvocationHandler(Class<? extends Annotation> type, Map<String, Object> memberValues) {
48 Class<?>[] superInterfaces = type.getInterfaces();
49 if (!type.isAnnotation() ||
50 superInterfaces.length != 1 ||
51 superInterfaces[0] != java.lang.annotation.Annotation.class)
52 throw new AnnotationFormatError("Attempt to create proxy for a non-annotation type.");
141 StringBuilder result = new StringBuilder(128);
142 result.append('@');
143 result.append(type.getName());
144 result.append('(');
145 boolean firstMember = true;
146 for (Map.Entry<String, Object> e : memberValues.entrySet()) {
147 if (firstMember)
148 firstMember = false;
149 else
150 result.append(", ");
151
152 result.append(e.getKey());
153 result.append('=');
154 result.append(memberValueToString(e.getValue()));
155 }
156 result.append(')');
157 return result.toString();
158 }
159
160 /**
161 * Translates a member value (in "dynamic proxy return form") into a string
162 */
163 private static String memberValueToString(Object value) {
164 Class<?> type = value.getClass();
165 if (!type.isArray()) // primitive, string, class, enum const,
166 // or annotation
167 return value.toString();
168
169 if (type == byte[].class)
170 return Arrays.toString((byte[]) value);
171 if (type == char[].class)
172 return Arrays.toString((char[]) value);
173 if (type == double[].class)
174 return Arrays.toString((double[]) value);
175 if (type == float[].class)
176 return Arrays.toString((float[]) value);
177 if (type == int[].class)
178 return Arrays.toString((int[]) value);
179 if (type == long[].class)
180 return Arrays.toString((long[]) value);
181 if (type == short[].class)
182 return Arrays.toString((short[]) value);
183 if (type == boolean[].class)
184 return Arrays.toString((boolean[]) value);
185 return Arrays.toString((Object[]) value);
186 }
187
188 /**
189 * Implementation of dynamicProxy.equals(Object o)
190 */
191 private Boolean equalsImpl(Object o) {
192 if (o == this)
193 return true;
194
195 if (!type.isInstance(o))
196 return false;
197 for (Method memberMethod : getMemberMethods()) {
198 String member = memberMethod.getName();
199 Object ourValue = memberValues.get(member);
200 Object hisValue = null;
201 AnnotationInvocationHandler hisHandler = asOneOfUs(o);
202 if (hisHandler != null) {
203 hisValue = hisHandler.memberValues.get(member);
204 } else {
205 try {
|
1 /*
2 * Copyright (c) 2003, 2016, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package sun.reflect.annotation;
27
28 import java.io.ObjectInputStream;
29 import java.lang.annotation.*;
30 import java.lang.reflect.*;
31 import java.io.Serializable;
32 import java.util.*;
33 import java.util.stream.Collectors;
34 import java.security.AccessController;
35 import java.security.PrivilegedAction;
36
37 /**
38 * InvocationHandler for dynamic proxy implementation of Annotation.
39 *
40 * @author Josh Bloch
41 * @since 1.5
42 */
43 class AnnotationInvocationHandler implements InvocationHandler, Serializable {
44 private static final long serialVersionUID = 6182022883658399397L;
45 private final Class<? extends Annotation> type;
46 private final Map<String, Object> memberValues;
47
48 AnnotationInvocationHandler(Class<? extends Annotation> type, Map<String, Object> memberValues) {
49 Class<?>[] superInterfaces = type.getInterfaces();
50 if (!type.isAnnotation() ||
51 superInterfaces.length != 1 ||
52 superInterfaces[0] != java.lang.annotation.Annotation.class)
53 throw new AnnotationFormatError("Attempt to create proxy for a non-annotation type.");
142 StringBuilder result = new StringBuilder(128);
143 result.append('@');
144 result.append(type.getName());
145 result.append('(');
146 boolean firstMember = true;
147 for (Map.Entry<String, Object> e : memberValues.entrySet()) {
148 if (firstMember)
149 firstMember = false;
150 else
151 result.append(", ");
152
153 result.append(e.getKey());
154 result.append('=');
155 result.append(memberValueToString(e.getValue()));
156 }
157 result.append(')');
158 return result.toString();
159 }
160
161 /**
162 * Translates a member value (in "dynamic proxy return form") into a string.
163 */
164 private static String memberValueToString(Object value) {
165 Class<?> type = value.getClass();
166 if (!type.isArray()) { // primitive, string, class, enum const,
167 // or annotation
168 if (type == Class.class)
169 return classValueToString((Class<?>) value);
170 else
171 return value.toString();
172 }
173
174 if (type == byte[].class)
175 return Arrays.toString((byte[]) value);
176 if (type == char[].class)
177 return Arrays.toString((char[]) value);
178 if (type == double[].class)
179 return Arrays.toString((double[]) value);
180 if (type == float[].class)
181 return Arrays.toString((float[]) value);
182 if (type == int[].class)
183 return Arrays.toString((int[]) value);
184 if (type == long[].class)
185 return Arrays.toString((long[]) value);
186 if (type == short[].class)
187 return Arrays.toString((short[]) value);
188 if (type == boolean[].class)
189 return Arrays.toString((boolean[]) value);
190 if (type == Class[].class)
191 return classArrayValueToString((Class<?>[])value);
192 return Arrays.toString((Object[]) value);
193 }
194
195 /**
196 * Translates a Class value to a form suitable for use in the
197 * string representation of an annotation.
198 */
199 private static String classValueToString(Class<?> clazz) {
200 return clazz.getName() + ".class" ;
201 }
202
203 private static String classArrayValueToString(Class<?>[] classes) {
204 return Arrays.stream(classes)
205 .map(AnnotationInvocationHandler::classValueToString)
206 .collect(Collectors.joining(", ", "{", "}"));
207 }
208
209 /**
210 * Implementation of dynamicProxy.equals(Object o)
211 */
212 private Boolean equalsImpl(Object o) {
213 if (o == this)
214 return true;
215
216 if (!type.isInstance(o))
217 return false;
218 for (Method memberMethod : getMemberMethods()) {
219 String member = memberMethod.getName();
220 Object ourValue = memberValues.get(member);
221 Object hisValue = null;
222 AnnotationInvocationHandler hisHandler = asOneOfUs(o);
223 if (hisHandler != null) {
224 hisValue = hisHandler.memberValues.get(member);
225 } else {
226 try {
|