interface.
| Method from org.apache.openjpa.util.ProxyManagerImpl Detail: |
protected boolean allowsDuplicates(Class type) {
return !Set.class.isAssignableFrom(type);
}
Return whether the given collection type allows duplicates. |
public Object copyArray(Object orig) {
if (orig == null)
return null;
try {
int length = Array.getLength(orig);
Object array = Array.newInstance(orig.getClass().
getComponentType(), length);
System.arraycopy(orig, 0, array, 0, length);
return array;
} catch (Exception e) {
throw new UnsupportedException(_loc.get("bad-array",
e.getMessage()), e);
}
}
|
public Calendar copyCalendar(Calendar orig) {
if (orig == null)
return null;
if (orig instanceof Proxy)
return (Calendar) ((Proxy) orig).copy(orig);
ProxyCalendar proxy = getFactoryProxyCalendar(orig.getClass());
return (Calendar) proxy.copy(orig);
}
|
public Collection copyCollection(Collection orig) {
if (orig == null)
return null;
if (orig instanceof Proxy)
return (Collection) ((Proxy) orig).copy(orig);
ProxyCollection proxy = getFactoryProxyCollection(orig.getClass());
return (Collection) proxy.copy(orig);
}
|
public Object copyCustom(Object orig) {
if (orig == null)
return null;
if (orig instanceof Proxy)
return ((Proxy) orig).copy(orig);
if (ImplHelper.isManageable(orig))
return null;
if (orig instanceof Collection)
return copyCollection((Collection) orig);
if (orig instanceof Map)
return copyMap((Map) orig);
if (orig instanceof Date)
return copyDate((Date) orig);
if (orig instanceof Calendar)
return copyCalendar((Calendar) orig);
ProxyBean proxy = getFactoryProxyBean(orig);
return (proxy == null) ? null : proxy.copy(orig);
}
|
public Date copyDate(Date orig) {
if (orig == null)
return null;
if (orig instanceof Proxy)
return (Date) ((Proxy) orig).copy(orig);
ProxyDate proxy = getFactoryProxyDate(orig.getClass());
return (Date) proxy.copy(orig);
}
|
public Map copyMap(Map orig) {
if (orig == null)
return null;
if (orig instanceof Proxy)
return (Map) ((Proxy) orig).copy(orig);
ProxyMap proxy = getFactoryProxyMap(orig.getClass());
return (Map) proxy.copy(orig);
}
|
protected Constructor findCopyConstructor(Class cls) {
Constructor[] cons = cls.getConstructors();
Constructor match = null;
Class matchParam = null;
Class[] params;
for (int i = 0; i < cons.length; i++) {
params = cons[i].getParameterTypes();
if (params.length != 1)
continue;
// quit immediately on exact match
if (params[0] == cls)
return cons[i];
if (params[0].isAssignableFrom(cls) && (matchParam == null
|| matchParam.isAssignableFrom(params[0]))) {
// track most derived collection constructor
match = cons[i];
matchParam = params[0];
}
}
return match;
}
Find an appropriate copy constructor for the given type, or return null
if none. |
protected Method findGetter(Class type,
Method setter) {
String name = setter.getName().substring(3);
Class param = setter.getParameterTypes()[0];
Method getter;
try {
getter = type.getMethod("get" + name, (Class[]) null);
if (getter.getReturnType().isAssignableFrom(param)
|| param.isAssignableFrom(getter.getReturnType()))
return getter;
} catch (NoSuchMethodException nsme) {
} catch (Exception e) {
throw new GeneralException(e);
}
if (param == boolean.class || param == Boolean.class) {
try {
getter = type.getMethod("is" + name, (Class[]) null);
if (getter.getReturnType().isAssignableFrom(param)
|| param.isAssignableFrom(getter.getReturnType()))
return getter;
} catch (NoSuchMethodException nsme) {
} catch (Exception e) {
throw new GeneralException(e);
}
}
return null;
}
Return the getter corresponding to the given setter, or null. |
protected BCClass generateProxyBeanBytecode(Class type,
boolean runtime) {
if (Modifier.isFinal(type.getModifiers()))
return null;
if (ImplHelper.isManagedType(null, type))
return null;
// we can only generate a valid proxy if there is a copy constructor
// or a default constructor
Constructor cons = findCopyConstructor(type);
if (cons == null) {
Constructor[] cs = type.getConstructors();
for (int i = 0; cons == null && i < cs.length; i++)
if (cs[i].getParameterTypes().length == 0)
cons = cs[i];
if (cons == null)
return null;
}
Project project = new Project();
BCClass bc = (BCClass) AccessController.doPrivileged(J2DoPrivHelper
.loadProjectClassAction(project, getProxyClassName(type, runtime)));
bc.setSuperclass(type);
bc.declareInterface(ProxyBean.class);
delegateConstructors(bc, type);
addProxyMethods(bc, true);
addProxyBeanMethods(bc, type, cons);
if (!proxySetters(bc, type))
return null;
addWriteReplaceMethod(bc, runtime);
return bc;
}
Generate the bytecode for a bean proxy for the given type. |
protected BCClass generateProxyCalendarBytecode(Class type,
boolean runtime) {
assertNotFinal(type);
Project project = new Project();
BCClass bc = (BCClass) AccessController.doPrivileged(J2DoPrivHelper
.loadProjectClassAction(project, getProxyClassName(type, runtime)));
bc.setSuperclass(type);
bc.declareInterface(ProxyCalendar.class);
delegateConstructors(bc, type);
addProxyMethods(bc, true);
addProxyCalendarMethods(bc, type);
proxySetters(bc, type);
addWriteReplaceMethod(bc, runtime);
return bc;
}
Generate the bytecode for a calendar proxy for the given type. |
protected BCClass generateProxyCollectionBytecode(Class type,
boolean runtime) {
assertNotFinal(type);
Project project = new Project();
BCClass bc = (BCClass) AccessController.doPrivileged(J2DoPrivHelper
.loadProjectClassAction(project, getProxyClassName(type, runtime)));
bc.setSuperclass(type);
bc.declareInterface(ProxyCollection.class);
delegateConstructors(bc, type);
addProxyMethods(bc, false);
addProxyCollectionMethods(bc, type);
proxyRecognizedMethods(bc, type, ProxyCollections.class,
ProxyCollection.class);
proxySetters(bc, type);
addWriteReplaceMethod(bc, runtime);
return bc;
}
Generate the bytecode for a collection proxy for the given type. |
protected BCClass generateProxyDateBytecode(Class type,
boolean runtime) {
assertNotFinal(type);
Project project = new Project();
BCClass bc = (BCClass) AccessController.doPrivileged(J2DoPrivHelper
.loadProjectClassAction(project, getProxyClassName(type, runtime)));
bc.setSuperclass(type);
bc.declareInterface(ProxyDate.class);
delegateConstructors(bc, type);
addProxyMethods(bc, true);
addProxyDateMethods(bc, type);
proxySetters(bc, type);
addWriteReplaceMethod(bc, runtime);
return bc;
}
Generate the bytecode for a date proxy for the given type. |
protected BCClass generateProxyMapBytecode(Class type,
boolean runtime) {
assertNotFinal(type);
Project project = new Project();
BCClass bc = (BCClass) AccessController.doPrivileged(J2DoPrivHelper
.loadProjectClassAction(project, getProxyClassName(type, runtime)));
bc.setSuperclass(type);
bc.declareInterface(ProxyMap.class);
delegateConstructors(bc, type);
addProxyMethods(bc, false);
addProxyMapMethods(bc, type);
proxyRecognizedMethods(bc, type, ProxyMaps.class, ProxyMap.class);
proxySetters(bc, type);
addWriteReplaceMethod(bc, runtime);
return bc;
}
Generate the bytecode for a map proxy for the given type. |
public boolean getAssertAllowedType() {
return _assertType;
}
Whether to perform runtime checks to ensure that all elements
added to collection and map proxies are the proper element/key/value
type as defined by the metadata. Defaults to false. |
public boolean getTrackChanges() {
return _trackChanges;
}
Whether proxies produced by this factory will use ChangeTracker s
to try to cut down on data store operations at the cost of some extra
bookkeeping overhead. Defaults to true. |
public Collection getUnproxyable() {
return _unproxyable;
}
Return a mutable view of class names we know cannot be proxied
correctly by this manager. |
protected boolean isOrdered(Class type) {
return List.class.isAssignableFrom(type)
|| "java.util.LinkedHashSet".equals(type.getName());
}
Return whether the given collection type maintains an artificial
ordering. |
protected boolean isSetter(Method meth) {
return startsWith(meth.getName(), "set")
|| startsWith(meth.getName(), "add")
|| startsWith(meth.getName(), "remove")
|| startsWith(meth.getName(), "insert")
|| startsWith(meth.getName(), "clear")
|| startsWith(meth.getName(), "roll"); // used by Calendar
}
Return whether the given method is a setter. |
protected boolean isUnproxyable(Class type) {
for (; type != null && type != Object.class;
type = type.getSuperclass()) {
if (_unproxyable.contains(type.getName()))
return true;
}
return false;
}
Return whether the given type is known to be unproxyable. |
protected Class loadBuildTimeProxy(Class type,
ClassLoader loader) {
try {
return Class.forName(getProxyClassName(type, false), true, loader);
} catch (Throwable t) {
return null;
}
}
Load the proxy class generated at build time for the given type,
returning null if none exists. |
public static void main(String[] args) throws IOException, ClassNotFoundException {
File dir = Files.getClassFile(ProxyManagerImpl.class);
dir = (dir == null) ? new File((String) AccessController.doPrivileged(
J2DoPrivHelper.getPropertyAction("user.dir")))
: dir.getParentFile();
Options opts = new Options();
args = opts.setFromCmdLine(args);
List types = new ArrayList();
types.addAll(Arrays.asList(args));
int utils = opts.removeIntProperty("utils", "u", 0);
if (utils >= 4) {
types.addAll(Arrays.asList(new String[] {
java.sql.Date.class.getName(),
java.sql.Time.class.getName(),
java.sql.Timestamp.class.getName(),
java.util.ArrayList.class.getName(),
java.util.Date.class.getName(),
java.util.GregorianCalendar.class.getName(),
java.util.HashMap.class.getName(),
java.util.HashSet.class.getName(),
java.util.Hashtable.class.getName(),
java.util.LinkedList.class.getName(),
java.util.Properties.class.getName(),
java.util.TreeMap.class.getName(),
java.util.TreeSet.class.getName(),
java.util.Vector.class.getName(),
}));
}
if (utils >= 5) {
types.addAll(Arrays.asList(new String[] {
"java.util.EnumMap",
"java.util.IdentityHashMap",
"java.util.LinkedHashMap",
"java.util.LinkedHashSet",
"java.util.PriorityQueue",
}));
}
final ProxyManagerImpl mgr = new ProxyManagerImpl();
Class cls;
BCClass bc;
for (int i = 0; i < types.size(); i++) {
cls = Class.forName((String) types.get(i));
try {
if (Class.forName(getProxyClassName(cls, false), true,
GeneratedClasses.getMostDerivedLoader(cls, Proxy.class))
!= null)
continue;
} catch (Throwable t) {
// expected if the class hasn't been generated
}
if (Collection.class.isAssignableFrom(cls))
bc = mgr.generateProxyCollectionBytecode(cls, false);
else if (Map.class.isAssignableFrom(cls))
bc = mgr.generateProxyMapBytecode(cls, false);
else if (Date.class.isAssignableFrom(cls))
bc = mgr.generateProxyDateBytecode(cls, false);
else if (Calendar.class.isAssignableFrom(cls))
bc = mgr.generateProxyCalendarBytecode(cls, false);
else {
final Class fCls = cls;
bc = (BCClass) AccessController
.doPrivileged(new PrivilegedAction() {
public Object run() {
return mgr.generateProxyBeanBytecode(fCls, false);
}
});
}
System.out.println(bc.getName());
bc.write(new File(dir, bc.getClassName() + ".class"));
}
}
Usage: java org.apache.openjpa.util.proxy.ProxyManagerImpl [option]*
<class name>+
Where the following options are recognized:
- -utils/-u <number>: Generate proxies for the standard
java.util collection, map, date, and calendar classes of the given Java
version. Use 4 for Java 1.4, 5 for Java 5, etc.
The main method generates .class files for the proxies to the classes
given on the command line. It writes the generated classes to beside the
ProxyManagerImpl.class file if possible; otherwise it writes to the
current directory. The proxy manager looks for these classes
before generating its own proxies at runtime. |
public Proxy newCalendarProxy(Class type,
TimeZone zone) {
if (type == Calendar.class)
type = GregorianCalendar.class;
ProxyCalendar proxy = getFactoryProxyCalendar(type);
ProxyCalendar cal = proxy.newInstance();
if (zone != null)
((Calendar) cal).setTimeZone(zone);
return cal;
}
|
public Proxy newCollectionProxy(Class type,
Class elementType,
Comparator compare) {
type = toProxyableCollectionType(type);
ProxyCollection proxy = getFactoryProxyCollection(type);
return proxy.newInstance((_assertType) ? elementType : null, compare,
_trackChanges);
}
|
public Proxy newCustomProxy(Object orig) {
if (orig == null)
return null;
if (orig instanceof Proxy)
return (Proxy) orig;
if (ImplHelper.isManageable(orig))
return null;
if (orig instanceof Collection) {
Comparator comp = (orig instanceof SortedSet)
? ((SortedSet) orig).comparator() : null;
Collection c = (Collection) newCollectionProxy(orig.getClass(),
null, comp);
c.addAll((Collection) orig);
return (Proxy) c;
}
if (orig instanceof Map) {
Comparator comp = (orig instanceof SortedMap)
? ((SortedMap) orig).comparator() : null;
Map m = (Map) newMapProxy(orig.getClass(), null, null, comp);
m.putAll((Map) orig);
return (Proxy) m;
}
if (orig instanceof Date) {
Date d = (Date) newDateProxy(orig.getClass());
d.setTime(((Date) orig).getTime());
if (orig instanceof Timestamp)
((Timestamp) d).setNanos(((Timestamp) orig).getNanos());
return (Proxy) d;
}
if (orig instanceof Calendar) {
Calendar c = (Calendar) newCalendarProxy(orig.getClass(),
((Calendar) orig).getTimeZone());
c.setTimeInMillis(((Calendar) orig).getTimeInMillis());
return (Proxy) c;
}
ProxyBean proxy = getFactoryProxyBean(orig);
return (proxy == null) ? null : proxy.newInstance(orig);
}
|
public Proxy newDateProxy(Class type) {
ProxyDate proxy = getFactoryProxyDate(type);
return proxy.newInstance();
}
|
public Proxy newMapProxy(Class type,
Class keyType,
Class elementType,
Comparator compare) {
type = toProxyableMapType(type);
ProxyMap proxy = getFactoryProxyMap(type);
return proxy.newInstance((_assertType) ? keyType : null,
(_assertType) ? elementType : null, compare, _trackChanges);
}
|
public void setAssertAllowedType(boolean assertType) {
_assertType = assertType;
}
Whether to perform runtime checks to ensure that all elements
added to collection and map proxies are the proper element/key/value
type as defined by the metadata. Defaults to false. |
public void setTrackChanges(boolean track) {
_trackChanges = track;
}
Whether proxies produced by this factory will use ChangeTracker s
to try to cut down on data store operations at the cost of some extra
bookkeeping overhead. Defaults to true. |
public void setUnproxyable(String clsNames) {
if (clsNames != null)
_unproxyable.addAll(Arrays.asList(Strings.split(clsNames, ";", 0)));
}
Provided for auto-configuration. Add the given semicolon-separated
class names to the set of class names we know cannot be proxied correctly
by this manager. |
protected Class toProxyableCollectionType(Class type) {
if (type.getName().endsWith(PROXY_SUFFIX))
type = type.getSuperclass();
else if (type.isInterface()) {
type = toConcreteType(type, _stdCollections);
if (type == null)
throw new UnsupportedException(_loc.get("no-proxy-intf", type));
} else if (Modifier.isAbstract(type.getModifiers()))
throw new UnsupportedException(_loc.get("no-proxy-abstract", type));
return type;
}
Return the concrete type for proxying. |
protected Class toProxyableMapType(Class type) {
if (type.getName().endsWith(PROXY_SUFFIX))
type = type.getSuperclass();
else if (type.isInterface()) {
type = toConcreteType(type, _stdMaps);
if (type == null)
throw new UnsupportedException(_loc.get("no-proxy-intf", type));
} else if (Modifier.isAbstract(type.getModifiers()))
throw new UnsupportedException(_loc.get("no-proxy-abstract", type));
return type;
}
Return the concrete type for proxying. |