Skip to content

Commit

Permalink
Patch to report candidate constructors when failing to instantiate a …
Browse files Browse the repository at this point in the history
…class because of an incorrect number of passed parameters. (fixes #2987186)

Example output:

No constructor found for NNAtom with the given number of parameters.
Candidates are: NNAtom(), NNAtom(String), NNAtom(IElement),
NNAtom(String, Point3d), NNAtom(String, Point2d)

Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
  • Loading branch information
egonw authored and rajarshi committed Apr 18, 2012
1 parent 6b9c1bd commit 2de5acc
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 19 deletions.
51 changes: 38 additions & 13 deletions src/main/org/openscience/cdk/DefaultChemObjectBuilder.java
Expand Up @@ -18,6 +18,7 @@
*/
package org.openscience.cdk;

import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

Expand All @@ -40,6 +41,7 @@
import org.openscience.cdk.interfaces.IChemFile;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IChemObject;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IChemSequence;
import org.openscience.cdk.interfaces.ICrystal;
import org.openscience.cdk.interfaces.IElectronContainer;
Expand All @@ -53,7 +55,6 @@
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IMoleculeSet;
import org.openscience.cdk.interfaces.IMonomer;
import org.openscience.cdk.interfaces.IChemObjectBuilder;
import org.openscience.cdk.interfaces.IPDBAtom;
import org.openscience.cdk.interfaces.IPDBMonomer;
import org.openscience.cdk.interfaces.IPDBPolymer;
Expand Down Expand Up @@ -178,11 +179,41 @@ public <T extends ICDKObject>T newInstance(
return (T)new AdductFormula((IMolecularFormula)params[0]);
}

throw new IllegalArgumentException(
"No constructor found with the given number of parameters."
);
throw new IllegalArgumentException(getNoConstructorFoundMessage(clazz));
}

private String getNoConstructorFoundMessage(Class clazz) {
StringBuffer buffer = new StringBuffer();
String className = clazz.getName().substring(32);
buffer.append("No constructor found for ");
buffer.append(className);
buffer.append(" with the given number of parameters.");

// try loading the implementation
try {
Class impl = this.getClass().getClassLoader().loadClass(
"org.openscience.cdk." + className
);
buffer.append(" Candidates are: ");
Constructor[] constructors = impl.getConstructors();
for (int i=0; i<constructors.length; i++) {
buffer.append(className).append('(');
Class[] params = constructors[i].getParameterTypes();
for (int j=0; j<params.length; j++) {
buffer.append(params[j].getName().substring(
params[j].getName().lastIndexOf('.') + 1
));
if ((j+1)<params.length) buffer.append(", ");
}
buffer.append(')');
if ((i+1)<constructors.length) buffer.append(", ");
}
} catch (ClassNotFoundException e) {
// ok, then we do without suggestions
}
return buffer.toString();
}

@SuppressWarnings("unchecked")
private <T extends ICDKObject>T newAtomContainerInstance(
Class<T> clazz, Object... params)
Expand Down Expand Up @@ -240,9 +271,7 @@ private <T extends ICDKObject>T newAtomContainerInstance(
}
}

throw new IllegalArgumentException(
"No constructor found with the given number of parameters."
);
throw new IllegalArgumentException(getNoConstructorFoundMessage(clazz));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -342,9 +371,7 @@ private <T extends ICDKObject>T newElementInstance(
}
}

throw new IllegalArgumentException(
"No constructor found with the given number of parameters."
);
throw new IllegalArgumentException(getNoConstructorFoundMessage(clazz));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -419,9 +446,7 @@ private <T extends ICDKObject>T newElectronContainerInstance(
if (params.length == 0) return (T)new ElectronContainer();
}

throw new IllegalArgumentException(
"No constructor found with the given number of parameters."
);
throw new IllegalArgumentException(getNoConstructorFoundMessage(clazz));
}

}
Expand Down
37 changes: 34 additions & 3 deletions src/main/org/openscience/cdk/debug/DebugChemObjectBuilder.java
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Constructor;

import javax.vecmath.Point2d;
import javax.vecmath.Point3d;
Expand Down Expand Up @@ -372,10 +373,40 @@ public <T extends ICDKObject>T newInstance(
return (T)new DebugAdductFormula((IMolecularFormula)params[0]);
}

throw new IllegalArgumentException(
"No constructor found with the given number of parameters."
);
throw new IllegalArgumentException(getNoConstructorFoundMessage(clazz));
}

private String getNoConstructorFoundMessage(Class clazz) {
StringBuffer buffer = new StringBuffer();
String className = "Debug" + clazz.getName().substring(32);
buffer.append("No constructor found for ");
buffer.append(className);
buffer.append(" with the given number of parameters.");

// try loading the implementation
try {
Class impl = this.getClass().getClassLoader().loadClass(
"org.openscience.cdk.debug." + className
);
buffer.append(" Candidates are: ");
Constructor[] constructors = impl.getConstructors();
for (int i=0; i<constructors.length; i++) {
buffer.append(className).append('(');
Class[] params = constructors[i].getParameterTypes();
for (int j=0; j<params.length; j++) {
buffer.append(params[j].getName().substring(
params[j].getName().lastIndexOf('.') + 1
));
if ((j+1)<params.length) buffer.append(", ");
}
buffer.append(')');
if ((i+1)<constructors.length) buffer.append(", ");
}
} catch (ClassNotFoundException e) {
// ok, then we do without suggestions
}
return buffer.toString();
}
}


Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Constructor;

import javax.vecmath.Point2d;
import javax.vecmath.Point3d;
Expand Down Expand Up @@ -377,11 +378,40 @@ public <T extends ICDKObject>T newInstance(
return (T)new NNAdductFormula((IMolecularFormula)params[0]);
}

throw new IllegalArgumentException(
"No constructor found with the given number of parameters."
);
throw new IllegalArgumentException(getNoConstructorFoundMessage(clazz));
}

private String getNoConstructorFoundMessage(Class clazz) {
StringBuffer buffer = new StringBuffer();
String className = "NN" + clazz.getName().substring(32);
buffer.append("No constructor found for ");
buffer.append(className);
buffer.append(" with the given number of parameters.");

// try loading the implementation
try {
Class impl = this.getClass().getClassLoader().loadClass(
"org.openscience.cdk.nonotify." + className
);
buffer.append(" Candidates are: ");
Constructor[] constructors = impl.getConstructors();
for (int i=0; i<constructors.length; i++) {
buffer.append(className).append('(');
Class[] params = constructors[i].getParameterTypes();
for (int j=0; j<params.length; j++) {
buffer.append(params[j].getName().substring(
params[j].getName().lastIndexOf('.') + 1
));
if ((j+1)<params.length) buffer.append(", ");
}
buffer.append(')');
if ((i+1)<constructors.length) buffer.append(", ");
}
} catch (ClassNotFoundException e) {
// ok, then we do without suggestions
}
return buffer.toString();
}
}


0 comments on commit 2de5acc

Please sign in to comment.