Search in sources :

Example 1 with GeneratedMetaMethod

use of org.codehaus.groovy.reflection.GeneratedMetaMethod in project groovy-core by groovy.

the class DgmConverter method main.

public static void main(String[] args) throws IOException, ClassNotFoundException {
    String targetDirectory = "target/classes/";
    boolean info = (args.length == 1 && args[0].equals("--info")) || (args.length == 2 && args[0].equals("--info"));
    if (info && args.length == 2) {
        targetDirectory = args[1];
        if (!targetDirectory.endsWith("/"))
            targetDirectory += "/";
    }
    List<CachedMethod> cachedMethodsList = new ArrayList<CachedMethod>();
    for (Class aClass : DefaultGroovyMethods.DGM_LIKE_CLASSES) {
        Collections.addAll(cachedMethodsList, ReflectionCache.getCachedClass(aClass).getMethods());
    }
    final CachedMethod[] cachedMethods = cachedMethodsList.toArray(new CachedMethod[cachedMethodsList.size()]);
    List<GeneratedMetaMethod.DgmMethodRecord> records = new ArrayList<GeneratedMetaMethod.DgmMethodRecord>();
    int cur = 0;
    for (CachedMethod method : cachedMethods) {
        if (!method.isStatic() || !method.isPublic())
            continue;
        if (method.getCachedMethod().getAnnotation(Deprecated.class) != null)
            continue;
        if (method.getParameterTypes().length == 0)
            continue;
        final Class returnType = method.getReturnType();
        final String className = "org/codehaus/groovy/runtime/dgm$" + cur++;
        GeneratedMetaMethod.DgmMethodRecord record = new GeneratedMetaMethod.DgmMethodRecord();
        records.add(record);
        record.methodName = method.getName();
        record.returnType = method.getReturnType();
        record.parameters = method.getNativeParameterTypes();
        record.className = className;
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        cw.visit(V1_3, ACC_PUBLIC, className, null, "org/codehaus/groovy/reflection/GeneratedMetaMethod", null);
        createConstructor(cw);
        final String methodDescriptor = BytecodeHelper.getMethodDescriptor(returnType, method.getNativeParameterTypes());
        createInvokeMethod(method, cw, returnType, methodDescriptor);
        createDoMethodInvokeMethod(method, cw, className, returnType, methodDescriptor);
        createIsValidMethodMethod(method, cw, className);
        cw.visitEnd();
        final byte[] bytes = cw.toByteArray();
        final FileOutputStream fileOutputStream = new FileOutputStream(targetDirectory + className + ".class");
        fileOutputStream.write(bytes);
        fileOutputStream.flush();
        fileOutputStream.close();
    }
    GeneratedMetaMethod.DgmMethodRecord.saveDgmInfo(records, targetDirectory + "/META-INF/dgminfo");
    if (info)
        System.out.println("Saved " + cur + " dgm records to: " + targetDirectory + "/META-INF/dgminfo");
}
Also used : ArrayList(java.util.ArrayList) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) ClassWriter(org.objectweb.asm.ClassWriter) FileOutputStream(java.io.FileOutputStream) CachedClass(org.codehaus.groovy.reflection.CachedClass) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod)

Example 2 with GeneratedMetaMethod

use of org.codehaus.groovy.reflection.GeneratedMetaMethod in project groovy-core by groovy.

the class MetaClassImpl method getProperties.

/**
     * Get all the properties defined for this type
     *
     * @return a list of MetaProperty objects
     */
