|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ClassHierarchyIntrospector.java | 91.7% | 83.8% | 60% | 84.8% |
|
1 |
/*****************************************************************************
|
|
2 |
* Copyright (C) PicoContainer Organization. All rights reserved. *
|
|
3 |
* ------------------------------------------------------------------------- *
|
|
4 |
* The software in this package is published under the terms of the BSD *
|
|
5 |
* style license a copy of which has been included with this distribution in *
|
|
6 |
* the LICENSE.txt file. *
|
|
7 |
* *
|
|
8 |
* Original code by *
|
|
9 |
*****************************************************************************/
|
|
10 |
package org.picocontainer.defaults;
|
|
11 |
|
|
12 |
import java.lang.reflect.Method;
|
|
13 |
import java.util.HashSet;
|
|
14 |
import java.util.Iterator;
|
|
15 |
import java.util.List;
|
|
16 |
import java.util.Set;
|
|
17 |
|
|
18 |
/**
|
|
19 |
* Helper class for introspecting interface and class hierarchies.
|
|
20 |
*
|
|
21 |
* @deprecated Use {@link com.thoughtworks.proxy.toys.multicast.ClassHierarchyIntrospector} instead. *
|
|
22 |
* @author Aslak Hellesøy
|
|
23 |
* @author Jörg Schaible
|
|
24 |
* @version $Revision: 1.3 $
|
|
25 |
*/
|
|
26 |
public class ClassHierarchyIntrospector { |
|
27 |
/** the {@link Object#equals(Object)} method. */
|
|
28 |
public static Method equals; |
|
29 |
/** the {@link Object#hashCode()} method. */
|
|
30 |
public static Method hashCode; |
|
31 |
|
|
32 |
static {
|
|
33 | 4 |
try {
|
34 | 4 |
equals = Object.class.getMethod("equals", new Class[]{Object.class}); |
35 | 4 |
hashCode = Object.class.getMethod("hashCode", null); |
36 |
} catch (NoSuchMethodException e) {
|
|
37 |
///CLOVER:OFF
|
|
38 |
throw new InternalError(); |
|
39 |
///CLOVER:ON
|
|
40 |
} catch (SecurityException e) {
|
|
41 |
///CLOVER:OFF
|
|
42 |
throw new InternalError(); |
|
43 |
///CLOVER:ON
|
|
44 |
} |
|
45 |
} |
|
46 |
|
|
47 | 0 |
private ClassHierarchyIntrospector() {
|
48 |
} |
|
49 |
|
|
50 |
/**
|
|
51 |
* Get all the interfaces implemented by a list of objects.
|
|
52 |
* @param objects the {@link List} of objects to consider.
|
|
53 |
* @return a {@link Set} of interfaces.
|
|
54 |
*/
|
|
55 | 0 |
public static Set getAllInterfaces(List objects) { |
56 | 0 |
Set interfaces = new HashSet();
|
57 | 0 |
for (Iterator iterator = objects.iterator(); iterator.hasNext();) {
|
58 | 0 |
Object o = iterator.next(); |
59 | 0 |
Class clazz = o.getClass(); |
60 | 0 |
getInterfaces(clazz, interfaces); |
61 |
} |
|
62 | 0 |
return interfaces;
|
63 |
} |
|
64 |
|
|
65 |
/**
|
|
66 |
* Get all interfaces of the given type.
|
|
67 |
* If the type is a {@link Class}, the returned list contains any interface, that is
|
|
68 |
* implemented by the class. If the type is an interface, the all
|
|
69 |
* superinterfaces and the interface itself are included.
|
|
70 |
* @param clazz type to explore.
|
|
71 |
* @return a {@link Set} with all interfaces. The array may be empty.
|
|
72 |
*/
|
|
73 | 14 |
public static Set getAllInterfaces(Class clazz) { |
74 | 14 |
Set interfaces = new HashSet();
|
75 | 14 |
getInterfaces(clazz, interfaces); |
76 | 14 |
return interfaces;
|
77 |
} |
|
78 |
|
|
79 | 46 |
private static void getInterfaces(Class clazz, Set interfaces) { |
80 | 46 |
if(clazz.isInterface()) {
|
81 | 33 |
interfaces.add(clazz); |
82 |
} |
|
83 |
// Class.getInterfaces will return only the interfaces that are
|
|
84 |
// implemented by the current class. Therefore we must loop up
|
|
85 |
// the hierarchy for the superclasses and the superinterfaces.
|
|
86 | 46 |
while (clazz != null) { |
87 | 65 |
Class[] implemented = clazz.getInterfaces(); |
88 | 65 |
for(int i = 0; i < implemented.length; i++) { |
89 | 38 |
if (!interfaces.contains(implemented[i])) {
|
90 | 32 |
getInterfaces(implemented[i], interfaces); |
91 |
} |
|
92 |
} |
|
93 | 65 |
clazz = clazz.getSuperclass(); |
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
/**
|
|
98 |
* Get most common superclass for all given objects.
|
|
99 |
* @param objects the array of objects to consider.
|
|
100 |
* @return the superclass or <code>{@link Void}.class</code> for an empty array.
|
|
101 |
*/
|
|
102 | 15 |
public static Class getMostCommonSuperclass(Object[] objects) { |
103 | 15 |
Class clazz = Void.class;
|
104 | 15 |
boolean found = false; |
105 | 15 |
if (objects != null && objects.length > 0) { |
106 | 14 |
while (!found) {
|
107 | 29 |
for (int i = 0; i < objects.length; i++) { |
108 | 66 |
found = true;
|
109 | 66 |
if (objects[i] != null) { |
110 | 64 |
Class currentClazz = objects[i].getClass(); |
111 | 64 |
if(clazz == Void.class) { |
112 | 13 |
clazz = currentClazz; |
113 |
} |
|
114 | 64 |
if(!clazz.isAssignableFrom(currentClazz)) {
|
115 | 17 |
if(currentClazz.isAssignableFrom(clazz)) {
|
116 | 2 |
clazz = currentClazz; |
117 |
} else {
|
|
118 | 15 |
clazz = clazz.getSuperclass(); |
119 | 15 |
found = false;
|
120 | 15 |
break;
|
121 |
} |
|
122 |
} |
|
123 |
} |
|
124 |
} |
|
125 |
} |
|
126 |
} |
|
127 | 15 |
return clazz;
|
128 |
} |
|
129 |
} |
|
130 |
|
|