本文整理汇总了Java中com.sun.tools.classfile.ClassFile.read方法的典型用法代码示例。如果您正苦于以下问题:Java ClassFile.read方法的具体用法?Java ClassFile.read怎么用?Java ClassFile.read使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.sun.tools.classfile.ClassFile
的用法示例。
在下文中一共展示了ClassFile.read方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: checkClassFile
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
void checkClassFile(final File cfile, String methodToFind, int[][] expectedLNT) throws Exception {
ClassFile classFile = ClassFile.read(cfile);
boolean methodFound = false;
for (Method method : classFile.methods) {
if (method.getName(classFile.constant_pool).equals(methodToFind)) {
methodFound = true;
Code_attribute code = (Code_attribute) method.attributes.get("Code");
LineNumberTable_attribute lnt =
(LineNumberTable_attribute) code.attributes.get("LineNumberTable");
Assert.check(lnt.line_number_table_length == expectedLNT.length,
"The LineNumberTable found has a length different to the expected one");
int i = 0;
for (LineNumberTable_attribute.Entry entry: lnt.line_number_table) {
Assert.check(entry.line_number == expectedLNT[i][0] &&
entry.start_pc == expectedLNT[i][1],
"LNT entry at pos " + i + " differ from expected." +
"Found " + entry.line_number + ":" + entry.start_pc +
". Expected " + expectedLNT[i][0] + ":" + expectedLNT[i][1]);
i++;
}
}
}
Assert.check(methodFound, "The seek method was not found");
}
示例2: verifyDefaultBody
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
void verifyDefaultBody(String classFile) {
String workDir = System.getProperty("test.classes");
File file = new File(workDir, classFile);
try {
final ClassFile cf = ClassFile.read(file);
for (Method m : cf.methods) {
Code_attribute codeAttr = (Code_attribute)m.attributes.get(Attribute.Code);
for (Instruction instr : codeAttr.getInstructions()) {
if (instr.getOpcode() == Opcode.INVOKESPECIAL) {
int pc_index = instr.getShort(1);
CPRefInfo ref = (CPRefInfo)cf.constant_pool.get(pc_index);
String className = ref.getClassName();
if (className.equals("BaseInterface"))
throw new IllegalStateException("Must not directly refer to TestedInterface");
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw new Error("error reading " + file +": " + e);
}
}
示例3: verifyInvokerBytecodeGenerator
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
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);
}
}
示例4: checkDebugAttributes
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
private void checkDebugAttributes(byte[] strippedClassFile) throws IOException, ConstantPoolException {
ClassFile classFile = ClassFile.read(new ByteArrayInputStream(strippedClassFile));
String[] debugAttributes = new String[]{
Attribute.LineNumberTable,
Attribute.LocalVariableTable,
Attribute.LocalVariableTypeTable
};
for (Method method : classFile.methods) {
String methodName = method.getName(classFile.constant_pool);
Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
for (String attr : debugAttributes) {
if (code.attributes.get(attr) != null) {
throw new AssertionError("Debug attribute was not removed: " + attr +
" from method " + classFile.getName() + "#" + methodName);
}
}
}
}
示例5: checkClassFile
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
void checkClassFile(final Path path) throws Exception {
ClassFile classFile = ClassFile.read(
new BufferedInputStream(Files.newInputStream(path)));
constantPool = classFile.constant_pool;
utf8Index = constantPool.getUTF8Index("STR_TO_LOOK_FOR");
for (Method method: classFile.methods) {
if (method.getName(constantPool).equals("methodToLookFor")) {
Code_attribute codeAtt = (Code_attribute)method.attributes.get(Attribute.Code);
for (Instruction inst: codeAtt.getInstructions()) {
inst.accept(codeVisitor, null);
}
}
}
Assert.check(numberOfRefToStr == 1,
"There should only be one reference to a CONSTANT_String_info structure in the generated code");
}
示例6: readFrom
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
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);
}
}
示例7: verifyASM
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
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);
}
}
示例8: testSimpleAnnotation
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
@Test
public void testSimpleAnnotation(Path base) throws Exception {
Path moduleSrc = base.resolve("module-src");
Path m1 = moduleSrc.resolve("m1x");
tb.writeJavaFiles(m1,
"@Deprecated module m1x { }");
Path modulePath = base.resolve("module-path");
Files.createDirectories(modulePath);
new JavacTask(tb)
.options("--module-source-path", moduleSrc.toString())
.outdir(modulePath)
.files(findJavaFiles(m1))
.run()
.writeAll();
ClassFile cf = ClassFile.read(modulePath.resolve("m1x").resolve("module-info.class"));
RuntimeVisibleAnnotations_attribute annotations = (RuntimeVisibleAnnotations_attribute) cf.attributes.map.get(Attribute.RuntimeVisibleAnnotations);
if (annotations == null || annotations.annotations.length != 1) {
throw new AssertionError("Annotations not correct!");
}
}
示例9: checkReference
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
void checkReference() throws IOException, ConstantPoolException {
File file = new File("A.class");
ClassFile classFile = ClassFile.read(file);
for (int i = 1;
i < classFile.constant_pool.size() - 1;
i += classFile.constant_pool.get(i).size()) {
for (int j = i + classFile.constant_pool.get(i).size();
j < classFile.constant_pool.size();
j += classFile.constant_pool.get(j).size()) {
if (classFile.constant_pool.get(i).toString().
equals(classFile.constant_pool.get(j).toString())) {
throw new AssertionError(
"Duplicate entries in the constant pool at positions " +
i + " and " + j);
}
}
}
}
示例10: findEntries
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
static Entry[] findEntries() throws IOException, ConstantPoolException {
ClassFile self = ClassFile.read(FinallyLineNumberTest.class.getResourceAsStream("FinallyLineNumberTest.class"));
for (Method m : self.methods) {
if ("method".equals(m.getName(self.constant_pool))) {
Code_attribute code_attribute = (Code_attribute)m.attributes.get(Attribute.Code);
for (Attribute at : code_attribute.attributes) {
if (Attribute.LineNumberTable.equals(at.getName(self.constant_pool))) {
return ((LineNumberTable_attribute)at).line_number_table;
}
}
}
}
return null;
}
示例11: readClassFile
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
protected ClassFile readClassFile(JarFile jarfile, JarEntry e) throws IOException {
InputStream is = null;
try {
is = jarfile.getInputStream(e);
return ClassFile.read(is);
} catch (ConstantPoolException ex) {
throw new ClassFileError(ex);
} finally {
if (is != null)
is.close();
}
}
示例12: analyzeDependencies
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
/**
* 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);
}
}
}
}
}
}
示例13: analyzeClassFile
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
void analyzeClassFile(File path) throws Exception {
ClassFile classFile = ClassFile.read(path);
InnerClasses_attribute innerClasses =
(InnerClasses_attribute) classFile.attributes.get(Attribute.InnerClasses);
for (Info classInfo : innerClasses.classes) {
Assert.check(!classInfo.inner_class_access_flags.is(AccessFlags.ACC_STRICT),
"Inner classes attribute must not have the ACC_STRICT flag set");
}
}
示例14: verifySourceFileAttribute
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
/** Check the SourceFileAttribute is the simple name of the original source file. */
void verifySourceFileAttribute(File f) {
System.err.println("verify: " + f);
try {
ClassFile cf = ClassFile.read(f);
SourceFile_attribute sfa = (SourceFile_attribute) cf.getAttribute(Attribute.SourceFile);
String found = sfa.getSourceFile(cf.constant_pool);
String expect = f.getName().replaceAll("([$.].*)?\\.class", ".java");
if (!expect.equals(found)) {
error("bad value found: " + found + ", expected: " + expect);
}
} catch (Exception e) {
error("error reading " + f +": " + e);
}
}
示例15: checkNoBridgeOnDefaults
import com.sun.tools.classfile.ClassFile; //导入方法依赖的package包/类
void checkNoBridgeOnDefaults(File f) {
System.err.println("check: " + f);
try {
ClassFile cf = ClassFile.read(f);
for (Method m : cf.methods) {
String mname = m.getName(cf.constant_pool);
if (mname.equals(TEST_METHOD_NAME)) {
throw new Error("unexpected bridge method found " + m);
}
}
} catch (Exception e) {
e.printStackTrace();
throw new Error("error reading " + f +": " + e);
}
}