Search in sources :

Example 11 with ClassInfo

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

the class MetaClassRegistryImpl method setMetaClass.

public void setMetaClass(Object obj, MetaClass theMetaClass) {
    Class theClass = obj.getClass();
    final ClassInfo info = ClassInfo.getClassInfo(theClass);
    MetaClass oldMC = null;
    info.lock();
    try {
        oldMC = info.getPerInstanceMetaClass(obj);
        info.setPerInstanceMetaClass(obj, theMetaClass);
    } finally {
        info.unlock();
    }
    fireConstantMetaClassUpdate(obj, theClass, oldMC, theMetaClass);
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) ClassInfo(org.codehaus.groovy.reflection.ClassInfo)

Example 12 with ClassInfo

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

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();
        }
    }
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MetaClassImpl(groovy.lang.MetaClassImpl) GroovyObject(groovy.lang.GroovyObject) ClassInfo(org.codehaus.groovy.reflection.ClassInfo)

Example 13 with ClassInfo

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

the class MetaClassImpl method findMethodInClassHierarchy.

protected static MetaMethod findMethodInClassHierarchy(Class instanceKlazz, String methodName, Class[] arguments, MetaClass metaClass) {
    if (metaClass instanceof MetaClassImpl) {
        boolean check = false;
        for (ClassInfo ci : ((MetaClassImpl) metaClass).theCachedClass.getHierarchy()) {
            final MetaClass aClass = ci.getStrongMetaClass();
            if (aClass instanceof MutableMetaClass && ((MutableMetaClass) aClass).isModified()) {
                check = true;
                break;
            }
        }
        if (!check)
            return null;
    }
    MetaMethod method = null;
    Class superClass;
    if (metaClass.getTheClass().isArray() && !metaClass.getTheClass().getComponentType().isPrimitive() && metaClass.getTheClass().getComponentType() != Object.class) {
        superClass = Object[].class;
    } else {
        superClass = metaClass.getTheClass().getSuperclass();
    }
    if (superClass != null) {
        MetaClass superMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(superClass);
        method = findMethodInClassHierarchy(instanceKlazz, methodName, arguments, superMetaClass);
    } else {
        if (metaClass.getTheClass().isInterface()) {
            MetaClass superMetaClass = GroovySystem.getMetaClassRegistry().getMetaClass(Object.class);
            method = findMethodInClassHierarchy(instanceKlazz, methodName, arguments, superMetaClass);
        }
    }
    method = findSubClassMethod(instanceKlazz, methodName, arguments, metaClass, method);
    method = getMetaMethod(instanceKlazz, methodName, arguments, metaClass, method);
    method = findOwnMethod(instanceKlazz, methodName, arguments, metaClass, method);
    return method;
}
Also used : NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) NewMetaMethod(org.codehaus.groovy.runtime.metaclass.NewMetaMethod) GeneratedMetaMethod(org.codehaus.groovy.reflection.GeneratedMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) NewStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod) TransformMetaMethod(org.codehaus.groovy.runtime.metaclass.TransformMetaMethod) CachedClass(org.codehaus.groovy.reflection.CachedClass) ClassInfo(org.codehaus.groovy.reflection.ClassInfo)

Example 14 with ClassInfo

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

the class MethodRankHelper method getPropertySuggestionString.

/**
 * Returns a string detailing possible solutions to a missing field or property
 * if no good solutions can be found a empty string is returned.
 *
 * @param fieldName the missing field
 * @param type the class on which the field is sought
 * @return a string with probable solutions to the exception
 */
public static String getPropertySuggestionString(String fieldName, Class type) {
    ClassInfo ci = ClassInfo.getClassInfo(type);
    List<MetaProperty> fi = ci.getMetaClass().getProperties();
    List<RankableField> rf = new ArrayList<RankableField>(fi.size());
    StringBuilder sb = new StringBuilder();
    sb.append("\nPossible solutions: ");
    for (MetaProperty mp : fi) rf.add(new RankableField(fieldName, mp));
    Collections.sort(rf);
    int i = 0;
    for (RankableField f : rf) {
        if (i > MAX_RECOMENDATIONS)
            break;
        if (f.score > MAX_FIELD_SCORE)
            break;
        if (i > 0)
            sb.append(", ");
        sb.append(f.f.getName());
        i++;
    }
    return i > 0 ? sb.toString() : "";
}
Also used : ArrayList(java.util.ArrayList) MetaProperty(groovy.lang.MetaProperty) ClassInfo(org.codehaus.groovy.reflection.ClassInfo)

Aggregations

ClassInfo (org.codehaus.groovy.reflection.ClassInfo)14 MetaClass (groovy.lang.MetaClass)7 CachedClass (org.codehaus.groovy.reflection.CachedClass)5 ArrayList (java.util.ArrayList)4 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)3 MetaClassImpl (groovy.lang.MetaClassImpl)3 MetaMethod (groovy.lang.MetaMethod)2 MetaProperty (groovy.lang.MetaProperty)2 GeneratedMetaMethod (org.codehaus.groovy.reflection.GeneratedMetaMethod)2 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 NewMetaMethod (org.codehaus.groovy.runtime.metaclass.NewMetaMethod)2 NewStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.NewStaticMetaMethod)2 TransformMetaMethod (org.codehaus.groovy.runtime.metaclass.TransformMetaMethod)2 Label (org.objectweb.asm.Label)2 MethodVisitor (org.objectweb.asm.MethodVisitor)2 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)1 GroovyObject (groovy.lang.GroovyObject)1