Search in sources :

Example 21 with CachedClass

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;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass) ParameterTypes(org.codehaus.groovy.reflection.ParameterTypes)

Example 22 with CachedClass

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;
}
Also used : CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 23 with CachedClass

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();
    }
}
Also used : CachedMethod(org.codehaus.groovy.reflection.CachedMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) ReflectionMetaMethod(org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod) Method(java.lang.reflect.Method) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) GString(groovy.lang.GString) GString(groovy.lang.GString) ReflectionMetaMethod(org.codehaus.groovy.runtime.metaclass.ReflectionMetaMethod)

Example 24 with CachedClass

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());
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) InputStream(java.io.InputStream) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) CachedClass(org.codehaus.groovy.reflection.CachedClass) IOException(java.io.IOException) URL(java.net.URL) MetaClassRegistryImpl(org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl) GroovyClassLoader(groovy.lang.GroovyClassLoader)

Example 25 with CachedClass

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;
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Aggregations

CachedClass (org.codehaus.groovy.reflection.CachedClass)68 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)17 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)17 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)15 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)15 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)15 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)13 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)13 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)13 SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)10 MetaMethodIndex (org.codehaus.groovy.runtime.metaclass.MetaMethodIndex)8 ClassWriter (org.objectweb.asm.ClassWriter)6 ParameterTypes (org.codehaus.groovy.reflection.ParameterTypes)4 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)4 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)4 MetaMethod (groovy.lang.MetaMethod)3 FastArray (org.codehaus.groovy.util.FastArray)3 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)2 ConvertedClosure (org.codehaus.groovy.runtime.ConvertedClosure)2 CurriedClosure (org.codehaus.groovy.runtime.CurriedClosure)2