Newsgroup: it.comp.java
Titolo: Java reflection su interfacce

Salve,
e' possibile utilizzare la reflection con le interfacce? 
Es. avere una lista dei metodi etc.
Grazie.
-- 
I'am what I'am, I do what I can

Si, ho teminato un'applicazione che analizza gli archivi jar, se serve...
"Setec Astronomy" <setec@_NO_NOSPAM_freemail.it> ha scritto nel messaggio
news:0TDbb.337371$Ny5.10...@twister2.libero.it...


 serve...  
Potresti postare il codice che permette di restituire le'elnco dei
metodi presenti in una interfaccia, senza avere alcuna classe che la
implementi.


Questo è il codice della classe che serve come modello per visualizzare gli
attributi della classe in un JTree ed il suo albero ereditario in un'altro.
Il codice che cerchi dovrebbe trovarsi all'interno di createnodes()
Presto l'applicazione con tutti i sorgenti sarà nel mio sito
www.mycgiserver.com/~erfranco
, per ora dedicato ad un'applicazione jsp.
/*
 * ClassModel.java
 *
 * Created on 13 settembre 2003, 22.17
 */
package erfranco.jarexplorer.model;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
import javax.swing.event.TreeModelListener;
import javax.swing.event.EventListenerList;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Iterator;
/** The model for the tree describing Class Fields, Methods and Costructors.
 * This Model builds two different trees, one with Class Fields, Methods and
Costructors,
 * the other with the inheritance tree and interfaces.
 *  The flagGenealogy distinguishes from each other.
 * @author ermanno
 */
