Search in sources :

Example 21 with MetaClass

use of groovy.lang.MetaClass in project groovy-core by groovy.

the class GroovyClassLoaderTest method testCompile.

public void testCompile() throws Exception {
    Class groovyClass = loader.parseClass(new File("src/test/org/codehaus/groovy/classgen/Main.groovy"));
    System.out.println("Invoking main...");
    GroovyObject object = (GroovyObject) groovyClass.newInstance();
    assertTrue(object != null);
    MetaClass metaClass = object.getMetaClass();
    System.out.println("Metaclass: " + metaClass);
    Class type = object.getClass();
    System.out.println("Type: " + type);
    // invoke via metaclass
    metaClass.invokeMethod(object, "main", null);
    // invoke directly
    object.invokeMethod("main", null);
}
Also used : MetaClass(groovy.lang.MetaClass) MetaClass(groovy.lang.MetaClass) File(java.io.File) GroovyObject(groovy.lang.GroovyObject)

Example 22 with MetaClass

use of groovy.lang.MetaClass in project groovy-core by groovy.

the class JO method getStaticMetaClass.

MetaClass getStaticMetaClass(Object obj) {
    MetaClass mc;
    if (staticMetaClass == null || (mc = (MetaClass) staticMetaClass.get()) == null) {
        mc = InvokerHelper.getMetaClass(obj);
        staticMetaClass = new SoftReference(mc);
    }
    return mc;
}
Also used : MetaClass(groovy.lang.MetaClass) SoftReference(java.lang.ref.SoftReference)

Example 23 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class Verifier method addStaticMetaClassField.

private void addStaticMetaClassField(final ClassNode node, final String classInternalName) {
    String _staticClassInfoFieldName = "$staticClassInfo";
    while (node.getDeclaredField(_staticClassInfoFieldName) != null) _staticClassInfoFieldName = _staticClassInfoFieldName + "$";
    final String staticMetaClassFieldName = _staticClassInfoFieldName;
    FieldNode staticMetaClassField = node.addField(staticMetaClassFieldName, ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC, ClassHelper.make(ClassInfo.class, false), null);
    staticMetaClassField.setSynthetic(true);
    node.addSyntheticMethod("$getStaticMetaClass", ACC_PROTECTED, ClassHelper.make(MetaClass.class), Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, new BytecodeSequence(new BytecodeInstruction() {

        @Override
        public void visit(final MethodVisitor mv) {
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
            if (BytecodeHelper.isClassLiteralPossible(node) || BytecodeHelper.isSameCompilationUnit(classNode, node)) {
                BytecodeHelper.visitClassLiteral(mv, node);
            } else {
                mv.visitMethodInsn(INVOKESTATIC, classInternalName, "$get$$class$" + classInternalName.replace('/', '$'), "()Ljava/lang/Class;", false);
            }
            Label l1 = new Label();
            mv.visitJumpInsn(IF_ACMPEQ, l1);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/runtime/ScriptBytecodeAdapter", "initMetaClass", "(Ljava/lang/Object;)Lgroovy/lang/MetaClass;", false);
            mv.visitInsn(ARETURN);
            mv.visitLabel(l1);
            mv.visitFieldInsn(GETSTATIC, classInternalName, staticMetaClassFieldName, "Lorg/codehaus/groovy/reflection/ClassInfo;");
            mv.visitVarInsn(ASTORE, 1);
            mv.visitVarInsn(ALOAD, 1);
            Label l0 = new Label();
            mv.visitJumpInsn(IFNONNULL, l0);
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
            mv.visitMethodInsn(INVOKESTATIC, "org/codehaus/groovy/reflection/ClassInfo", "getClassInfo", "(Ljava/lang/Class;)Lorg/codehaus/groovy/reflection/ClassInfo;", false);
            mv.visitInsn(DUP);
            mv.visitVarInsn(ASTORE, 1);
            mv.visitFieldInsn(PUTSTATIC, classInternalName, staticMetaClassFieldName, "Lorg/codehaus/groovy/reflection/ClassInfo;");
            mv.visitLabel(l0);
            mv.visitVarInsn(ALOAD, 1);
            mv.visitMethodInsn(INVOKEVIRTUAL, "org/codehaus/groovy/reflection/ClassInfo", "getMetaClass", "()Lgroovy/lang/MetaClass;", false);
            mv.visitInsn(ARETURN);
        }
    }));
}
Also used : FieldNode(org.codehaus.groovy.ast.FieldNode) MetaClass(groovy.lang.MetaClass) Label(org.objectweb.asm.Label) ClassInfo(org.codehaus.groovy.reflection.ClassInfo) MethodVisitor(org.objectweb.asm.MethodVisitor)

Example 24 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class CachedClass method getNewMetaMethods.

public MetaMethod[] getNewMetaMethods() {
    List<MetaMethod> arr = new ArrayList<>(Arrays.asList(classInfo.newMetaMethods));
    final MetaClass metaClass = classInfo.getStrongMetaClass();
    if (metaClass instanceof ExpandoMetaClass) {
        arr.addAll(((ExpandoMetaClass) metaClass).getExpandoMethods());
    }
    if (isInterface) {
        MetaClass mc = ReflectionCache.OBJECT_CLASS.classInfo.getStrongMetaClass();
        addSubclassExpandos(arr, mc);
    } else {
        for (CachedClass cls = this; cls != null; cls = cls.getCachedSuperClass()) {
            MetaClass mc = cls.classInfo.getStrongMetaClass();
            addSubclassExpandos(arr, mc);
        }
    }
    for (CachedClass inf : getInterfaces()) {
        MetaClass mc = inf.classInfo.getStrongMetaClass();
        addSubclassExpandos(arr, mc);
    }
    return arr.toArray(MetaMethod.EMPTY_ARRAY);
}
Also used : MetaMethod(groovy.lang.MetaMethod) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ClosureMetaClass(org.codehaus.groovy.runtime.metaclass.ClosureMetaClass) ArrayList(java.util.ArrayList) ExpandoMetaClass(groovy.lang.ExpandoMetaClass)

Example 25 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class ClassInfo method getMetaClassUnderLock.

private MetaClass getMetaClassUnderLock() {
    MetaClass answer = getStrongMetaClass();
    if (answer != null)
        return answer;
    answer = getWeakMetaClass();
    final MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
    MetaClassRegistry.MetaClassCreationHandle mccHandle = metaClassRegistry.getMetaClassCreationHandler();
    if (isValidWeakMetaClass(answer, mccHandle)) {
        return answer;
    }
    answer = mccHandle.create(classRef.get(), metaClassRegistry);
    answer.initialize();
    if (GroovySystem.isKeepJavaMetaClasses()) {
        setStrongMetaClass(answer);
    } else {
        setWeakMetaClass(answer);
    }
    return answer;
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass)

Aggregations

MetaClass (groovy.lang.MetaClass)141 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)62 GroovyObject (groovy.lang.GroovyObject)62 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)40 MetaClassImpl (groovy.lang.MetaClassImpl)18 MetaProperty (groovy.lang.MetaProperty)12 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)11 MetaMethod (groovy.lang.MetaMethod)11 ArrayList (java.util.ArrayList)11 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)11 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)9 MetaClassRegistry (groovy.lang.MetaClassRegistry)9 Map (java.util.Map)9 GString (groovy.lang.GString)7 CachedClass (org.codehaus.groovy.reflection.CachedClass)7 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)7 MissingMethodException (groovy.lang.MissingMethodException)6 MissingPropertyException (groovy.lang.MissingPropertyException)6 IOException (java.io.IOException)6 Method (java.lang.reflect.Method)6