public List<MetaProperty> getProperties() {
    checkInitalised();
    SingleKeyHashMap propertyMap = classPropertyIndex.getNullable(theCachedClass);
    if (propertyMap == null) {
        // GROOVY-6903: May happen in some special environment, like under Android, due
        // to classloading issues
        propertyMap = new SingleKeyHashMap();
    }
    // simply return the values of the metaproperty map as a List
    List ret = new ArrayList(propertyMap.size());
    for (ComplexKeyHashMap.EntryIterator iter = propertyMap.getEntrySetIterator(); iter.hasNext(); ) {
        MetaProperty element = (MetaProperty) ((SingleKeyHashMap.Entry) iter.next()).value;
        if (element instanceof CachedField)
            continue;
        // filter out DGM beans
        if (element instanceof MetaBeanProperty) {
            MetaBeanProperty mp = (MetaBeanProperty) element;
            boolean setter = true;
            boolean getter = true;
            if (mp.getGetter() == null || mp.getGetter() instanceof GeneratedMetaMethod || mp.getGetter() instanceof NewInstanceMetaMethod) {
                getter = false;
            }
            if (mp.getSetter() == null || mp.getSetter() instanceof GeneratedMetaMethod || mp.getSetter() instanceof NewInstanceMetaMethod) {
                setter = false;
            }
            if (!setter && !getter)
                continue;
        //  TODO: I (ait) don't know why these strange tricks needed and comment following as it effects some Grails tests
        //                if (!setter && mp.getSetter() != null) {
        //                    element = new MetaBeanProperty(mp.getName(), mp.getType(), mp.getGetter(), null);
        //                }
        //                if (!getter && mp.getGetter() != null) {
        //                    element = new MetaBeanProperty(mp.getName(), mp.getType(), null, mp.getSetter());
        //                }
        }
        ret.add(element);
    }
    return ret;
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) CachedField(org.codehaus.groovy.reflection.CachedField)

Example 3 with GeneratedMetaMethod

use of org.codehaus.groovy.reflection.GeneratedMetaMethod in project groovy by apache.

the class DgmConverter method main.

public static void main(String[] args) throws IOException {
    String targetDirectory = "target/classes/";
    boolean info = (args.length == 1 && args[0].equals("--info")) || (args.length == 2 && args[0].equals("--info"));
    if (info && args.length == 2) {
        targetDirectory = args[1];
        if (!targetDirectory.endsWith("/"))
            targetDirectory += "/";
    }
    List<CachedMethod> cachedMethodsList = new ArrayList<CachedMethod>();
    for (Class aClass : DefaultGroovyMethods.DGM_LIKE_CLASSES) {
        Collections.addAll(cachedMethodsList, ReflectionCache.getCachedClass(aClass).getMethods());
    }
    final CachedMethod[] cachedMethods = cachedMethodsList.toArray(new CachedMethod[cachedMethodsList.size()]);
    List<GeneratedMetaMethod.DgmMethodRecord> records = new ArrayList<GeneratedMetaMethod.DgmMethodRecord>();
    int cur = 0;
    for (CachedMethod method : cachedMethods) {
        if (!method.isStatic() || !method.isPublic())
            continue;
        if (method.getCachedMethod().getAnnotation(Deprecated.class) != null)
            continue;
        if (method.getParameterTypes().length == 0)
            continue;
        final Class returnType = method.getReturnType();
        final String className = "org/codehaus/groovy/runtime/dgm$" + cur++;
        GeneratedMetaMethod.DgmMethodRecord record = new GeneratedMetaMethod.DgmMethodRecord();
        records.add(record);
        record.methodName = method.getName();
        record.returnType = method.getReturnType();
        record.parameters = method.getNativeParameterTypes();
        record.className = className;
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
        cw.visit(V1_3, ACC_PUBLIC, className, null, "org/codehaus/groovy/reflection/GeneratedMetaMethod", null);
        createConstructor(cw);
        final String methodDescriptor = BytecodeHelper.getMethodDescriptor(returnType, method.getNativeParameterTypes());
        createInvokeMethod(method, cw, returnType, methodDescriptor);
        createDoMethodInvokeMethod(method, cw, className, returnType, methodDescriptor);
        createIsValidMethodMethod(method, cw, className);
        cw.visitEnd();
        final byte[] bytes = cw.toByteArray();
        final FileOutputStream fileOutputStream = new FileOutputStream(targetDirectory + className + ".class");
        fileOutputStream.write(bytes);
        fileOutputStream.flush();
        fileOutputStream.close();
    }
    GeneratedMetaMethod.DgmMethodRecord.saveDgmInfo(records, targetDirectory + "/META-INF/dgminfo");
    if (info)
        System.out.println("Saved " + cur + " dgm records to: " + targetDirectory + "/META-INF/dgminfo");
}
Also used : ArrayList(java.util.ArrayList) CachedMethod(org.codehaus.groovy.reflection.CachedMethod) ClassWriter(org.objectweb.asm.ClassWriter) FileOutputStream(java.io.FileOutputStream) CachedClass(org.codehaus.groovy.reflection.CachedClass) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod)

