com.sun.tools.classfile.ConstantPoolException Java Examples

The following examples show how to use com.sun.tools.classfile.ConstantPoolException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ClassReader.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitString(CONSTANT_String_info c, Integer p) {
    try {

        String value = slist.get(p);
        if (value == null) {
            value = c.getString();
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_String",
                      new String[]{"id", p.toString()},
                      value));
        }
        return value;
    } catch (ConstantPoolException ex) {
        throw new RuntimeException("Fatal error", ex);
    }
}
 
Example #2
Source File: ClassReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info c,
                                      Integer p) {
    String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.class_index), c.class_index);
            value = value.concat(" " +
                                 visit(cfpool.get(c.name_and_type_index),
                                 c.name_and_type_index));
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_InterfaceMethodref",
                      new String[]{"id", p.toString()},
                      value));

        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #3
Source File: ClassReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitFieldref(CONSTANT_Fieldref_info c, Integer p) {
String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.class_index), c.class_index);
            value = value.concat(" " + visit(cfpool.get(c.name_and_type_index),
                                 c.name_and_type_index));
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_Fieldref",
                      new String[]{"id", p.toString()},
                      value));
        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #4
Source File: ClassReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitMethodref(CONSTANT_Methodref_info c, Integer p) {
    String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.class_index), c.class_index);
            value = value.concat(" " +
                                 visit(cfpool.get(c.name_and_type_index),
                                 c.name_and_type_index));
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_Methodref",
                      new String[]{"id", p.toString()},
                      value));

        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #5
Source File: ClassReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitMethodHandle(CONSTANT_MethodHandle_info c, Integer p) {
String value = slist.get(p);
    if (value == null) {
        try {
            value = c.reference_kind.name();
            value = value.concat(" "
                    + visit(cfpool.get(c.reference_index), c.reference_index));
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_MethodHandle",
                      new String[]{"id", p.toString()},
                      value));

        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #6
Source File: ClassReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitMethodType(CONSTANT_MethodType_info c, Integer p) {
    String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.descriptor_index), c.descriptor_index);
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_MethodType",
                      new String[]{"id", p.toString()},
                      value));
        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #7
Source File: ClassReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitString(CONSTANT_String_info c, Integer p) {
    try {

        String value = slist.get(p);
        if (value == null) {
            value = c.getString();
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_String",
                      new String[]{"id", p.toString()},
                      value));
        }
        return value;
    } catch (ConstantPoolException ex) {
        throw new RuntimeException("Fatal error", ex);
    }
}
 
Example #8
Source File: ClassReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example #9
Source File: ClassReader.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitMethodref(CONSTANT_Methodref_info c, Integer p) {
    String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.class_index), c.class_index);
            value = value.concat(" " +
                                 visit(cfpool.get(c.name_and_type_index),
                                 c.name_and_type_index));
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_Methodref",
                      new String[]{"id", p.toString()},
                      value));

        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #10
Source File: ClassReader.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitMethodHandle(CONSTANT_MethodHandle_info c, Integer p) {
String value = slist.get(p);
    if (value == null) {
        try {
            value = c.reference_kind.name();
            value = value.concat(" "
                    + visit(cfpool.get(c.reference_index), c.reference_index));
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_MethodHandle",
                      new String[]{"id", p.toString()},
                      value));

        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #11
Source File: ClassReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #12
Source File: FindNativeFiles.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #13
Source File: ByteCodeTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private static boolean verifyClassFileAttributes(File classFile, TestCases tc) {
    ClassFile c = null;
    try {
        c = ClassFile.read(classFile);
    } catch (IOException | ConstantPoolException e) {
        e.printStackTrace();
    }
    ConstantPoolVisitor cpv = new ConstantPoolVisitor(c, c.constant_pool.size());
    Map<Integer, String> hm = cpv.getBSMMap();

    List<String> expectedValList = tc.getExpectedArgValues();
    expectedValList.add(tc.bsmSpecifier.specifier);
    if(!(hm.values().containsAll(new HashSet<String>(expectedValList)))) {
        System.out.println("Values do not match");
        return false;
    }
    return true;
}
 
Example #14
Source File: ByteCodeTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitFieldref(CONSTANT_Fieldref_info c, Integer p) {

String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.class_index), c.class_index);
            value = value.concat(" " + visit(cfpool.get(c.name_and_type_index),
                                 c.name_and_type_index));
            slist.set(p, value);
        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #15
Source File: ByteCodeTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitInterfaceMethodref(CONSTANT_InterfaceMethodref_info c,
                                      Integer p) {

    String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.class_index), c.class_index);
            value = value.concat(" " +
                                 visit(cfpool.get(c.name_and_type_index),
                                 c.name_and_type_index));
            slist.set(p, value);
        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #16
Source File: CompareTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #17
Source File: ByteCodeTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitMethodref(CONSTANT_Methodref_info c, Integer p) {

    String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.class_index), c.class_index);
            value = value.concat(" " +
                                 visit(cfpool.get(c.name_and_type_index),
                                 c.name_and_type_index));
            slist.set(p, value);
        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #18
