我有两个问题:
我想提取一个类的接口列表,并且使用下面的代码来做到这一点。问题在于此代码同时提取了超类和接口,而我只想提取接口。我曾尝试使用代码if(inter.isInterface())
,但没有用,我仍在同时检索接口和超类。
您能给我指示如何吸引一个类的仅接口(不包括超类)吗? getSuperInterfaces()方法有替代方法吗?
for(CtType<?> clazz : classFactory.getAll(true)) {
String myinterfaceclassid = null;
String myinterfacename = null;
String myclassid = null;
String myclassname = null;
String FullClassName= clazz.getPackage()+"."+clazz.getSimpleName();
Set<CtTypeReference<?>> interfaces = clazz.getSuperInterfaces();
for(CtTypeReference<?> inter: interfaces) {
ResultSet interfacesnames = st.executeQuery("SELECT classname from classes where classname='"+inter+"'");
while(interfacesnames.next()) {
myinterfacename= interfacesnames.getString("classname");
}
ResultSet interfacesclasses = st.executeQuery("SELECT id from classes where classname='"+inter+"'");
while(interfacesclasses.next()) {
myinterfaceclassid= interfacesclasses.getString("id");
}
ResultSet classesnames= st.executeQuery("SELECT classname from classes where classname='"+FullClassName+"'");
while(classesnames.next()) {
myclassname= classesnames.getString("classname");
}
ResultSet interfacesname = st.executeQuery("SELECT id from classes where classname='"+FullClassName+"'");
while(interfacesname.next()) {
myclassid= interfacesname.getString("id");
}
if(myinterfaceclassid != null) {
st.executeUpdate("INSERT INTO `interfaces`(`interfaceclassid`,`interfacename`,`ownerclassid`, `classname`) VALUES ('"+myinterfaceclassid +"','" +myinterfacename+"','" +myclassid+"','" +myclassname+"')");
}
System.out.println(inter);
CtTypeReference<?> mysuperclass = inter.getSuperclass();
System.out.println("SUPERCLASS :"+ mysuperclass);
}
}
这是我使用Spoon解析的项目中的接口示例。在这种情况下,接口为TransformationPly,超类为Ply。在我的情况下,方法getSuperInterfaces()
返回超类和接口,这是我不希望的。
public interface TransformationPly extends Ply {
// Methods
/**
* Get the new type of the piece after the transformation.
*
* @return The new piece after the transformation.
*/
byte getTypeAfterTransformation();
/**
* Set the new piece type after the transformation.
*
* @param pieceType The new piece type after the transformation.
*/
void setTypeAfterTransformation( byte pieceType);
}
我还想提取接口的超类,如上例所示。在上述情况下,Ply是TransformationPly的超类。
我尝试了以下代码来提取TransformarionPly的超类,但mysuperclass始终为null。
for(CtType<?> clazz : classFactory.getAll(true)) {
Set<CtTypeReference<?>> interfaces = clazz.getSuperInterfaces();
for(CtTypeReference<?> inter: interfaces) {
CtTypeReference<?> mysuperclass = inter.getSuperclass();
System.out.println("SUPERCLASS :"+ mysuperclass);
}
能否请您指导我如何提取接口的超类?