use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.
the class MethodSelectionException method appendMethods.
private void appendMethods(StringBuilder buffer) {
for (int i = 0; i < methods.size; i++) {
buffer.append("\n ");
Object methodOrConstructor = methods.get(i);
if (methodOrConstructor instanceof MetaMethod) {
MetaMethod method = (MetaMethod) methodOrConstructor;
buffer.append(Modifier.toString(method.getModifiers()));
buffer.append(" ").append(method.getReturnType().getName());
buffer.append(" ").append(method.getDeclaringClass().getName());
buffer.append("#");
buffer.append(method.getName());
appendClassNames(buffer, method.getNativeParameterTypes());
} else {
CachedConstructor method = (CachedConstructor) methodOrConstructor;
buffer.append(Modifier.toString(method.cachedConstructor.getModifiers()));
buffer.append(" ").append(method.cachedConstructor.getDeclaringClass().getName());
buffer.append("#<init>");
appendClassNames(buffer, method.getNativeParameterTypes());
}
}
}
use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.
the class MetaClassImpl method retrieveConstructor.
/**
* This is a helper method added in Groovy 2.1.0, which is used only by indy.
* This method is for internal use only.
* @since Groovy 2.1.0
*/
public MetaMethod retrieveConstructor(Object[] arguments) {
checkInitalised();
if (arguments == null)
arguments = EMPTY_ARGUMENTS;
Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
MetaClassHelper.unwrap(arguments);
Object res = chooseMethod("<init>", constructors, argClasses);
if (res instanceof MetaMethod)
return (MetaMethod) res;
CachedConstructor constructor = (CachedConstructor) res;
if (constructor != null)
return new MetaConstructor(constructor, false);
if (arguments.length == 1 && arguments[0] instanceof Map) {
res = chooseMethod("<init>", constructors, MetaClassHelper.EMPTY_TYPE_ARRAY);
} else if (arguments.length == 2 && arguments[1] instanceof Map && theClass.getEnclosingClass() != null && theClass.getEnclosingClass().isAssignableFrom(argClasses[0])) {
res = chooseMethod("<init>", constructors, new Class[] { argClasses[0] });
}
if (res instanceof MetaMethod)
return (MetaMethod) res;
constructor = (CachedConstructor) res;
if (constructor != null)
return new MetaConstructor(constructor, true);
return null;
}
use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.
the class MetaClassImpl method selectConstructorAndTransformArguments1.
/**
* Constructor selection algorithm for Groovy 2.1.9+.
* This selection algorithm was introduced as a workaround for GROOVY-6080. Instead of generating an index between
* 0 and N where N is the number of super constructors at the time the class is compiled, this algorithm uses
* a hash of the constructor descriptor instead.
*
* This has the advantage of letting the super class add new constructors while being binary compatible. But there
* are still problems with this approach:
* <ul>
* <li>There's a risk of hash collision, even if it's very low (two constructors of the same class must have the same hash)</li>
* <li>If the super class adds a new constructor which takes as an argument a superclass of an existing constructor parameter and
* that this new constructor is selected at runtime, it would not find it.</li>
* </ul>
*
* Hopefully in the last case, the error message is much nicer now since it explains that it's a binary incompatible change.
*
* @param arguments the actual constructor call arguments
* @return a hash used to identify the constructor to be called
* @since 2.1.9
*/
private int selectConstructorAndTransformArguments1(Object[] arguments) {
if (arguments == null)
arguments = EMPTY_ARGUMENTS;
Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
MetaClassHelper.unwrap(arguments);
CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, argClasses);
if (constructor == null) {
constructor = (CachedConstructor) chooseMethod("<init>", constructors, argClasses);
}
if (constructor == null) {
throw new GroovyRuntimeException("Could not find matching constructor for: " + theClass.getName() + "(" + InvokerHelper.toTypeString(arguments) + ")");
}
final String methodDescriptor = BytecodeHelper.getMethodDescriptor(Void.TYPE, constructor.getNativeParameterTypes());
// keeping 3 bits for additional information such as vargs
return BytecodeHelper.hashCode(methodDescriptor);
}
use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.
the class MetaClassImpl method invokeConstructor.
private Object invokeConstructor(Class at, Object[] arguments) {
checkInitalised();
if (arguments == null)
arguments = EMPTY_ARGUMENTS;
Class[] argClasses = MetaClassHelper.convertToTypeArray(arguments);
MetaClassHelper.unwrap(arguments);
CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, argClasses);
if (constructor != null) {
return constructor.doConstructorInvoke(arguments);
}
if (arguments.length == 1) {
Object firstArgument = arguments[0];
if (firstArgument instanceof Map) {
constructor = (CachedConstructor) chooseMethod("<init>", constructors, MetaClassHelper.EMPTY_TYPE_ARRAY);
if (constructor != null) {
Object bean = constructor.doConstructorInvoke(MetaClassHelper.EMPTY_ARRAY);
setProperties(bean, ((Map) firstArgument));
return bean;
}
}
}
throw new GroovyRuntimeException("Could not find matching constructor for: " + theClass.getName() + "(" + InvokerHelper.toTypeString(arguments) + ")");
}
Aggregations