use of org.codehaus.groovy.reflection.ClassInfo in project groovy-core by groovy.
the class DefaultGroovyMethods method hasPerInstanceMetaClass.
private static MetaClass hasPerInstanceMetaClass(Object object) {
if (object instanceof GroovyObject) {
MetaClass mc = ((GroovyObject) object).getMetaClass();
if (mc == GroovySystem.getMetaClassRegistry().getMetaClass(object.getClass()) || mc.getClass() == MetaClassImpl.class)
return null;
else
return mc;
} else {
ClassInfo info = ClassInfo.getClassInfo(object.getClass());
info.lock();
try {
return info.getPerInstanceMetaClass(object);
} finally {
info.unlock();
}
}
}
use of org.codehaus.groovy.reflection.ClassInfo 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 org.codehaus.groovy.reflection.ClassInfo in project groovy by apache.
the class MethodRankHelper method getMethodSuggestionString.
/**
* Returns a string detailing possible solutions to a missing method
* if no good solutions can be found a empty string is returned.
*
* @param methodName the name of the method that doesn't exist
* @param type the class on which the method is invoked
* @param arguments the arguments passed to the method
* @return a string with probable solutions to the exception
*/
public static String getMethodSuggestionString(String methodName, Class type, Object[] arguments) {
ClassInfo ci = ClassInfo.getClassInfo(type);
List<MetaMethod> methods = new ArrayList<MetaMethod>(ci.getMetaClass().getMethods());
methods.addAll(ci.getMetaClass().getMetaMethods());
List<MetaMethod> sugg = rankMethods(methodName, arguments, methods);
StringBuilder sb = new StringBuilder();
if (!sugg.isEmpty()) {
sb.append("\nPossible solutions: ");
for (int i = 0; i < sugg.size(); i++) {
if (i != 0)
sb.append(", ");
sb.append(sugg.get(i).getName()).append("(");
sb.append(listParameterNames(sugg.get(i).getParameterTypes()));
sb.append(")");
}
}
Class[] argumentClasses = getArgumentClasses(arguments);
List<Pair<Class, Class>> conflictClasses = getConflictClasses(sugg, argumentClasses);
if (!conflictClasses.isEmpty()) {
sb.append("\nThe following classes appear as argument class and as parameter class, ");
sb.append("but are defined by different class loader:\n");
boolean first = true;
for (Pair<Class, Class> pair : conflictClasses) {
if (!first) {
sb.append(", ");
} else {
first = false;
}
sb.append(pair.u.getName()).append(" (defined by '");
sb.append(pair.u.getClassLoader());
sb.append("' and '");
sb.append(pair.v.getClassLoader());
sb.append("')");
}
sb.append("\nIf one of the method suggestions matches the method you wanted to call, ");
sb.append("\nthen check your class loader setup.");
}
return sb.toString();
}
use of org.codehaus.groovy.reflection.ClassInfo in project groovy by apache.
the class CallSiteArray method createPojoSite.
// for MetaClassImpl we try to pick meta method,
// otherwise or if method doesn't exist we make call via POJO meta class
private static CallSite createPojoSite(CallSite callSite, Object receiver, Object[] args) {
final Class klazz = receiver.getClass();
MetaClass metaClass = InvokerHelper.getMetaClass(receiver);
if (!GroovyCategorySupport.hasCategoryInCurrentThread() && metaClass instanceof MetaClassImpl) {
final MetaClassImpl mci = (MetaClassImpl) metaClass;
final ClassInfo info = mci.getTheCachedClass().classInfo;
if (info.hasPerInstanceMetaClasses()) {
return new PerInstancePojoMetaClassSite(callSite, info);
} else {
return mci.createPojoCallSite(callSite, receiver, args);
}
}
ClassInfo info = ClassInfo.getClassInfo(klazz);
if (info.hasPerInstanceMetaClasses()) {
return new PerInstancePojoMetaClassSite(callSite, info);
} else {
return new PojoMetaClassSite(callSite, metaClass);
}
}
use of org.codehaus.groovy.reflection.ClassInfo in project groovy by apache.
the class MetaClassRegistryImpl method setMetaClass.
/**
* if oldMc is null, newMc will replace whatever meta class was used before.
* if oldMc is not null, then newMc will be used only if the stored mc is
* the same as oldMc
*/
private void setMetaClass(Class theClass, MetaClass oldMc, MetaClass newMc) {
final ClassInfo info = ClassInfo.getClassInfo(theClass);
MetaClass mc = null;
info.lock();
try {
mc = info.getStrongMetaClass();
info.setStrongMetaClass(newMc);
} finally {
info.unlock();
}
if ((oldMc == null && mc != newMc) || (oldMc != null && mc != newMc && mc != oldMc)) {
fireConstantMetaClassUpdate(null, theClass, mc, newMc);
}
}
Aggregations