/[classpath]/classpath/java/lang/Class.java
ViewVC logotype

Diff of /classpath/java/lang/Class.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.6 by cbj, Sat Apr 5 19:42:42 2003 UTC revision 1.7 by cbj, Mon Apr 7 11:41:19 2003 UTC
# Line 50  import java.security.Permissions; Line 50  import java.security.Permissions;
50  import java.security.ProtectionDomain;  import java.security.ProtectionDomain;
51  import java.util.ArrayList;  import java.util.ArrayList;
52  import java.util.Arrays;  import java.util.Arrays;
53    import java.util.HashMap;
54    import java.util.HashSet;
55  import gnu.java.lang.ClassHelper;  import gnu.java.lang.ClassHelper;
56    
57  /*  /*
# Line 475  public final class Class implements Seri Line 477  public final class Class implements Seri
477     */     */
478    public Object[] getSigners()    public Object[] getSigners()
479    {    {
480      return (Object[]) signers.clone ();      return signers == null ? null : (Object[]) signers.clone ();
481    }    }
482    
483    /**    /**
# Line 564  public final class Class implements Seri Line 566  public final class Class implements Seri
566     * Like <code>getFields()</code> but without the security checks.     * Like <code>getFields()</code> but without the security checks.
567     */     */
568    private Field[] internalGetFields() {    private Field[] internalGetFields() {
569      ArrayList list = new ArrayList();      HashSet set = new HashSet();
570      list.addAll(Arrays.asList(getDeclaredFields(true)));      set.addAll(Arrays.asList(getDeclaredFields(true)));
571      if (isInterface()) {      Class[] interfaces = getInterfaces();
572        Class[] interfaces = getInterfaces();      for (int i = 0; i < interfaces.length; i++)
573        for (int i = 0; i < interfaces.length; i++)          set.addAll(Arrays.asList(interfaces[i].internalGetFields()));
574          list.addAll(Arrays.asList(interfaces[i].internalGetFields()));      Class superClass = getSuperclass();
575      } else {      if (superClass != null)
576        Class superClass = getSuperclass();          set.addAll(Arrays.asList(superClass.internalGetFields()));
577        if (superClass != null)      return (Field[])set.toArray(new Field[set.size()]);
         list.addAll(Arrays.asList(superClass.internalGetFields()));  
     }  
     return (Field[])list.toArray(new Field[list.size()]);  
578    }    }
579    
580    /**    /**
581     * Get all the public methods declared in this class or inherited from     * Get all the public methods declared in this class or inherited from
582     * superclasses. This returns an array of length 0 if there are no methods,     * superclasses. This returns an array of length 0 if there are no methods,
583     * including for primitive types. This does include the implicit methods of     * including for primitive types. This does not include the implicit
584     * arrays and interfaces which mirror methods of Object, nor does it     * methods of interfaces which mirror methods of Object, nor does it
585     * include constructors or the class initialization methods. The Virtual     * include constructors or the class initialization methods. The Virtual
586     * Machine allows multiple methods with the same signature but differing     * Machine allows multiple methods with the same signature but differing
587     * return types; all such methods are in the returned array. A security     * return types; all such methods are in the returned array. A security
# Line 596  public final class Class implements Seri Line 595  public final class Class implements Seri
595     */     */
596    public Method[] getMethods() {    public Method[] getMethods() {
597      memberAccessCheck(Member.PUBLIC);      memberAccessCheck(Member.PUBLIC);
598        // NOTE the API docs claim that no methods are returned for arrays,
599        // but Sun's implementation *does* return the public methods of Object
600        // (as would be expected), so we follow their implementation instead
601        // of their documentation.
602      return internalGetMethods();      return internalGetMethods();
603    }    }
604    
# Line 604  public final class Class implements Seri Line 607  public final class Class implements Seri
607     */     */
608    private Method[] internalGetMethods()    private Method[] internalGetMethods()
609    {    {
610      java.util.HashMap map = new java.util.HashMap();      HashMap map = new HashMap();
611      Method[] methods;      Method[] methods;
612      Class[] interfaces = getInterfaces();      Class[] interfaces = getInterfaces();
613      for(int i = 0; i < interfaces.length; i++)      for(int i = 0; i < interfaces.length; i++)
# Line 712  public final class Class implements Seri Line 715  public final class Class implements Seri
715     */     */
716    public Field getField(String name) throws NoSuchFieldException {    public Field getField(String name) throws NoSuchFieldException {
717      memberAccessCheck(Member.PUBLIC);      memberAccessCheck(Member.PUBLIC);
718        Field field = internalGetField(name);
719        if(field != null)
720            return field;
721        throw new NoSuchFieldException();
722      }
723    
724      /**
725       * Like <code>getField(String)</code> but without the security checks and returns null
726       * instead of throwing NoSuchFieldException.
727       */
728      private Field internalGetField(String name) {
729      Field[] fields = getDeclaredFields(true);      Field[] fields = getDeclaredFields(true);
730      for (int i = 0; i < fields.length; i++) {      for (int i = 0; i < fields.length; i++) {
731        Field field = fields[i];          Field field = fields[i];
732        if (field.getName().equals(name))          if (field.getName().equals(name))
733          return field;              return field;
734      }      }
735      Class[] interfaces = getInterfaces();      Class[] interfaces = getInterfaces();
736      for (int i = 0; i < interfaces.length; i++) {      for (int i = 0; i < interfaces.length; i++) {
737        try {          Field field = interfaces[i].internalGetField(name);
738          return interfaces[i].getField(name);          if(field != null)
739        } catch (NoSuchFieldException e) {              return field;
       }  
740      }      }
741      Class superclass = getSuperclass();      Class superClass = getSuperclass();
742      if (superclass != null)      if (superClass != null)
743        return superclass.getField(name);          return superClass.internalGetField(name);
744      throw new NoSuchFieldException();      return null;
745    }    }
746    
747    /**    /**
748     * Get a public method declared or inherited in this class, where name is     * Get a public method declared or inherited in this class, where name is
749     * its simple name. The implicit methods of Object are not available from     * its simple name. The implicit methods of Object are not available from
750     * arrays or interfaces.  Constructors (named "<init>" in the class file)     * interfaces.  Constructors (named "<init>" in the class file) and class
751     * and class initializers (name "<clinit>") are not available.  The Virtual     * initializers (name "<clinit>") are not available.  The Virtual
752     * Machine allows multiple methods with the same signature but differing     * Machine allows multiple methods with the same signature but differing
753     * return types, and the class can inherit multiple methods of the same     * return types, and the class can inherit multiple methods of the same
754     * return type; in such a case the most specific return types are favored,     * return type; in such a case the most specific return types are favored,
# Line 756  public final class Class implements Seri Line 769  public final class Class implements Seri
769    public Method getMethod(String name, Class[] args)    public Method getMethod(String name, Class[] args)
770          throws NoSuchMethodException {          throws NoSuchMethodException {
771      memberAccessCheck(Member.PUBLIC);      memberAccessCheck(Member.PUBLIC);
772      for (Class c = this; c != null; c = c.getSuperclass()) {      Method method = internalGetMethod(name, args);
773          Method match = matchMethod(c.getDeclaredMethods(true), name, args);      if (method != null)
774            return method;
775        throw new NoSuchMethodException();
776      }
777    
778      /**
779       * Like <code>getMethod(String,Class[])</code> but without the security
780       * checks and returns null instead of throwing NoSuchMethodException.
781       */
782      public Method internalGetMethod(String name, Class[] args) {
783        Method match = matchMethod(getDeclaredMethods(true), name, args);
784        if (match != null)
785            return match;
786        Class superClass = getSuperclass();
787        if (superClass != null) {
788            match = superClass.internalGetMethod(name, args);
789            if(match != null)
790                return match;
791        }
792        Class[] interfaces = getInterfaces();
793        for (int i = 0; i < interfaces.length; i++) {
794            match = interfaces[i].internalGetMethod(name, args);
795          if (match != null)          if (match != null)
796              return match;              return match;
         Class[] interfaces = c.getInterfaces();  
         for (int i = 0; i < interfaces.length; i++)  
         {  
             match = matchMethod(interfaces[i].getDeclaredMethods(true), name, args);  
             if (match != null)  
                 return match;  
         }  
797      }      }
798      throw new NoSuchMethodException();      return null;
799    }    }
800    
801    /**    /**
# Line 811  public final class Class implements Seri Line 838  public final class Class implements Seri
838      if (types1.length != types2.length)      if (types1.length != types2.length)
839        return false;        return false;
840      for (int i = 0; i < types1.length; i++) {      for (int i = 0; i < types1.length; i++) {
841        if (!types1[i].equals(types2[i]))        if (types1[i] != types2[i])
842          return false;          return false;
843      }      }
844      return true;      return true;

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.7

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26