我正在创建应用程序的过程中,但我偶然发现了一个问题。
我正在创建一个程序,它将根据用户输入生成一个.java文件。在该程序中,您将能够选择自定义api(但不能提供给您)。一旦选择了API调用,就必须为该方法指定输入。您还可以指定另一个API调用作为当前参数的输入。我只想显示提供正确返回值作为所选api调用的输入的api调用。问题就在这里。我可以检测到所选api调用的参数的输入类型,但似乎无法检测到提供给listAPICallsWithReturnValue(...)的classValue参数的类型。函数的作用是:返回一个java.lang.reflect.Method对象。
我希望你们都能理解我的意思。:)
public void displayParameterDialogs(APICall call) {
JDialogMethodParameters dialog = new JDialogMethodParameters(mainframe, true);
for (int i = 0; i < call.getMethod().getParameterTypes().length; i++) {
dialog.init(i, call.getMethod().getParameterTypes()[i]);
dialog.setVisible(true);
}
}
//dialog class
public void init(int parameterIndex, Class parameterType) {
this.jLabelInfo.setText("Data for input parameter: " + parameterIndex);
DefaultComboBoxModel cmodel = new DefaultComboBoxModel();
for (APICall call : TestFactory.getInstance().listAPICallsWithReturnValue(parameterType)) {
cmodel.addElement(call);
}
this.jComboBox1.setModel(cmodel);
}
public APICall[] listAPICallsWithReturnValue(Class<?> classValue) {
APICall[] calls;
Vector<APICall> temp = new Vector<APICall>();
Method[] methods = TestSuite.class.getMethods();
for (Method method : methods) {
System.out.println(method.getReturnType().getName());
System.out.println(classValue.getClass().getName());
System.out.println(classValue.toString());
if (method.getReturnType().getCanonicalName().equals(classValue.toString())) {
temp.add(new APICall(method));
}
}
calls = new APICall[temp.size()];
return temp.toArray(calls);
}发布于 2011-05-20 22:58:36
也许:
classValue.getName()
classValue.getClass().getName()将返回“类”(因为classValue的类型是类)。
发布于 2011-05-20 23:10:04
我建议简单地打印出getCanonicalName(),您会发现它与toString()不匹配。事实上,getName将匹配,如果不是非常接近,也许它不会添加单词“类”等……
也许可以使用class.getName进行两种比较,或者仔细检查您还可以使用什么……
发布于 2011-05-20 23:23:35
尝试将getClass()与isAssignableFrom()结合使用
示例:
public class Main {
static class A {}
static class B extends A {}
public static void main(String[] args) {
Object a = new A();
Object b = new B();
boolean aAssignableFromB = a.getClass().isAssignableFrom(b.getClass()); // true
boolean bAssignableFromA = b.getClass().isAssignableFrom(a.getClass()); // false
System.out.println(aAssignableFromB);
System.out.println(bAssignableFromA);
}}
https://stackoverflow.com/questions/6073733
复制相似问题