blob: c0a48b7567c2a6a6864434be8d55868d019ac4bd [file] [log] [blame]
/*
* Copyright 2003-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.codehaus.groovy.reflection;
import java.lang.ref.SoftReference;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
public class ReflectionCache {
private static Map primitiveTypesMap = new HashMap();
static {
primitiveTypesMap.put(byte.class, Byte.class);
primitiveTypesMap.put(boolean.class, Boolean.class);
primitiveTypesMap.put(char.class, Character.class);
primitiveTypesMap.put(double.class, Double.class);
primitiveTypesMap.put(float.class, Float.class);
primitiveTypesMap.put(int.class, Integer.class);
primitiveTypesMap.put(long.class, Long.class);
primitiveTypesMap.put(short.class, Short.class);
}
public static Class autoboxType(Class type) {
final Class res = (Class) primitiveTypesMap.get(type);
return res == null ? type : res;
/*
final String name = type.getName();
switch (name.charAt(0)) {
case 'b':
if ("boolean".equals(name))
return Boolean.class;
else
if ("byte".equals(name))
return Byte.class;
else
return null;
case 'c':
return "char".equals(name) ? Character.class : null;
case 'd':
return "double".equals(name) ? Double.class : null;
case 'f':
return "float".equals(name) ? Float.class : null;
case 'i':
return "int".equals(name) ? Integer.class : null;
case 'l':
return "long".equals(name) ? Long.class : null;
case 's':
return "short".equals(name) ? Short.class : null;
default:
return null;
}
*/
}
static TripleKeyHashMap mopNames = new TripleKeyHashMap() {
public boolean checkEquals(Entry e, Object key1, Object key2, Object key3) {
MopNameEntry ee = (MopNameEntry) e;
return ee.key1 == key1 && ee.key2.equals(key2) && ee.key3 == key3;
}
public Entry createEntry() {
return new MopNameEntry();
}
};
private static class MopNameEntry extends TripleKeyHashMap.Entry {
String value;
}
public static String getMOPMethodName(Class declaringClass, String name, boolean useThis) {
MopNameEntry mopNameEntry = (MopNameEntry) mopNames.getOrPut(declaringClass, name, Boolean.valueOf(useThis));
if (mopNameEntry.value == null) {
mopNameEntry.value = new StringBuffer().append(useThis ? "this$" : "super$").append(getSuperClassDistance(declaringClass)).append("$").append(name).toString();
}
return mopNameEntry.value;
}
static final Map /*<Class,SoftReference<CachedClass>>*/ CACHED_CLASS_MAP = new WeakHashMap();
private static Map CACHED_METHOD_MAP = new WeakHashMap ();
private static final Map CACHED_CONSTRUCTOR_MAP = new WeakHashMap ();
public static boolean isArray(Class klazz) {
CachedClass cachedClass = getCachedClass(klazz);
return cachedClass.isArray;
}
static DoubleKeyHashMap assignableMap = new DoubleKeyHashMap() {
public boolean checkEquals(ComplexKeyHashMap.Entry e, Object key1, Object key2) {
IsAssignableEntry ee = (IsAssignableEntry) e;
return ee.key1 == key1 && ee.key2 == key2;
}
public Entry createEntry() {
return new IsAssignableEntry();
}
};
public static class IsAssignableEntry extends DoubleKeyHashMap.Entry {
Boolean value;
}
public static boolean isAssignableFrom(Class klazz, Class aClass) {
IsAssignableEntry val = (IsAssignableEntry) assignableMap.getOrPut(klazz, aClass);
if (val.value == null) {
val.value = Boolean.valueOf(klazz.isAssignableFrom(aClass));
}
return val.value.booleanValue();
}
static boolean arrayContentsEq(Object[] a1, Object[] a2) {
if (a1 == null) {
return a2 == null || a2.length == 0;
}
if (a2 == null) {
return a1.length == 0;
}
if (a1.length != a2.length) {
return false;
}
for (int i = 0; i < a1.length; i++) {
if (a1[i] != a2[i]) {
return false;
}
}
return true;
}
static int getSuperClassDistance(Class klazz) {
CachedClass cachedClass = getCachedClass(klazz);
synchronized (cachedClass) {
if (cachedClass.distance == -1) {
int distance = 0;
for (; klazz != null; klazz = klazz.getSuperclass())
distance++;
cachedClass.distance = distance;
}
return cachedClass.distance;
}
}
private static final CachedClass OBJECT_CLASS = new CachedClass(Object.class) {
public synchronized CachedClass getCachedSuperClass() {
return null;
}
};
public static CachedClass getCachedClass(Class klazz) {
if (klazz == null)
return null;
if (klazz == Object.class)
return OBJECT_CLASS;
CachedClass cachedClass;
synchronized (CACHED_CLASS_MAP) {
SoftReference ref = (SoftReference) CACHED_CLASS_MAP.get(klazz);
if (ref == null || (cachedClass = (CachedClass) ref.get()) == null) {
cachedClass = new CachedClass(klazz);
CACHED_CLASS_MAP.put(klazz, new SoftReference(cachedClass));
}
}
return cachedClass;
}
public static CachedMethod getCachedMethod(Method method) {
CachedMethod cachedMethod;
synchronized (CACHED_METHOD_MAP) {
SoftReference ref = (SoftReference) CACHED_METHOD_MAP.get(method);
if (ref == null || (cachedMethod = (CachedMethod) ref.get()) == null) {
cachedMethod = new CachedMethod(method);
CACHED_METHOD_MAP.put(method, new SoftReference(cachedMethod));
}
}
return cachedMethod;
}
public static CachedConstructor getCachedConstructor(Constructor c) {
CachedConstructor cachedConstructor;
synchronized (CACHED_CONSTRUCTOR_MAP) {
SoftReference ref = (SoftReference) CACHED_CONSTRUCTOR_MAP.get(c);
if (ref == null || (cachedConstructor = (CachedConstructor) ref.get()) == null) {
cachedConstructor = new CachedConstructor(c);
CACHED_CONSTRUCTOR_MAP.put(c, new SoftReference(cachedConstructor));
}
}
return cachedConstructor;
}
public static CachedMethod[] getDeclaredMethodsCached(final Class klazz) {
return getCachedClass(klazz).getMethods();
}
}