本文整理汇总了Java中com.sun.tools.classfile.ClassFile类的典型用法代码示例。如果您正苦于以下问题:Java ClassFile类的具体用法?Java ClassFile怎么用?Java ClassFile使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ClassFile类属于com.sun.tools.classfile包,在下文中一共展示了ClassFile类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assertAttributePresent
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
private void assertAttributePresent(ClassFile classFile, String fileName) throws Exception {
//We need to count attributes with the same names because there is no appropriate API in the ClassFile.
List<SourceFile_attribute> sourceFileAttributes = new ArrayList<>();
for (Attribute a : classFile.attributes.attrs) {
if (Attribute.SourceFile.equals(a.getName(classFile.constant_pool))) {
sourceFileAttributes.add((SourceFile_attribute) a);
}
}
assertEquals(sourceFileAttributes.size(), 1, "Should be the only SourceFile attribute");
SourceFile_attribute attribute = sourceFileAttributes.get(0);
assertEquals(classFile.constant_pool.getUTF8Info(attribute.attribute_name_index).value,
Attribute.SourceFile, "Incorrect attribute name");
assertEquals(classFile.constant_pool.getUTF8Info(attribute.sourcefile_index).value, fileName,
"Incorrect source file name");
assertEquals(attribute.attribute_length, 2, "Incorrect attribute length");
}
示例2: checkClassFile
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
void checkClassFile(final File cfile, String methodToFind) 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,
foundLNTLengthDifferentThanExpMsg);
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, seekMethodNotFoundMsg);
}
示例3: testAnnotation
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
public void testAnnotation(TestResult testResult, ClassFile classFile, Annotation annotation)
throws ConstantPoolException {
testResult.checkEquals(classFile.constant_pool.getUTF8Value(annotation.type_index),
String.format("L%s;", annotationName), "Testing annotation name : " + annotationName);
testResult.checkEquals(annotation.num_element_value_pairs,
elementValues.size(), "Number of element values");
if (!testResult.checkEquals(annotation.num_element_value_pairs, elementValues.size(),
"Number of element value pairs")) {
return;
}
for (int i = 0; i < annotation.num_element_value_pairs; ++i) {
Annotation.element_value_pair pair = annotation.element_value_pairs[i];
testResult.checkEquals(classFile.constant_pool.getUTF8Value(pair.element_name_index),
elementValues.get(i).elementName, "element_name_index : " + elementValues.get(i).elementName);
elementValues.get(i).elementValue.testElementValue(testResult, classFile, pair.value);
}
}
示例4: 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);
}
}
示例5: readClass
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
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();
}
示例6: readCP
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
private void readCP(ClassFile c) throws IOException {
cpool = new Element("ConstantPool", c.constant_pool.size());
ConstantPoolVisitor cpv = new ConstantPoolVisitor(cpool, c,
c.constant_pool.size());
for (int i = 1 ; i < c.constant_pool.size() ; i++) {
try {
cpv.visit(c.constant_pool.get(i), i);
} catch (InvalidIndex ex) {
// can happen periodically when accessing doubles etc. ignore it
// ex.printStackTrace();
}
}
thePool = cpv.getPoolList();
if (verbose) {
for (int i = 0; i < thePool.size(); i++) {
System.out.println("[" + i + "]: " + thePool.get(i));
}
}
if (keepCP) {
cfile.add(cpool);
}
}
示例7: checkClassFile
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
void checkClassFile(final File cfile, String[] methodsToFind) throws Exception {
ClassFile classFile = ClassFile.read(cfile);
int numberOfmethodsFound = 0;
for (String methodToFind : methodsToFind) {
for (Method method : classFile.methods) {
if (method.getName(classFile.constant_pool).equals(methodToFind)) {
numberOfmethodsFound++;
Code_attribute code = (Code_attribute) method.attributes.get("Code");
Assert.check(code.exception_table_length == expectedExceptionTable.length,
"The ExceptionTable found has a length different to the expected one");
int i = 0;
for (Exception_data entry: code.exception_table) {
Assert.check(entry.start_pc == expectedExceptionTable[i][0] &&
entry.end_pc == expectedExceptionTable[i][1] &&
entry.handler_pc == expectedExceptionTable[i][2] &&
entry.catch_type == expectedExceptionTable[i][3],
"Exception table entry at pos " + i + " differ from expected.");
i++;
}
}
}
}
Assert.check(numberOfmethodsFound == 2, "Some seek methods were not found");
}
示例8: run
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
public void run() throws Exception {
ClassFile cf = getClassFile("TypeAnnotationPropagationTest$Test.class");
Method f = null;
for (Method m : cf.methods) {
if (m.getName(cf.constant_pool).contains("f")) {
f = m;
break;
}
}
int idx = f.attributes.getIndex(cf.constant_pool, Attribute.Code);
Code_attribute cattr = (Code_attribute) f.attributes.get(idx);
idx = cattr.attributes.getIndex(cf.constant_pool, Attribute.RuntimeVisibleTypeAnnotations);
RuntimeVisibleTypeAnnotations_attribute attr =
(RuntimeVisibleTypeAnnotations_attribute) cattr.attributes.get(idx);
TypeAnnotation anno = attr.annotations[0];
assertEquals(anno.position.lvarOffset, new int[] {3}, "start_pc");
assertEquals(anno.position.lvarLength, new int[] {8}, "length");
assertEquals(anno.position.lvarIndex, new int[] {1}, "index");
}
示例9: compare
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
public static boolean compare(Map<String, TypeAnnotation.Position> expectedAnnos,
List<TypeAnnotation> actualAnnos, ClassFile cf) throws InvalidIndex, UnexpectedEntry {
if (actualAnnos.size() != expectedAnnos.size()) {
throw new ComparisionException("Wrong number of annotations",
expectedAnnos,
actualAnnos);
}
for (Map.Entry<String, TypeAnnotation.Position> e : expectedAnnos.entrySet()) {
String aName = e.getKey();
TypeAnnotation.Position expected = e.getValue();
TypeAnnotation actual = findAnnotation(aName, actualAnnos, cf);
if (actual == null)
throw new ComparisionException("Expected annotation not found: " + aName);
if (!areEquals(expected, actual.position)) {
throw new ComparisionException("Unexpected position for annotation : " + aName +
"\n Expected: " + expected.toString() +
"\n Found: " + actual.position.toString());
}
}
return true;
}
示例10: main
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
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");
}
}
示例11: verifyBytecode
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
void verifyBytecode() {
File compiledTest = new File("Test.class");
try {
ClassFile cf = ClassFile.read(compiledTest);
BootstrapMethods_attribute bsm_attr =
(BootstrapMethods_attribute)cf
.getAttribute(Attribute.BootstrapMethods);
int length = bsm_attr.bootstrap_method_specifiers.length;
if (length != 1) {
throw new Error("Bad number of method specifiers " +
"in BootstrapMethods attribute: " + length);
}
} catch (Exception e) {
e.printStackTrace();
throw new Error("error reading " + compiledTest +": " + e);
}
}
示例12: checkClassFile
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
void checkClassFile(final File cfile, String methodToFind) 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");
}
示例13: 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!");
}
}
示例14: checkClassFile
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
void checkClassFile(File file)
throws IOException, ConstantPoolException, InvalidDescriptor {
ClassFile classFile = ClassFile.read(file);
ConstantPool constantPool = classFile.constant_pool;
//lets get all the methods in the class file.
for (Method method : classFile.methods) {
for (ElementKey elementKey: aliveRangeMap.keySet()) {
String methodDesc = method.getName(constantPool) +
method.descriptor.getParameterTypes(constantPool).replace(" ", "");
if (methodDesc.equals(elementKey.elem.toString())) {
checkMethod(constantPool, method, aliveRangeMap.get(elementKey));
seenAliveRanges.add(elementKey);
}
}
}
}
示例15: checkReferences
import com.sun.tools.classfile.ClassFile; //导入依赖的package包/类
void checkReferences() throws IOException, ConstantPoolException {
File testClasses = new File(System.getProperty("test.classes"));
File file = new File(testClasses,
CPoolRefClassContainingInlinedCts.class.getName() + ".class");
ClassFile classFile = ClassFile.read(file);
int i = 1;
CPInfo cpInfo;
while (i < classFile.constant_pool.size()) {
cpInfo = classFile.constant_pool.get(i);
if (cpInfo instanceof CONSTANT_Class_info) {
checkClassName(((CONSTANT_Class_info)cpInfo).getName());
}
i += cpInfo.size();
}
if (numberOfReferencedClassesToBeChecked != 16) {
throw new AssertionError("Class reference missing in the constant pool");
}
}