use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.
the class MetaClassImpl method createConstructorSite.
/**
* Create a CallSite
*/
public CallSite createConstructorSite(CallSite site, Object[] args) {
if (!(this instanceof AdaptingMetaClass)) {
Class[] params = MetaClassHelper.convertToTypeArray(args);
CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, params);
if (constructor != null) {
return ConstructorSite.createConstructorSite(site, this, constructor, params, args);
} else {
if (args.length == 1 && args[0] instanceof Map) {
constructor = (CachedConstructor) chooseMethod("<init>", constructors, MetaClassHelper.EMPTY_TYPE_ARRAY);
if (constructor != null) {
return new ConstructorSite.NoParamSite(site, this, constructor, params);
}
} else if (args.length == 2 && theClass.getEnclosingClass() != null && args[1] instanceof Map) {
Class enclosingClass = theClass.getEnclosingClass();
String enclosingInstanceParamType = args[0] != null ? args[0].getClass().getName() : "";
if (enclosingClass.getName().equals(enclosingInstanceParamType)) {
constructor = (CachedConstructor) chooseMethod("<init>", constructors, new Class[] { enclosingClass });
if (constructor != null) {
return new ConstructorSite.NoParamSiteInnerClass(site, this, constructor, params);
}
}
}
}
}
return new MetaClassConstructorSite(site, this);
}
use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.
the class MetaClassImpl method retrieveConstructor.
public Constructor retrieveConstructor(Class[] arguments) {
CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, arguments);
if (constructor != null) {
return constructor.cachedConstructor;
}
constructor = (CachedConstructor) chooseMethod("<init>", constructors, arguments);
if (constructor != null) {
return constructor.cachedConstructor;
}
return null;
}
use of org.codehaus.groovy.reflection.CachedConstructor in project groovy by apache.
the class MetaClassImpl method selectConstructorAndTransformArguments0.
private int selectConstructorAndTransformArguments0(final int numberOfConstructors, Object[] arguments) {
//TODO: that is just a quick prototype, not the real thing!
if (numberOfConstructors != constructors.size()) {
throw new IncompatibleClassChangeError("the number of constructors during runtime and compile time for " + this.theClass.getName() + " do not match. Expected " + numberOfConstructors + " but got " + constructors.size());
}
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) + ")");
}
List l = new ArrayList(constructors.toList());
Comparator comp = new Comparator() {
public int compare(Object arg0, Object arg1) {
CachedConstructor c0 = (CachedConstructor) arg0;
CachedConstructor c1 = (CachedConstructor) arg1;
String descriptor0 = BytecodeHelper.getMethodDescriptor(Void.TYPE, c0.getNativeParameterTypes());
String descriptor1 = BytecodeHelper.getMethodDescriptor(Void.TYPE, c1.getNativeParameterTypes());
return descriptor0.compareTo(descriptor1);
}
};
Collections.sort(l, comp);
int found = -1;
for (int i = 0; i < l.size(); i++) {
if (l.get(i) != constructor)
continue;
found = i;
break;
}
// NOTE: must be changed to "1 |" if constructor was vargs
return 0 | (found << 8);
}
use of org.codehaus.groovy.reflection.CachedConstructor in project groovy-core by groovy.
the class MetaClassImpl method retrieveConstructor.
public Constructor retrieveConstructor(Class[] arguments) {
CachedConstructor constructor = (CachedConstructor) chooseMethod("<init>", constructors, arguments);
if (constructor != null) {
return constructor.cachedConstructor;
}
constructor = (CachedConstructor) chooseMethod("<init>", constructors, arguments);
if (constructor != null) {
return constructor.cachedConstructor;
}
return null;
}
use of org.codehaus.groovy.reflection.CachedConstructor in project groovy-core by groovy.
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());
}
}
}
Aggregations