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