1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.commons.proxy;
19
20 import org.apache.commons.proxy.invoker.NullInvoker;
21
22 import java.util.LinkedList;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.HashMap;
26
27 /**
28 * Provides some helpful proxy utility methods.
29 *
30 * @author James Carman
31 * @since 1.0
32 */
33 public class ProxyUtils
34 {
35 //----------------------------------------------------------------------------------------------------------------------
36 // Fields
37 //----------------------------------------------------------------------------------------------------------------------
38
39 public static final Object[] EMPTY_ARGUMENTS = new Object[0];
40 public static final Class[] EMPTY_ARGUMENT_TYPES = new Class[0];
41 private static final Map wrapperClassMap = new HashMap();
42
43 //----------------------------------------------------------------------------------------------------------------------
44 // Static Methods
45 //----------------------------------------------------------------------------------------------------------------------
46
47 static
48 {
49 wrapperClassMap.put( Integer.TYPE, Integer.class );
50 wrapperClassMap.put( Character.TYPE, Character.class );
51 wrapperClassMap.put( Boolean.TYPE, Boolean.class );
52 wrapperClassMap.put( Short.TYPE, Short.class );
53 wrapperClassMap.put( Long.TYPE, Long.class );
54 wrapperClassMap.put( Float.TYPE, Float.class );
55 wrapperClassMap.put( Double.TYPE, Double.class );
56 wrapperClassMap.put( Byte.TYPE, Byte.class );
57 }
58 /**
59 * Creates a "null object" which implements the <code>proxyClasses</code>.
60 *
61 * @param proxyFactory the proxy factory to be used to create the proxy object
62 * @param proxyClasses the proxy interfaces
63 * @return a "null object" which implements the <code>proxyClasses</code>.
64 */
65 public static Object createNullObject( ProxyFactory proxyFactory, Class[] proxyClasses )
66 {
67 return proxyFactory.createInvokerProxy( new NullInvoker(), proxyClasses );
68 }
69
70 /**
71 * Creates a "null object" which implements the <code>proxyClasses</code>.
72 *
73 * @param proxyFactory the proxy factory to be used to create the proxy object
74 * @param classLoader the class loader to be used by the proxy factory to create the proxy object
75 * @param proxyClasses the proxy interfaces
76 * @return a "null object" which implements the <code>proxyClasses</code>.
77 */
78 public static Object createNullObject( ProxyFactory proxyFactory, ClassLoader classLoader, Class[] proxyClasses )
79 {
80 return proxyFactory.createInvokerProxy( classLoader, new NullInvoker(), proxyClasses );
81 }
82
83 /**
84 * <p>Gets an array of {@link Class} objects representing all interfaces implemented by the given class and its
85 * superclasses.</p>
86 * <p/>
87 * <p>The order is determined by looking through each interface in turn as declared in the source file and following
88 * its hierarchy up. Then each superclass is considered in the same way. Later duplicates are ignored, so the order
89 * is maintained.</p>
90 * <p/>
91 * <b>Note</b>: Implementation of this method was "borrowed" from
92 * <a href="http://commons.apache.org/lang/">Apache Commons Lang</a> to avoid a dependency.</p>
93 *
94 * @param cls the class to look up, may be <code>null</code>
95 * @return an array of {@link Class} objects representing all interfaces implemented by the given class and its
96 * superclasses or <code>null</code> if input class is null.
97 */
98 public static Class[] getAllInterfaces( Class cls )
99 {
100 final List interfaces = getAllInterfacesImpl( cls, new LinkedList() );
101 return interfaces == null ? null : ( Class[] ) interfaces.toArray( new Class[interfaces.size()] );
102 }
103
104 private static List getAllInterfacesImpl( Class cls, List list )
105 {
106 if( cls == null )
107 {
108 return null;
109 }
110 while( cls != null )
111 {
112 Class[] interfaces = cls.getInterfaces();
113 for( int i = 0; i < interfaces.length; i++ )
114 {
115 if( !list.contains( interfaces[i] ) )
116 {
117 list.add( interfaces[i] );
118 }
119 getAllInterfacesImpl( interfaces[i], list );
120 }
121 cls = cls.getSuperclass();
122 }
123 return list;
124 }
125
126 /**
127 * Returns the class name as you would expect to see it in Java code.
128 * <p/>
129 * <b>Examples:</b> <ul> <li>getJavaClassName( Object[].class ) == "Object[]"</li> <li>getJavaClassName(
130 * Object[][].class ) == "Object[][]"</li> <li>getJavaClassName( Integer.TYPE ) == "int"</li> </p>
131 *
132 * @param clazz the class
133 * @return the class' name as you would expect to see it in Java code
134 */
135 public static String getJavaClassName( Class clazz )
136 {
137 if( clazz.isArray() )
138 {
139 return getJavaClassName( clazz.getComponentType() ) + "[]";
140 }
141 return clazz.getName();
142 }
143
144 /**
145 * Returns the wrapper class for the given primitive type.
146 * @param primitiveType the primitive type
147 * @return the wrapper class
148 */
149 public static Class getWrapperClass( Class primitiveType )
150 {
151 return ( Class )wrapperClassMap.get( primitiveType );
152 }
153 }
154