public class ClassModel implements TreeModel
{
  private final CITreeNode root;
  private final Class cl;
  private final ArrayList listGenealogy= new ArrayList();
  private final EventListenerList listenerList = new EventListenerList();
  private boolean flagGenealogy;
  /** Creates a new instance of ClassModel
   * @param cl
   */
  public ClassModel(Class cl)
  {
    this(cl, false);
  }
  /**
   * @param cl
   * @param searchGenealogy
   */
  public ClassModel(Class cl,boolean searchGenealogy)
  {
    flagGenealogy=searchGenealogy;
 
this.cl
=cl;
    root = new CITreeNode(cl.getName());
root.setType(cl.isInterface()?CITreeNode.INTERFACE:(cl.getModifiers()&Modifi
er.ABSTRACT)!=0?CITreeNode.ABSTRACTCLASS:CITreeNode.CLASS);
    if (flagGenealogy)
    {
      listGenealogy.clear();
      addClass(cl);
    } else
    {
      createnodes();
    }
  }
  /**
   * @param cl
   */
  private void addClass(Class cl)
  {
    CITreeNode pnode=root;
    CITreeNode cnode;
    Iterator it;
    Class[] interfaces= cl.getInterfaces();
    if (interfaces.length>0)
    {
      root.addChild(pnode=new CITreeNode("ext. or implemented interfaces"));
      addInterfaces(cl,pnode);
    }
    if (!cl.isInterface())
    {
      addSuperclasses(cl,root);
    }
  }
  /** Recursive method that explores hierarchy of class extended by
   * a class, and create a tree structure to hold them
   * @param cl
   * @param pnode
   */
  private void addSuperclasses(Class cl, CITreeNode pnode)
  {
    int mod;
    if (!(cl==null||pnode==null||cl.isInterface()))
    {
      mod=cl.getModifiers();
      CITreeNode cnode=new CITreeNode(cl.getName());
cnode.setType(cl.equals(Object.class)?CITreeNode.PROPERTY:Modifier.isAbstrac
t(mod)?CITreeNode.ABSTRACTCLASS:CITreeNode.CLASS);
      addSuperclasses(cl=cl.getSuperclass(), cnode);
      pnode.addChild(cnode);
    }
  }
  /** Recursive method that explores hierarchy of interfaces extended or
implemented by
   * a class, and create a tree structure to hold them
   * @param cl
   * @param pnode
   */
  private void addInterfaces(Class cl, CITreeNode pnode)
  {
    CITreeNode cnode;
    CITreeNode anode;
    Class[] interfaces= cl.getInterfaces();
    if (interfaces.length>0)
    {
      for (int i=0;i<interfaces.length;i++)
      {
        cnode=new CITreeNode(interfaces[i].getName());
        cnode.setType(CITreeNode.INTERFACE);
        addInterfaces(interfaces[i],cnode);
        pnode.addChild(cnode);
      }
    }
  }
  private void createnodes()
  {
    CITreeNode pnode=root;
    CITreeNode cnode;
    // Constructors
    Constructor[] constructors = cl.getDeclaredConstructors();
    for (int i=0; i<constructors.length; i++)
    {
      Constructor c = constructors[i];
      Class[] paramTypes = c.getParameterTypes();
      String name = c.getName();
      StringBuffer sb = new StringBuffer();
      sb.append(Modifier.toString(c.getModifiers()));
      sb.append(" " + name + "(");
      for (int j=0; j<paramTypes.length; j++)
      {
        if (j>0) sb.append(", ");
        sb.append(paramTypes[j].getName());
      }
      sb.append(");");
      cnode = new CITreeNode(sb.toString());
      cnode.setType(CITreeNode.CONSTRUCTOR);
      pnode.addChild(cnode);
    }
    // Methods
    Method[] methods = cl.getDeclaredMethods();
    for (int i=0; i<methods.length; i++)
    {
      Method m = methods[i];
      //why only new methods?
      Class retType = m.getReturnType();
      Class[] paramTypes = m.getParameterTypes();
      String name = m.getName();
      StringBuffer sb = new StringBuffer();
      sb.append(Modifier.toString(m.getModifiers()));
      sb.append(" " + retType.getName() + " " + name + "(");
      for (int j=0; j<paramTypes.length; j++)
      {
        if (j>0) sb.append(", ");
        sb.append(paramTypes[j].getName());
      }
      sb.append(");");
      cnode = new CITreeNode(sb.toString());
      cnode.setType(CITreeNode.METHOD);
      pnode.addChild(cnode);
    }
    // Properties
    Field[] fields = cl.getDeclaredFields();
    for (int i=0; i<fields.length; i++)
    {
      Field f = fields[i];
      //why only new fields?
      Class type = f.getType();
      String name = f.getName();
      StringBuffer sb = new StringBuffer();
      sb.append(Modifier.toString(f.getModifiers()));
      sb.append(" " + type.getName() + " " + name + ";");
      cnode = new CITreeNode(sb.toString());
      cnode.setType(CITreeNode.PROPERTY);
      pnode.addChild(cnode);
    }
  }
  /**
   * @param l
   */
  public void addTreeModelListener(TreeModelListener l)
  {
    listenerList.add(TreeModelListener.class, l);
  }
  /**
   * @param parent
   * @param index
   * @return
   */
  public Object getChild(Object parent, int index)
  {    ArrayList v = ((CITreeNode) parent).getChildren();
       return v.get(index);
  }
  /**
   * @param parent
   * @return
   */
  public int getChildCount(Object parent)
  {        return ((CITreeNode) parent).getChildren().size();
  }
  /**
   * @param parent
   * @param child
   * @return
   */
  public int getIndexOfChild(Object parent, Object child)
  {      ArrayList v = ((CITreeNode) parent).getChildren();
         return v.indexOf(child);
  }
  /**
   * @return the root Object
   */
  public Object getRoot()
  {return root;
  }
  /**
   * @param node
   * @return
   */
  public boolean isLeaf(Object node)
  {
    return ((CITreeNode) node).getType() > CITreeNode.ABSTRACTINTERFACE;
  }
  /**
   * @param l
   */
  public void removeTreeModelListener(TreeModelListener l)
  {
    listenerList.remove(TreeModelListener.class, l);
  }
  /** not implemented
   * @param path
   * @param newValue
   */
  public void valueForPathChanged(TreePath path, Object newValue)
  {
  }
}

 ereditario in un'altro.
[cut]
Ti ringrazio per quello che hai postato, il mio problem e' pero'
leggermente diverso.
Ho un file che definisce una interfaccia es:
// ---------- Begin Code ---------- //
public interface ProjectsIntf {
 
    public boolean createproject (int IDOwner, String Title, String
Description,
    int Privilege, boolean IsEnabled, boolean IsKilled);
 
    public boolean deleteproject (int IDProject);
 
    public boolean updateproject (int IDProject, int IDOwner, String
Title,
    String Description, int Privilege, boolean IsEnabled, boolean
IsKilled);
 
    public boolean enableproject (int IDProject);
 
    public boolean disableproject (int IDProject) ;
}
// ---------- End Code ---------- //
E non ho alcuna classe che la implementa (per il momento).
Vorrei poter avere l'elenco dei metodi definiti dall'interfaccia per
poter creare da questa una serie di skeleton per le classi.
Se con la reflection non e' possibile faro' un semplice parser di
interfacce (e' relativamente facile da implementare).
