use of com.sun.tools.classfile.ClassFile in project jdk8u_jdk by JetBrains.
the class CheckDeps method analyzeDependencies.
/**
* Analyze the dependencies of all classes in the given JAR file. The
* method updates knownTypes and unknownRefs as part of the analysis.
*/
static void analyzeDependencies(Path jarpath) throws Exception {
System.out.format("Analyzing %s%n", jarpath);
try (JarFile jf = new JarFile(jarpath.toFile())) {
Enumeration<JarEntry> entries = jf.entries();
while (entries.hasMoreElements()) {
JarEntry e = entries.nextElement();
String name = e.getName();
if (name.endsWith(".class")) {
ClassFile cf = ClassFile.read(jf.getInputStream(e));
for (Dependency d : finder.findDependencies(cf)) {
String origin = toClassName(d.getOrigin().getName());
String target = toClassName(d.getTarget().getName());
// origin is now known
unknownRefs.remove(origin);
knownTypes.add(origin);
// if the target is not known then record the reference
if (!knownTypes.contains(target)) {
Set<String> refs = unknownRefs.get(target);
if (refs == null) {
// first time seeing this unknown type
refs = new HashSet<>();
unknownRefs.put(target, refs);
}
refs.add(origin);
}
}
}
}
}
}
use of com.sun.tools.classfile.ClassFile in project ceylon-compiler by ceylon.
the class OverrideBridge method compile.
static Map<ClassFile, List<Method>> compile(Implementation implB, Implementation implC, Implementation implD, String destPath) throws Exception {
File destDir = new File(workDir, destPath);
destDir.mkdir();
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
JavaSource source = new JavaSource(implB, implC, implD);
JavacTask ct = (JavacTask) tool.getTask(null, null, null, Arrays.asList("-d", destPath), null, Arrays.asList(source));
ct.generate();
Map<ClassFile, List<Method>> members = new HashMap<>();
addMembers(destDir, members);
return members;
}
use of com.sun.tools.classfile.ClassFile in project jdk8u_jdk by JetBrains.
the class LambdaAsm method verifyASM.
static void verifyASM() throws Exception {
ClassWriter cw = new ClassWriter(0);
cw.visit(V1_8, ACC_PUBLIC, "X", null, "java/lang/Object", null);
MethodVisitor mv = cw.visitMethod(ACC_STATIC, "foo", "()V", null, null);
mv.visitMaxs(2, 1);
mv.visitMethodInsn(INVOKESTATIC, "java/util/function/Function.class", "identity", "()Ljava/util/function/Function;", true);
mv.visitInsn(RETURN);
cw.visitEnd();
byte[] carray = cw.toByteArray();
// for debugging
// write((new File("X.class")).toPath(), carray, CREATE, TRUNCATE_EXISTING);
// verify using javap/classfile reader
ClassFile cf = ClassFile.read(new ByteArrayInputStream(carray));
int mcount = checkMethod(cf, "foo");
if (mcount < 1) {
throw new RuntimeException("unexpected method count, expected 1" + "but got " + mcount);
}
}
use of com.sun.tools.classfile.ClassFile in project jdk8u_jdk by JetBrains.
the class LambdaAsm method verifyInvokerBytecodeGenerator.
static void verifyInvokerBytecodeGenerator() throws Exception {
int count = 0;
int mcount = 0;
try (DirectoryStream<Path> ds = newDirectoryStream(new File(".").toPath(), // filter in lambda proxy classes
"A$I$$Lambda$?.class")) {
for (Path p : ds) {
System.out.println(p.toFile());
ClassFile cf = ClassFile.read(p.toFile());
// Check those methods implementing Supplier.get
mcount += checkMethod(cf, "get");
count++;
}
}
if (count < 3) {
throw new RuntimeException("unexpected number of files, " + "expected atleast 3 files, but got only " + count);
}
if (mcount < 3) {
throw new RuntimeException("unexpected number of methods, " + "expected atleast 3 methods, but got only " + mcount);
}
}
use of com.sun.tools.classfile.ClassFile in project jdk8u_jdk by JetBrains.
the class AnnotationsElementVisitor method readFrom.
public Element readFrom(InputStream in) throws IOException {
try {
this.in = in;
ClassFile c = ClassFile.read(in);
// read the file header
if (c.magic != 0xCAFEBABE) {
throw new RuntimeException("bad magic number " + Integer.toHexString(c.magic));
}
cfile.setAttr("magic", "" + c.magic);
int minver = c.minor_version;
int majver = c.major_version;
cfile.setAttr("minver", "" + minver);
cfile.setAttr("majver", "" + majver);
readCP(c);
readClass(c);
return result();
} catch (InvalidDescriptor | ConstantPoolException ex) {
throw new IOException("Fatal error", ex);
}
}
Aggregations