use of org.codehaus.groovy.reflection.CachedClass in project groovy by apache.
the class MetaClassHelper method chooseMostGeneralMethodWith1NullParam.
/**
* Warning: this method does not choose properly if multiple methods with
* the same distance are encountered
* @param methods the methods to choose from
* @return the method with 1 parameter which takes the most general type of
* object (e.g. Object) ignoring primitive types
* @deprecated
*/
@Deprecated
public static Object chooseMostGeneralMethodWith1NullParam(FastArray methods) {
// let's look for methods with 1 argument which matches the type of the
// arguments
CachedClass closestClass = null;
CachedClass closestVargsClass = null;
Object answer = null;
int closestDist = -1;
final int len = methods.size();
for (int i = 0; i != len; ++i) {
final Object[] data = methods.getArray();
Object method = data[i];
final ParameterTypes pt = (ParameterTypes) method;
CachedClass[] paramTypes = pt.getParameterTypes();
int paramLength = paramTypes.length;
if (paramLength == 0 || paramLength > 2)
continue;
CachedClass theType = paramTypes[0];
if (theType.isPrimitive)
continue;
if (paramLength == 2) {
if (!pt.isVargsMethod(ARRAY_WITH_NULL))
continue;
if (closestClass == null) {
closestVargsClass = paramTypes[1];
closestClass = theType;
answer = method;
} else if (closestClass.getTheClass() == theType.getTheClass()) {
if (closestVargsClass == null)
continue;
CachedClass newVargsClass = paramTypes[1];
if (isAssignableFrom(newVargsClass.getTheClass(), closestVargsClass.getTheClass())) {
closestVargsClass = newVargsClass;
answer = method;
}
} else if (isAssignableFrom(theType.getTheClass(), closestClass.getTheClass())) {
closestVargsClass = paramTypes[1];
closestClass = theType;
answer = method;
}
} else {
if (closestClass == null || isAssignableFrom(theType.getTheClass(), closestClass.getTheClass())) {
closestVargsClass = null;
closestClass = theType;
answer = method;
closestDist = -1;
} else {
// to check the distance to Object
if (closestDist == -1)
closestDist = closestClass.getSuperClassDistance();
int newDist = theType.getSuperClassDistance();
if (newDist < closestDist) {
closestDist = newDist;
closestVargsClass = null;
closestClass = theType;
answer = method;
}
}
}
}
return answer;
}
use of org.codehaus.groovy.reflection.CachedClass in project gradle by gradle.
the class StringToEnumTransformer method transform.
@Override
public Object[] transform(CachedClass[] types, Object[] args) {
boolean needsTransform = false;
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
Class type = types[i].getTheClass();
if (type.isInstance(arg) || arg == null) {
// Can use arg without conversion
continue;
}
if (!(arg instanceof CharSequence && type.isEnum())) {
// Cannot convert
return args;
}
needsTransform = true;
}
if (!needsTransform) {
return args;
}
Object[] transformed = new Object[args.length];
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
Class type = types[i].getTheClass();
if (type.isEnum() && arg instanceof CharSequence) {
transformed[i] = toEnumValue(type, (CharSequence) arg);
} else {
transformed[i] = args[i];
}
}
return transformed;
}
use of org.codehaus.groovy.reflection.CachedClass in project grails-core by grails.
the class BaseApiProvider method addApi.
@SuppressWarnings("unchecked")
public void addApi(final Object apiInstance) {
if (apiInstance == null) {
return;
}
Class<?> currentClass = apiInstance.getClass();
while (currentClass != Object.class) {
final Method[] declaredMethods = currentClass.getDeclaredMethods();
for (final Method javaMethod : declaredMethods) {
final int modifiers = javaMethod.getModifiers();
if (!isNotExcluded(javaMethod, modifiers)) {
continue;
}
if (Modifier.isStatic(modifiers)) {
if (isConstructorCallMethod(javaMethod)) {
constructors.add(javaMethod);
} else {
staticMethods.add(javaMethod);
}
} else {
instanceMethods.add(new ReflectionMetaMethod(new CachedMethod(javaMethod)) {
{
CachedClass[] paramTypes = super.getParameterTypes();
if (paramTypes.length > 0) {
setParametersTypes((CachedClass[]) GrailsArrayUtils.subarray(paramTypes, 1, paramTypes.length));
}
}
@Override
public String getName() {
String methodName = super.getName();
if (isConstructorCallMethod(javaMethod)) {
return CTOR_GROOVY_METHOD;
}
return methodName;
}
@Override
public Object invoke(Object object, Object[] arguments) {
if (arguments.length == 0) {
return super.invoke(apiInstance, new Object[] { object });
}
return super.invoke(apiInstance, (Object[]) GrailsArrayUtils.add(checkForGStrings(arguments), 0, object));
}
private Object[] checkForGStrings(Object[] arguments) {
for (int i = 0; i < arguments.length; i++) {
if (arguments[i] instanceof GString) {
arguments[i] = arguments[i].toString();
}
}
return arguments;
}
@Override
public CachedClass[] getParameterTypes() {
final CachedClass[] paramTypes = method.getParameterTypes();
if (paramTypes.length > 0) {
return (CachedClass[]) GrailsArrayUtils.subarray(paramTypes, 1, paramTypes.length);
}
return paramTypes;
}
});
}
}
currentClass = currentClass.getSuperclass();
}
}
use of org.codehaus.groovy.reflection.CachedClass in project grails-core by grails.
the class DefaultGrailsApplication method initialiseGroovyExtensionModules.
protected static void initialiseGroovyExtensionModules() {
if (extensionMethodsInitialized)
return;
extensionMethodsInitialized = true;
Map<CachedClass, List<MetaMethod>> map = new HashMap<CachedClass, List<MetaMethod>>();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
try {
Enumeration<URL> resources = classLoader.getResources(ExtensionModuleScanner.MODULE_META_INF_FILE);
while (resources.hasMoreElements()) {
URL url = resources.nextElement();
if (url.getPath().contains("groovy-all")) {
// already registered
continue;
}
Properties properties = new Properties();
InputStream inStream = null;
try {
inStream = url.openStream();
properties.load(inStream);
((MetaClassRegistryImpl) GroovySystem.getMetaClassRegistry()).registerExtensionModuleFromProperties(properties, classLoader, map);
} catch (IOException e) {
throw new GroovyRuntimeException("Unable to load module META-INF descriptor", e);
} finally {
if (inStream != null) {
inStream.close();
}
}
}
} catch (IOException ignored) {
}
for (Map.Entry<CachedClass, List<MetaMethod>> moduleMethods : map.entrySet()) {
CachedClass cls = moduleMethods.getKey();
cls.addNewMopMethods(moduleMethods.getValue());
}
}
use of org.codehaus.groovy.reflection.CachedClass in project groovy-core by groovy.
the class MetaClassImpl method getCategoryMethodGetter.
private MetaMethod getCategoryMethodGetter(Class sender, String name, boolean useLongVersion) {
List possibleGenericMethods = GroovyCategorySupport.getCategoryMethods(name);
if (possibleGenericMethods != null) {
for (Iterator iter = possibleGenericMethods.iterator(); iter.hasNext(); ) {
MetaMethod mmethod = (MetaMethod) iter.next();
if (!mmethod.getDeclaringClass().getTheClass().isAssignableFrom(sender))
continue;
CachedClass[] paramTypes = mmethod.getParameterTypes();
if (useLongVersion) {
if (paramTypes.length == 1 && paramTypes[0].getTheClass() == String.class) {
return mmethod;
}
} else {
if (paramTypes.length == 0)
return mmethod;
}
}
}
return null;
}
Aggregations