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.proxy2.impl;
19
20 import java.lang.reflect.Method;
21 import java.lang.reflect.Modifier;
22 import java.util.Collection;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28 /**
29 * A useful superclass for {@link ProxyClassGenerator} implementations.
30 *
31 * @since 1.0
32 */
33 public abstract class AbstractProxyClassGenerator implements ProxyClassGenerator
34 {
35 //******************************************************************************************************************
36 // Static Methods
37 //******************************************************************************************************************
38
39 /**
40 * Returns all methods that a proxy class must implement from the proxy interfaces. This method makes sure there are
41 * no method signature clashes. For methods with the same signature (name and parameter types), the one encountered
42 * first will be returned in the result. Final methods are also excluded from the result.
43 *
44 * @param proxyClasses
45 * the interfaces the proxy class must implement
46 * @return all methods that the proxy class must implement
47 */
48 public static Method[] getImplementationMethods(Class<?>[] proxyClasses)
49 {
50 final Map<MethodSignature, Method> signatureMethodMap = new HashMap<MethodSignature, Method>();
51 final Set<MethodSignature> finalizedSignatures = new HashSet<MethodSignature>();
52 for (int i = 0; i < proxyClasses.length; i++)
53 {
54 Class<?> proxyInterface = proxyClasses[i];
55 final Method[] methods = proxyInterface.getMethods();
56 for (int j = 0; j < methods.length; j++)
57 {
58 final MethodSignature signature = new MethodSignature(methods[j]);
59 if (Modifier.isFinal(methods[j].getModifiers()))
60 {
61 finalizedSignatures.add(signature);
62 }
63 else if (!signatureMethodMap.containsKey(signature))
64 {
65 signatureMethodMap.put(signature, methods[j]);
66 }
67 }
68 }
69 final Collection<Method> resultingMethods = signatureMethodMap.values();
70 for (MethodSignature signature : finalizedSignatures)
71 {
72 resultingMethods.remove(signatureMethodMap.get(signature));
73 }
74 return resultingMethods.toArray(new Method[resultingMethods.size()]);
75 }
76 }