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);
}
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;
}
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);
}
}));
}
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);
}
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;
}
Aggregations