WrapperHandler.java

biz/hammurapi/wrap/WrapperHandler.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 049 Use a Collection instead of arrays Object[] 2 66:23
Java Inspector 049 Use a Collection instead of arrays Object[] 2 132:30
Java Inspector 049 Use a Collection instead of arrays Object[] 2 148:30
Java Inspector 081 Avoid static collections, they can grow in size over time. 2 52:9
Java Inspector 089 Undocumented constructor 2 54:9
Java Inspector 089 Undocumented method 2 61:9
Java Inspector 089 Undocumented method 2 65:9
Java Inspector 089 Method documentation is too short. It is only 2 words. Should be at least 3 words. 2 103:9
Java Inspector 089 Undocumented method 2 120:9
Java Inspector 089 Undocumented method 2 124:9
Java Inspector 089 Method is not properly documented 2 130:9
Java Inspector 089 Parameter sourceClass is not documented 2 130:9
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 76:89
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 76:133
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 87:56
Java Inspector 040 Parameter name proxy clashes with field name in WrapperHandler 3 65:30
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 54:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.wrap;
24
25import java.lang.reflect.InvocationHandler;
26import java.lang.reflect.Method;
27import java.lang.reflect.Proxy;
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.Collection;
31import java.util.HashMap;
32import java.util.HashSet;
33import java.util.Iterator;
34import java.util.Map;
35import java.util.Set;
36
37/**
38 * Wraps Jsel object such as the underlying object becomes garbage collection
39 * eligible and will be transparently restored from the database and source files
40 * on demand
41 * @author Pavel Vlasov
42 * @version $Revision: 1.2 $
43 */
44public abstract class WrapperHandler implements InvocationHandler {
45 private Object proxy;
46
47 /**
48 * Subclasses can add more classes to wrap.
49 */
50 protected Collection classesToWrap=new ArrayList();
51
52 private static Map interfaceMap=new HashMap();
53
54 public WrapperHandler(Object master) {
55 proxy=Proxy.newProxyInstance(master.getClass().getClassLoader(), getClassInterfaces(master.getClass()), this);
56
57 classesToWrap.add(Collection.class);
58 classesToWrap.add(Map.class);
59 }
60
61 public Object getProxy() {
62 return proxy;
63 }
64
65 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
66 Object[] unwrappedArgs;
67 if (args!=null && args.length!=0) {
68 unwrappedArgs=new Object[args.length];
69 for (int i=0; i<unwrappedArgs.length; i++) {
70 unwrappedArgs[i]=args[i];
71 if (Proxy.isProxyClass(args[i].getClass())) {
72 InvocationHandler handler=Proxy.getInvocationHandler(proxy);
73 if (handler instanceof WrapperHandler) {
74 unwrappedArgs[i]=((WrapperHandler) handler).getMaster();
75 if (unwrappedArgs[i]==null) {
76 throw new IllegalStateException("Unwrapped object is null for parameter "+i+" in method "+method);
77 }
78 }
79 }
80 }
81 } else {
82 unwrappedArgs=args;
83 }
84
85 Object master = getMaster();
86 if (master==null) {
87 throw new NullPointerException("Master object is null");
88 }
89
90 Object ret=method.invoke(master, unwrappedArgs);
91
92 return wrap(ret, classesToWrap);
93 }
94
95 /**
96 * Convenience method.
97 * @param toWrap Object to wrap
98 * @param classesToWrap collection of classes that shall be wrapped. Wrappable is always wrapped. Can be null.
99 * @return Proxy object for toWrap if toWrap is either Wrappable or instance of one of classes from classesToWrap,
100 * toWrap otherwise
101 * @throws Throwable
102 */
103 public static Object wrap(Object toWrap, Collection classesToWrap) throws Throwable {
104 if (toWrap instanceof Wrappable) {
105 return ((Wrappable) toWrap).getProxy();
106 }
107
108 if (classesToWrap!=null) {
109 Iterator it=classesToWrap.iterator();
110 while (it.hasNext()) {
111 if (((Class) it.next()).isInstance(toWrap)) {
112 return new StrongWrapperHandler(toWrap).getProxy();
113 }
114 }
115 }
116
117 return toWrap;
118 }
119
120 public static Object wrap(Object toWrap) throws Throwable {
121 return wrap(toWrap, null);
122 }
123
124 protected abstract Object getMaster() throws Throwable;
125
126 /**
127 * @param sourceClass
128 * @return all interfaces implemented by this class
129 */
130 public static Class[] getClassInterfaces(Class sourceClass) {
131 synchronized (interfaceMap) {
132 Class[] ret=(Class[]) interfaceMap.get(sourceClass);
133 if (ret==null) {
134 Set set=new HashSet();
135 getClassInterfaces(sourceClass, set);
136 ret = (Class[]) new ArrayList(set).toArray(new Class[set.size()]);
137 interfaceMap.put(sourceClass, ret);
138 }
139 return ret;
140 }
141 }
142
143 /**
144 * @param sourceClass
145 */
146 private static void getClassInterfaces(Class sourceClass, Collection set) {
147 if (sourceClass!=null) {
148 Class[] interfaces = sourceClass.getInterfaces();
149 for (int i=0; interfaces!=null && i<interfaces.length; i++) {
150 getClassInterfaces(interfaces[i]);
151 }
152 getClassInterfaces(sourceClass.getSuperclass(), set);
153 set.addAll(Arrays.asList(interfaces));
154 }
155 }
156}
157