Source File: ByteCodeTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitMethodHandle(CONSTANT_MethodHandle_info c, Integer p) {

String value = slist.get(p);
    if (value == null) {
        try {
            value = c.reference_kind.name();
            value = value.concat(" "
                    + visit(cfpool.get(c.reference_index), c.reference_index));
            slist.set(p, value);
        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #19
Source File: LVTHarness.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
            }
        }
    }
}
 
Example #20
Source File: LVTHarness.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
void checkMethod(ConstantPool constantPool, Method method, AliveRanges ranges)
        throws InvalidIndex, UnexpectedEntry, ConstantPoolException {
    Code_attribute code = (Code_attribute) method.attributes.get(Attribute.Code);
    LocalVariableTable_attribute lvt =
        (LocalVariableTable_attribute) (code.attributes.get(Attribute.LocalVariableTable));
    List<String> infoFromRanges = convertToStringList(ranges);
    List<String> infoFromLVT = convertToStringList(constantPool, lvt);

    // infoFromRanges most be contained in infoFromLVT
    int i = 0;
    int j = 0;
    while (i < infoFromRanges.size() && j < infoFromLVT.size()) {
        int comparison = infoFromRanges.get(i).compareTo(infoFromLVT.get(j));
        if (comparison == 0) {
            i++; j++;
        } else if (comparison > 0) {
            j++;
        } else {
            break;
        }
    }

    if (i < infoFromRanges.size()) {
        error(infoFromLVT, infoFromRanges, method.getName(constantPool).toString());
    }
}
 
Example #21
Source File: CPoolRefClassContainingInlinedCts.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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");
    }
}
 
Example #22
Source File: CheckACC_STRICTFlagOnclinitTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
void check(String dir, String... fileNames)
    throws
        IOException,
        ConstantPoolException,
        Descriptor.InvalidDescriptor {
    for (String fileName : fileNames) {
        ClassFile classFileToCheck = ClassFile.read(new File(dir, fileName));

        for (Method method : classFileToCheck.methods) {
            if ((method.access_flags.flags & ACC_STRICT) == 0) {
                errors.add(String.format(offendingMethodErrorMessage,
                        method.getName(classFileToCheck.constant_pool),
                        classFileToCheck.getName()));
            }
        }
    }
}
 
Example #23
Source File: DuplicateConstantPoolEntry.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
            }
        }
    }
}
 
Example #24
Source File: DetectMutableStaticFields.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
private void run()
    throws
        IOException,
        ConstantPoolException,
        InvalidDescriptor,
        URISyntaxException {

    URI resource = findResource(keyResource);
    if (resource == null) {
        throw new AssertionError("Resource " + keyResource +
            "not found in the class path");
    }
    analyzeResource(resource);

    if (errors.size() > 0) {
        for (String error: errors) {
            System.err.println(error);
        }
        throw new AssertionError("There are mutable fields, "
            + "please check output");
    }
}
 
Example #25
Source File: DetectMutableStaticFields.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
void analyzeResource(URI resource)
    throws
        IOException,
        ConstantPoolException,
        InvalidDescriptor {
    JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
    JavaFileManager.Location location =
            StandardLocation.locationFor(resource.getPath());
    fm.setLocation(location, com.sun.tools.javac.util.List.of(
            new File(resource.getPath())));

    for (JavaFileObject file : fm.list(location, "", EnumSet.of(CLASS), true)) {
        String className = fm.inferBinaryName(location, file);
        int index = className.lastIndexOf('.');
        String pckName = index == -1 ? "" : className.substring(0, index);
        if (shouldAnalyzePackage(pckName)) {
            analyzeClassFile(ClassFile.read(file.openInputStream()));
        }
    }
}
 
Example #26
Source File: CheckACC_STRICTFlagOnDefaultMethodTest.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
void check(String dir, String... fileNames)
    throws
        IOException,
        ConstantPoolException,
        Descriptor.InvalidDescriptor {
    for (String fileName : fileNames) {
        ClassFile classFileToCheck = ClassFile.read(new File(dir, fileName));

        for (Method method : classFileToCheck.methods) {
            if ((method.access_flags.flags & ACC_STRICT) == 0) {
                errors.add(String.format(offendingMethodErrorMessage,
                        method.getName(classFileToCheck.constant_pool),
                        classFileToCheck.getName()));
            }
        }
    }
}
 
Example #27
Source File: ClassReader.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #28
Source File: ClassReader.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
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();
}
 
Example #29
Source File: ClassReader.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitClass(CONSTANT_Class_info c, Integer p) {
    String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.name_index), c.name_index);
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_Class",
                    new String[]{"id", p.toString()},
                    value));
        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}
 
Example #30
Source File: ClassReader.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public String visitFieldref(CONSTANT_Fieldref_info c, Integer p) {
String value = slist.get(p);
    if (value == null) {
        try {
            value = visit(cfpool.get(c.class_index), c.class_index);
            value = value.concat(" " + visit(cfpool.get(c.name_and_type_index),
                                 c.name_and_type_index));
            slist.set(p, value);
            xpool.add(new Element("CONSTANT_Fieldref",
                      new String[]{"id", p.toString()},
                      value));
        } catch (ConstantPoolException ex) {
            ex.printStackTrace();
        }
    }
    return value;
}