use of com.sun.tools.classfile.AccessFlags in project jdk8u_jdk by JetBrains.
the class AnnotationsElementVisitor method readClass.
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();
}
use of com.sun.tools.classfile.AccessFlags in project jdk8u_jdk by JetBrains.
the class RemoveMethods method main.
public static void main(String[] args) throws Exception {
if (args.length < 2) {
System.err.println("Usage: java RemoveMethods classfile output [method...]");
System.exit(-1);
}
// class file to read
Path input = Paths.get(args[0]);
// class file to write, if directory then use the name of the input
Path output = Paths.get(args[1]);
if (Files.isDirectory(output))
output = output.resolve(input.getFileName());
// the methods to remove
Set<String> methodsToRemove = new HashSet<>();
int i = 2;
while (i < args.length) methodsToRemove.add(args[i++]);
// read class file
ClassFile cf;
try (InputStream in = Files.newInputStream(input)) {
cf = ClassFile.read(in);
}
final int magic = cf.magic;
final int major_version = cf.major_version;
final int minor_version = cf.minor_version;
final ConstantPool cp = cf.constant_pool;
final AccessFlags access_flags = cf.access_flags;
final int this_class = cf.this_class;
final int super_class = cf.super_class;
final int[] interfaces = cf.interfaces;
final Field[] fields = cf.fields;
final Attributes class_attrs = cf.attributes;
// remove the requested methods, no signature check at this time
Method[] methods = cf.methods;
i = 0;
while (i < methods.length) {
Method m = methods[i];
String name = m.getName(cp);
if (methodsToRemove.contains(name)) {
int len = methods.length;
Method[] newMethods = new Method[len - 1];
if (i > 0)
System.arraycopy(methods, 0, newMethods, 0, i);
int after = methods.length - i - 1;
if (after > 0)
System.arraycopy(methods, i + 1, newMethods, i, after);
methods = newMethods;
String paramTypes = m.descriptor.getParameterTypes(cp);
System.out.format("Removed method %s%s from %s%n", name, paramTypes, cf.getName());
continue;
}
i++;
}
// TBD, prune constant pool of entries that are no longer referenced
// re-write class file
cf = new ClassFile(magic, minor_version, major_version, cp, access_flags, this_class, super_class, interfaces, fields, methods, class_attrs);
try (OutputStream out = Files.newOutputStream(output)) {
new ClassWriter().write(cf, out);
}
}
use of com.sun.tools.classfile.AccessFlags in project ceylon-compiler by ceylon.
the class ClassWriter method writeField.
protected void writeField(Field f) {
if (!options.checkAccess(f.access_flags))
return;
AccessFlags flags = f.access_flags;
writeModifiers(flags.getFieldModifiers());
Signature_attribute sigAttr = getSignature(f.attributes);
if (sigAttr == null)
print(getJavaFieldType(f.descriptor));
else {
try {
Type t = sigAttr.getParsedSignature().getType(constant_pool);
print(getJavaName(t.toString()));
} catch (ConstantPoolException e) {
// report error?
// fall back on non-generic descriptor
print(getJavaFieldType(f.descriptor));
}
}
print(" ");
print(getFieldName(f));
if (options.showConstants && !options.compat) {
// BUG 4111861 print static final field contents
Attribute a = f.attributes.get(Attribute.ConstantValue);
if (a instanceof ConstantValue_attribute) {
print(" = ");
ConstantValue_attribute cv = (ConstantValue_attribute) a;
print(getConstantValue(f.descriptor, cv.constantvalue_index));
}
}
print(";");
println();
indent(+1);
if (options.showInternalSignatures)
println("Signature: " + getValue(f.descriptor));
if (options.verbose && !options.compat)
writeList("flags: ", flags.getFieldFlags(), NEWLINE);
if (options.showAllAttrs) {
for (Attribute attr : f.attributes) attrWriter.write(f, attr, constant_pool);
println();
}
indent(-1);
if (options.showDisassembled || options.showLineAndLocalVariableTables)
println();
}
use of com.sun.tools.classfile.AccessFlags in project ceylon-compiler by ceylon.
the class AttributeWriter method visitInnerClasses.
public Void visitInnerClasses(InnerClasses_attribute attr, Void ignore) {
boolean first = true;
if (options.compat) {
writeInnerClassHeader();
first = false;
}
for (int i = 0; i < attr.classes.length; i++) {
InnerClasses_attribute.Info info = attr.classes[i];
//access
AccessFlags access_flags = info.inner_class_access_flags;
if (options.compat) {
// BUG 6622215: javap ignores certain relevant access flags
access_flags = access_flags.ignore(ACC_STATIC | ACC_PROTECTED | ACC_PRIVATE | ACC_INTERFACE | ACC_SYNTHETIC | ACC_ENUM);
// BUG 6622232: javap gets whitespace confused
print(" ");
}
if (options.checkAccess(access_flags)) {
if (first) {
writeInnerClassHeader();
first = false;
}
print(" ");
for (String name : access_flags.getInnerClassModifiers()) print(name + " ");
if (info.inner_name_index != 0) {
print("#" + info.inner_name_index + "= ");
}
print("#" + info.inner_class_info_index);
if (info.outer_class_info_index != 0) {
print(" of #" + info.outer_class_info_index);
}
print("; //");
if (info.inner_name_index != 0) {
print(getInnerName(constant_pool, info) + "=");
}
constantWriter.write(info.inner_class_info_index);
if (info.outer_class_info_index != 0) {
print(" of ");
constantWriter.write(info.outer_class_info_index);
}
println();
}
}
if (!first)
indent(-1);
return null;
}
use of com.sun.tools.classfile.AccessFlags in project ceylon-compiler by ceylon.
the class ClassWriter method write.
public void write(ClassFile cf) {
setClassFile(cf);
if ((options.sysInfo || options.verbose) && !options.compat) {
if (uri != null) {
if (uri.getScheme().equals("file"))
println("Classfile " + uri.getPath());
else
println("Classfile " + uri);
}
indent(+1);
if (lastModified != -1) {
Date lm = new Date(lastModified);
DateFormat df = DateFormat.getDateInstance();
if (size > 0) {
println("Last modified " + df.format(lm) + "; size " + size + " bytes");
} else {
println("Last modified " + df.format(lm));
}
} else if (size > 0) {
println("Size " + size + " bytes");
}
if (digestName != null && digest != null) {
StringBuilder sb = new StringBuilder();
for (byte b : digest) sb.append(String.format("%02x", b));
println(digestName + " checksum " + sb);
}
}
Attribute sfa = cf.getAttribute(Attribute.SourceFile);
if (sfa instanceof SourceFile_attribute) {
println("Compiled from \"" + getSourceFile((SourceFile_attribute) sfa) + "\"");
}
if ((options.sysInfo || options.verbose) && !options.compat) {
indent(-1);
}
String name = getJavaName(classFile);
AccessFlags flags = cf.access_flags;
writeModifiers(flags.getClassModifiers());
if (classFile.isClass())
print("class ");
else if (classFile.isInterface())
print("interface ");
print(name);
Signature_attribute sigAttr = getSignature(cf.attributes);
if (sigAttr == null) {
// use info from class file header
if (classFile.isClass() && classFile.super_class != 0) {
String sn = getJavaSuperclassName(cf);
if (!sn.equals("java.lang.Object")) {
print(" extends ");
print(sn);
}
}
for (int i = 0; i < classFile.interfaces.length; i++) {
print(i == 0 ? (classFile.isClass() ? " implements " : " extends ") : ",");
print(getJavaInterfaceName(classFile, i));
}
} else {
try {
Type t = sigAttr.getParsedSignature().getType(constant_pool);
JavaTypePrinter p = new JavaTypePrinter(classFile.isInterface());
// FieldType and a ClassSignatureType that only contains a superclass type.
if (t instanceof Type.ClassSigType) {
print(p.print(t));
} else if (options.verbose || !t.isObject()) {
print(" extends ");
print(p.print(t));
}
} catch (ConstantPoolException e) {
print(report(e));
}
}
if (options.verbose) {
println();
indent(+1);
attrWriter.write(cf, cf.attributes, constant_pool);
println("minor version: " + cf.minor_version);
println("major version: " + cf.major_version);
if (!options.compat)
writeList("flags: ", flags.getClassFlags(), NEWLINE);
indent(-1);
constantWriter.writeConstantPool();
} else {
print(" ");
}
println("{");
indent(+1);
writeFields();
writeMethods();
indent(-1);
println("}");
}
Aggregations