constructors.add(c);
}
}
}
MetaConstructor constructor;
if (constructors.isEmpty()) {
constructor = toMap.getConstructor(new MetaClass[0]);
}
else if (constructors.size() > 1) {
throw new InvalidMappingException("found more than one matching constructor for mapping: "
+ toMap.getFullyQualifiedName());
}
else {
constructor = constructors.iterator().next();
simpleConstructorMapping.setConstructor(constructor);
}
definition.setInstantiationMapping(simpleConstructorMapping);
if (toMap.isEnum()) {
return definition;
}
if (simpleConstructorMapping.getMappings().length == 0) {
Set<MetaMethod> factoryMethods = new HashSet<MetaMethod>();
SimpleFactoryMapping simpleFactoryMapping = new SimpleFactoryMapping();
for (MetaMethod method : toMap.getDeclaredMethods()) {
if (method.isStatic()) {
boolean satisifed = true;
for (int i = 0; i < method.getParameters().length; i++) {
Annotation[] annotations = method.getParameters()[i].getAnnotations();
if (annotations.length == 0) {
satisifed = false;
}
else {
for (Annotation a : annotations) {
if (MapsTo.class.isAssignableFrom(a.annotationType())) {
MapsTo mapsTo = (MapsTo) a;
String key = mapsTo.value();
simpleFactoryMapping.mapParmToIndex(key, i, method.getParameters()[i].getType());
}
}
}
if (satisifed) {
factoryMethods.add(method);
}
}
}
}
if (factoryMethods.size() > 1) {
throw new InvalidMappingException("found more than one matching factory method for mapping: "
+ toMap.getFullyQualifiedName());
}
else if (factoryMethods.size() == 1) {
final MetaMethod meth = factoryMethods.iterator().next();
simpleFactoryMapping.setMethod(meth);
definition.setInheritedInstantiationMapping(simpleFactoryMapping);
}
}
if (definition.getInstantiationMapping() instanceof ConstructorMapping
&& definition.getInstantiationMapping().getMappings().length == 0) {
MetaConstructor defaultConstructor = toMap.getDeclaredConstructor();
if (defaultConstructor == null || !defaultConstructor.isPublic()) {
throw new InvalidMappingException("there is no custom mapping or default no-arg constructor to map: "
+ toMap.getFullyQualifiedName());
}
}