1 /*
2 * Copyright (c) 1997, 2013, 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
27
28 import java.lang.reflect.Array;
29 import java.lang.reflect.Field;
30 import java.lang.reflect.GenericArrayType;
31 import java.lang.reflect.GenericDeclaration;
32 import java.lang.reflect.Method;
33 import java.lang.reflect.Modifier;
34 import java.lang.reflect.ParameterizedType;
35 import java.lang.reflect.Type;
36 import java.lang.reflect.TypeVariable;
37 import java.lang.reflect.WildcardType;
38 import java.util.Arrays;
39 import java.util.Collection;
40
41 import com.sun.xml.internal.bind.v2.runtime.Location;
42
43 /**
44 * {@link Navigator} implementation for {@code java.lang.reflect}.
45 *
46 */
47 public final class ReflectionNavigator implements Navigator<Type, Class, Field, Method> {
48
49 /**
50 * Singleton.
51 *
52 * Use {@link Navigator#REFLECTION}
53 */
54 ReflectionNavigator() {
55 }
56
57 public Class getSuperClass(Class clazz) {
58 if (clazz == Object.class) {
59 return null;
60 }
61 Class sc = clazz.getSuperclass();
62 if (sc == null) {
63 sc = Object.class; // error recovery
64 }
65 return sc;
66 }
67 private static final TypeVisitor<Type, Class> baseClassFinder = new TypeVisitor<Type, Class>() {
68
69 public Type onClass(Class c, Class sup) {
70 // t is a raw type
71 if (sup == c) {
72 return sup;
73 }
74
75 Type r;
76
77 Type sc = c.getGenericSuperclass();
78 if (sc != null) {
79 r = visit(sc, sup);
80 if (r != null) {
81 return r;
82 }
83 }
84
85 for (Type i : c.getGenericInterfaces()) {
86 r = visit(i, sup);
479 return field.toString();
480 }
481 };
482 }
483
484 public Location getMethodLocation(final Method method) {
485 return new Location() {
486
487 @Override
488 public String toString() {
489 return method.toString();
490 }
491 };
492 }
493
494 public boolean hasDefaultConstructor(Class c) {
495 try {
496 c.getDeclaredConstructor();
497 return true;
498 } catch (NoSuchMethodException e) {
499 return false;
500 }
501 }
502
503 public boolean isStaticField(Field field) {
504 return Modifier.isStatic(field.getModifiers());
505 }
506
507 public boolean isPublicMethod(Method method) {
508 return Modifier.isPublic(method.getModifiers());
509 }
510
511 public boolean isPublicField(Field field) {
512 return Modifier.isPublic(field.getModifiers());
513 }
514
515 public boolean isEnum(Class c) {
516 return Enum.class.isAssignableFrom(c);
517 }
518
519 public Field[] getEnumConstants(Class clazz) {
527 } catch (NoSuchFieldException e) {
528 // impossible
529 throw new NoSuchFieldError(e.getMessage());
530 }
531 }
532
533 public Type getVoidType() {
534 return Void.class;
535 }
536
537 public String getPackageName(Class clazz) {
538 String name = clazz.getName();
539 int idx = name.lastIndexOf('.');
540 if (idx < 0) {
541 return "";
542 } else {
543 return name.substring(0, idx);
544 }
545 }
546
547 public Class findClass(String className, Class referencePoint) {
548 try {
549 ClassLoader cl = SecureLoader.getClassClassLoader(referencePoint);
550 if (cl == null) {
551 cl = SecureLoader.getSystemClassLoader();
552 }
553 return cl.loadClass(className);
554 } catch (ClassNotFoundException e) {
555 return null;
556 }
557 }
558
559 public boolean isBridgeMethod(Method method) {
560 return method.isBridge();
561 }
562
563 public boolean isOverriding(Method method, Class base) {
564 // this isn't actually correct,
565 // as the JLS considers
566 // class Derived extends Base<Integer> {
567 // Integer getX() { ... }
568 // }
569 // class Base<T> {
570 // T getX() { ... }
571 // }
572 // to be overrided. Handling this correctly needs a careful implementation
573
|
1 /*
2 * Copyright (c) 1997, 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
27
28 import java.lang.reflect.Array;
29 import java.lang.reflect.Field;
30 import java.lang.reflect.GenericArrayType;
31 import java.lang.reflect.GenericDeclaration;
32 import java.lang.reflect.Method;
33 import java.lang.reflect.Modifier;
34 import java.lang.reflect.ParameterizedType;
35 import java.lang.reflect.Type;
36 import java.lang.reflect.TypeVariable;
37 import java.lang.reflect.WildcardType;
38 import java.util.Arrays;
39 import java.util.Collection;
40
41 import com.sun.xml.internal.bind.v2.runtime.Location;
42
43 /**
44 * {@link Navigator} implementation for {@code java.lang.reflect}.
45 *
46 */
47 /*package*/final class ReflectionNavigator implements Navigator<Type, Class, Field, Method> {
48
49 // ---------- Singleton -----------------
50 private static final ReflectionNavigator INSTANCE = new ReflectionNavigator();
51
52 /*package*/static ReflectionNavigator getInstance() {
53 return INSTANCE;
54 }
55
56 private ReflectionNavigator() {
57 }
58 // ---------------------------------------
59
60 public Class getSuperClass(Class clazz) {
61 if (clazz == Object.class) {
62 return null;
63 }
64 Class sc = clazz.getSuperclass();
65 if (sc == null) {
66 sc = Object.class; // error recovery
67 }
68 return sc;
69 }
70
71 private static final TypeVisitor<Type, Class> baseClassFinder = new TypeVisitor<Type, Class>() {
72
73 public Type onClass(Class c, Class sup) {
74 // t is a raw type
75 if (sup == c) {
76 return sup;
77 }
78
79 Type r;
80
81 Type sc = c.getGenericSuperclass();
82 if (sc != null) {
83 r = visit(sc, sup);
84 if (r != null) {
85 return r;
86 }
87 }
88
89 for (Type i : c.getGenericInterfaces()) {
90 r = visit(i, sup);
483 return field.toString();
484 }
485 };
486 }
487
488 public Location getMethodLocation(final Method method) {
489 return new Location() {
490
491 @Override
492 public String toString() {
493 return method.toString();
494 }
495 };
496 }
497
498 public boolean hasDefaultConstructor(Class c) {
499 try {
500 c.getDeclaredConstructor();
501 return true;
502 } catch (NoSuchMethodException e) {
503 return false; // todo: do this WITHOUT exception throw
504 }
505 }
506
507 public boolean isStaticField(Field field) {
508 return Modifier.isStatic(field.getModifiers());
509 }
510
511 public boolean isPublicMethod(Method method) {
512 return Modifier.isPublic(method.getModifiers());
513 }
514
515 public boolean isPublicField(Field field) {
516 return Modifier.isPublic(field.getModifiers());
517 }
518
519 public boolean isEnum(Class c) {
520 return Enum.class.isAssignableFrom(c);
521 }
522
523 public Field[] getEnumConstants(Class clazz) {
531 } catch (NoSuchFieldException e) {
532 // impossible
533 throw new NoSuchFieldError(e.getMessage());
534 }
535 }
536
537 public Type getVoidType() {
538 return Void.class;
539 }
540
541 public String getPackageName(Class clazz) {
542 String name = clazz.getName();
543 int idx = name.lastIndexOf('.');
544 if (idx < 0) {
545 return "";
546 } else {
547 return name.substring(0, idx);
548 }
549 }
550
551 @Override
552 public Class loadObjectFactory(Class referencePoint, String pkg) {
553 ClassLoader cl = SecureLoader.getClassClassLoader(referencePoint);
554 if (cl == null)
555 cl = SecureLoader.getSystemClassLoader();
556
557 try {
558 return cl.loadClass(pkg + ".ObjectFactory");
559 } catch (ClassNotFoundException e) {
560 return null;
561 }
562 }
563
564 public boolean isBridgeMethod(Method method) {
565 return method.isBridge();
566 }
567
568 public boolean isOverriding(Method method, Class base) {
569 // this isn't actually correct,
570 // as the JLS considers
571 // class Derived extends Base<Integer> {
572 // Integer getX() { ... }
573 // }
574 // class Base<T> {
575 // T getX() { ... }
576 // }
577 // to be overrided. Handling this correctly needs a careful implementation
578
|