Example 4 with GeneratedMetaMethod

use of org.codehaus.groovy.reflection.GeneratedMetaMethod in project groovy by apache.

the class MetaClassImpl method getProperties.

/**
     * Get all the properties defined for this type
     *
     * @return a list of MetaProperty objects
     */
public List<MetaProperty> getProperties() {
    checkInitalised();
    SingleKeyHashMap propertyMap = classPropertyIndex.getNullable(theCachedClass);
    if (propertyMap == null) {
        // GROOVY-6903: May happen in some special environment, like under Android, due
        // to classloading issues
        propertyMap = new SingleKeyHashMap();
    }
    // simply return the values of the metaproperty map as a List
    List ret = new ArrayList(propertyMap.size());
    for (ComplexKeyHashMap.EntryIterator iter = propertyMap.getEntrySetIterator(); iter.hasNext(); ) {
        MetaProperty element = (MetaProperty) ((SingleKeyHashMap.Entry) iter.next()).value;
        if (element instanceof CachedField)
            continue;
        // filter out DGM beans
        if (element instanceof MetaBeanProperty) {
            MetaBeanProperty mp = (MetaBeanProperty) element;
            boolean setter = true;
            boolean getter = true;
            if (mp.getGetter() == null || mp.getGetter() instanceof GeneratedMetaMethod || mp.getGetter() instanceof NewInstanceMetaMethod) {
                getter = false;
            }
            if (mp.getSetter() == null || mp.getSetter() instanceof GeneratedMetaMethod || mp.getSetter() instanceof NewInstanceMetaMethod) {
                setter = false;
            }
            if (!setter && !getter)
                continue;
        //  TODO: I (ait) don't know why these strange tricks needed and comment following as it effects some Grails tests
        //                if (!setter && mp.getSetter() != null) {
        //                    element = new MetaBeanProperty(mp.getName(), mp.getType(), mp.getGetter(), null);
        //                }
        //                if (!getter && mp.getGetter() != null) {
        //                    element = new MetaBeanProperty(mp.getName(), mp.getType(), null, mp.getSetter());
        //                }
        }
        ret.add(element);
    }
    return ret;
}
Also used : SingleKeyHashMap(org.codehaus.groovy.util.SingleKeyHashMap) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) ComplexKeyHashMap(org.codehaus.groovy.util.ComplexKeyHashMap) GetMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty) GetBeanMethodMetaProperty(org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) CachedField(org.codehaus.groovy.reflection.CachedField)

Aggregations

GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)4 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 CachedClass (org.codehaus.groovy.reflection.CachedClass)2 CachedField (org.codehaus.groovy.reflection.CachedField)2 CachedMethod (org.codehaus.groovy.reflection.CachedMethod)2 GetBeanMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetBeanMethodMetaProperty)2 GetMethodMetaProperty (org.codehaus.groovy.runtime.metaclass.MethodMetaProperty.GetMethodMetaProperty)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 ComplexKeyHashMap (org.codehaus.groovy.util.ComplexKeyHashMap)2 SingleKeyHashMap (org.codehaus.groovy.util.SingleKeyHashMap)2 ClassWriter (org.objectweb.asm.ClassWriter)2