001: // Copyright (c) 1997 Per M.A. Bothner.
002: // This is free software; for terms and warranty disclaimer see ./COPYING.
003:
004: package gnu.bytecode;
005:
006: import java.io.*;
007:
008: /** Access flags. */
009: /* When using JDK 1.1, replace this class by java.lang.reflec.Modifiers. */
010:
011: public class Access {
012: static public final short PUBLIC = 0x0001;
013: static public final short PRIVATE = 0x0002;
014: static public final short PROTECTED = 0x0004;
015: static public final short STATIC = 0x0008;
016: static public final short FINAL = 0x0010;
017: static public final short SUPER = 0x0020;
018: static public final short SYNCHRONIZED = 0x0020;
019: static public final short VOLATILE = 0x0040;
020: static public final short TRANSIENT = 0x0080;
021: static public final short NATIVE = 0x0100;
022: static public final short INTERFACE = 0x0200;
023: static public final short ABSTRACT = 0x0400;
024:
025: public static String toString(int flags) {
026: StringBuffer buf = new StringBuffer();
027: if ((flags & PUBLIC) != 0)
028: buf.append(" public");
029: if ((flags & PRIVATE) != 0)
030: buf.append(" private");
031: if ((flags & PROTECTED) != 0)
032: buf.append(" protected");
033: if ((flags & STATIC) != 0)
034: buf.append(" static");
035: if ((flags & FINAL) != 0)
036: buf.append(" final");
037: if ((flags & SYNCHRONIZED) != 0)
038: buf.append(" synchronized");
039: if ((flags & VOLATILE) != 0)
040: buf.append(" volatile");
041: if ((flags & TRANSIENT) != 0)
042: buf.append(" transient");
043: if ((flags & NATIVE) != 0)
044: buf.append(" native");
045: if ((flags & INTERFACE) != 0)
046: buf.append(" interface");
047: if ((flags & ABSTRACT) != 0)
048: buf.append(" abstract");
049: int unknown = flags
050: & ~(PUBLIC | PRIVATE | PROTECTED | STATIC | FINAL
051: | SYNCHRONIZED | VOLATILE | NATIVE | INTERFACE | ABSTRACT);
052: if (unknown != 0) {
053: buf.append(" 0x");
054: buf.append(Integer.toHexString(unknown));
055: }
056: return buf.toString();
057: }
058:
059: /**
060: @return true if code in class c can access method m, with the first
061: argument of the call being receiver.
062:
063: receiver is null if the call is static.
064: */
065: public static boolean legal(ClassType c, Method m, Type receiver) {
066: int mod = m.getModifiers();
067: if ((mod & PUBLIC) != 0)
068: return true;
069:
070: ClassType target = m.getDeclaringClass();
071:
072: if ((mod & PRIVATE) != 0)
073: return c == target;
074:
075: // clone is the only method overriden for arrays, where it is public.
076: // (JLS-2 10.7)
077: if (receiver != null && receiver.isArray()
078: && m.getName().equals("clone"))
079: return true;
080:
081: // DEFAULT (PACKAGE) OR PROTECTED
082: if (c.getPackageName().equals(target.getPackageName()))
083: return true;
084:
085: // PROTECTED
086: return (mod & PROTECTED) != 0 && c.isSubclass(target)
087: && receiver.isSubtype(c);
088: }
089:
090: /***
091: * Returns true if classType is legally accessible in the given package.
092: */
093: public static boolean legal(ClassType classType, String packageName) {
094: int mod = classType.getModifiers();
095:
096: if ((mod & PUBLIC) != 0)
097: return true;
098:
099: if ((mod & PRIVATE) != 0)
100: return false;
101:
102: if (!packageName.equals(classType.getPackageName()))
103: return false;
104:
105: return true;
106: }
107: }
|