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.
/**
* 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-core by groovy.
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);
}
Aggregations