/** * Checks access of class, field or method. */ public boolean checkAccess(AccessFlags flags){ boolean isPublic = flags.is(AccessFlags.ACC_PUBLIC); boolean isProtected = flags.is(AccessFlags.ACC_PROTECTED); boolean isPrivate = flags.is(AccessFlags.ACC_PRIVATE); boolean isPackage = !(isPublic || isProtected || isPrivate); if ((showAccess == AccessFlags.ACC_PUBLIC) && (isProtected || isPrivate || isPackage)) return false; else if ((showAccess == AccessFlags.ACC_PROTECTED) && (isPrivate || isPackage)) return false; else if ((showAccess == 0) && (isPrivate)) return false; else return true; }
private void readClass(ClassFile c) throws IOException, ConstantPoolException, InvalidDescriptor { klass = new Element("Class"); cfile.add(klass); String thisk = c.getName(); klass.setAttr("name", thisk); AccessFlags af = new AccessFlags(c.access_flags.flags); klass.setAttr("flags", flagString(af, klass)); if (!"java/lang/Object".equals(thisk)) { klass.setAttr("super", c.getSuperclassName()); } for (int i : c.interfaces) { klass.add(new Element("Interface", "name", getCpString(i))); } readFields(c, klass); readMethods(c, klass); readAttributesFor(c, c.attributes, klass); klass.trimToSize(); }
protected String flagString(AccessFlags af, String kind) { Set<String> mods = null; switch (kind) { case "Class": mods = af.getClassFlags(); break; case "InnerClass": mods = af.getInnerClassFlags(); break; case "Field": mods = af.getFieldFlags(); break; case "Method": mods = af.getMethodFlags(); break; default: throw new RuntimeException("should not reach here"); } StringBuilder sb = new StringBuilder(); for (String x : mods) { sb.append(x.substring(x.indexOf('_') + 1).toLowerCase()).append(" "); } return sb.toString().trim(); }
public static void main(String[] args) throws IOException, ConstantPoolException { Path outdir = Paths.get("."); ToolBox tb = new ToolBox(); final Path moduleInfo = Paths.get("module-info.java"); tb.writeFile(moduleInfo, "module test_module{}"); new JavacTask(tb) .outdir(outdir) .files(moduleInfo) .run(); AccessFlags accessFlags = ClassFile.read(outdir.resolve("module-info.class")) .access_flags; if (!accessFlags.is(AccessFlags.ACC_MODULE)) { throw new RuntimeException("Classfile doesn't have module access flag"); } }
private void readClass(ClassFile c) throws IOException, ConstantPoolException, InvalidDescriptor { klass = new Element("Class"); cfile.add(klass); String thisk = c.getName(); klass.setAttr("name", thisk); AccessFlags af = new AccessFlags(c.access_flags.flags); klass.setAttr("flags", flagString(af, klass)); if (!"java/lang/Object".equals(thisk)) { if (c.super_class != 0) { klass.setAttr("super", c.getSuperclassName()); } } for (int i : c.interfaces) { klass.add(new Element("Interface", "name", getCpString(i))); } readFields(c, klass); readMethods(c, klass); readAttributesFor(c, c.attributes, klass); klass.trimToSize(); }
boolean isNativeClass(JarFile jar, JarEntry entry) throws IOException, ConstantPoolException { String name = entry.getName(); if (name.startsWith("META-INF") || !name.endsWith(".class")) return false; //String className = name.substring(0, name.length() - 6).replace("/", "."); //System.err.println("check " + className); InputStream in = jar.getInputStream(entry); ClassFile cf = ClassFile.read(in); for (int i = 0; i < cf.methods.length; i++) { Method m = cf.methods[i]; if (m.access_flags.is(AccessFlags.ACC_NATIVE)) { // System.err.println(className); return true; } } return false; }
boolean isNativeClass(JarFile jar, JarEntry entry) throws IOException, ConstantPoolException { String name = entry.getName(); if (name.startsWith("META-INF") || !name.endsWith(".class")) return false; //String className = name.substring(0, name.length() - 6).replace("/", "."); //System.err.println("check " + className); InputStream in = jar.getInputStream(entry); ClassFile cf = ClassFile.read(in); in.close(); for (int i = 0; i < cf.methods.length; i++) { Method m = cf.methods[i]; if (m.access_flags.is(AccessFlags.ACC_NATIVE)) { // System.err.println(className); return true; } } return false; }