8294969: Convert jdk.jdeps javap to use the Classfile API

Reviewed-by: vromero
This commit is contained in:
Adam Sotona 2023-09-18 08:35:31 +00:00
parent fbc766ee21
commit 1203e11a8d
29 changed files with 2012 additions and 2870 deletions

View file

@ -102,6 +102,16 @@ public sealed interface ClassReader extends ConstantPool
*/
PoolEntry readEntry(int offset);
/**
* {@return the constant pool entry of a given type whose index is given
* at the specified offset within the classfile}
* @param offset the offset of the index within the classfile
* @param cls the entry type
* @throws ConstantPoolException if the index is out of range of the
* constant pool size, or zero, or the entry is not of the given type
*/
<T extends PoolEntry> T readEntry(int offset, Class<T> cls);
/**
* {@return the constant pool entry whose index is given at the specified
* offset within the classfile, or null if the index at the specified

View file

@ -38,6 +38,11 @@ public sealed interface DynamicConstantPoolEntry extends PoolEntry
*/
BootstrapMethodEntry bootstrap();
/**
* {@return index of the entry in the bootstrap method table for this constant}
*/
int bootstrapMethodIndex();
/**
* {@return the invocation name and type}
*/

View file

@ -383,7 +383,7 @@ public abstract sealed class AbstractInstruction
@Override
public FieldRefEntry field() {
if (fieldEntry == null)
fieldEntry = (FieldRefEntry) code.classReader.readEntry(pos + 1);
fieldEntry = code.classReader.readEntry(pos + 1, FieldRefEntry.class);
return fieldEntry;
}
@ -413,7 +413,7 @@ public abstract sealed class AbstractInstruction
@Override
public MemberRefEntry method() {
if (methodEntry == null)
methodEntry = (MemberRefEntry) code.classReader.readEntry(pos + 1);
methodEntry = code.classReader.readEntry(pos + 1, MemberRefEntry.class);
return methodEntry;
}
@ -453,7 +453,7 @@ public abstract sealed class AbstractInstruction
@Override
public MemberRefEntry method() {
if (methodEntry == null)
methodEntry = (InterfaceMethodRefEntry) code.classReader.readEntry(pos + 1);
methodEntry = code.classReader.readEntry(pos + 1, InterfaceMethodRefEntry.class);
return methodEntry;
}
@ -493,7 +493,7 @@ public abstract sealed class AbstractInstruction
@Override
public InvokeDynamicEntry invokedynamic() {
if (indyEntry == null)
indyEntry = (InvokeDynamicEntry) code.classReader.readEntry(pos + 1);
indyEntry = code.classReader.readEntry(pos + 1, InvokeDynamicEntry.class);
return indyEntry;
}

View file

@ -815,6 +815,13 @@ public abstract sealed class AbstractPoolEntry {
return bootstrapMethod;
}
/**
* @return the bsmIndex
*/
public int bootstrapMethodIndex() {
return bsmIndex;
}
/**
* @return the nameAndType
*/

View file

@ -58,14 +58,14 @@ class AnnotationReader {
char tag = (char) classReader.readU1(p);
++p;
return switch (tag) {
case AEV_BYTE -> new AnnotationImpl.OfByteImpl((IntegerEntry)classReader.readEntry(p));
case AEV_CHAR -> new AnnotationImpl.OfCharacterImpl((IntegerEntry)classReader.readEntry(p));
case AEV_DOUBLE -> new AnnotationImpl.OfDoubleImpl((DoubleEntry)classReader.readEntry(p));
case AEV_FLOAT -> new AnnotationImpl.OfFloatImpl((FloatEntry)classReader.readEntry(p));
case AEV_INT -> new AnnotationImpl.OfIntegerImpl((IntegerEntry)classReader.readEntry(p));
case AEV_LONG -> new AnnotationImpl.OfLongImpl((LongEntry)classReader.readEntry(p));
case AEV_SHORT -> new AnnotationImpl.OfShortImpl((IntegerEntry)classReader.readEntry(p));
case AEV_BOOLEAN -> new AnnotationImpl.OfBooleanImpl((IntegerEntry)classReader.readEntry(p));
case AEV_BYTE -> new AnnotationImpl.OfByteImpl(classReader.readEntry(p, IntegerEntry.class));
case AEV_CHAR -> new AnnotationImpl.OfCharacterImpl(classReader.readEntry(p, IntegerEntry.class));
case AEV_DOUBLE -> new AnnotationImpl.OfDoubleImpl(classReader.readEntry(p, DoubleEntry.class));
case AEV_FLOAT -> new AnnotationImpl.OfFloatImpl(classReader.readEntry(p, FloatEntry.class));
case AEV_INT -> new AnnotationImpl.OfIntegerImpl(classReader.readEntry(p, IntegerEntry.class));
case AEV_LONG -> new AnnotationImpl.OfLongImpl(classReader.readEntry(p, LongEntry.class));
case AEV_SHORT -> new AnnotationImpl.OfShortImpl(classReader.readEntry(p, IntegerEntry.class));
case AEV_BOOLEAN -> new AnnotationImpl.OfBooleanImpl(classReader.readEntry(p, IntegerEntry.class));
case AEV_STRING -> new AnnotationImpl.OfStringImpl(classReader.readUtf8Entry(p));
case AEV_ENUM -> new AnnotationImpl.OfEnumImpl(classReader.readUtf8Entry(p), classReader.readUtf8Entry(p + 2));
case AEV_CLASS -> new AnnotationImpl.OfClassImpl(classReader.readUtf8Entry(p));

View file

@ -483,7 +483,7 @@ public abstract sealed class BoundAttribute<T extends Attribute<T>>
@Override
public ConstantValueEntry constant() {
return (ConstantValueEntry) classReader.readEntry(payloadStart);
return classReader.readEntry(payloadStart, ConstantValueEntry.class);
}
}

View file

@ -27,7 +27,6 @@ package jdk.internal.classfile.impl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
@ -37,10 +36,6 @@ import jdk.internal.classfile.attribute.BootstrapMethodsAttribute;
import jdk.internal.classfile.constantpool.ClassEntry;
import jdk.internal.classfile.constantpool.ConstantPoolException;
import jdk.internal.classfile.constantpool.LoadableConstantEntry;
import jdk.internal.classfile.constantpool.MethodHandleEntry;
import jdk.internal.classfile.constantpool.ModuleEntry;
import jdk.internal.classfile.constantpool.NameAndTypeEntry;
import jdk.internal.classfile.constantpool.PackageEntry;
import jdk.internal.classfile.constantpool.PoolEntry;
import jdk.internal.classfile.constantpool.Utf8Entry;
@ -61,6 +56,10 @@ import static jdk.internal.classfile.Classfile.TAG_NAMEANDTYPE;
import static jdk.internal.classfile.Classfile.TAG_PACKAGE;
import static jdk.internal.classfile.Classfile.TAG_STRING;
import static jdk.internal.classfile.Classfile.TAG_UTF8;
import jdk.internal.classfile.constantpool.MethodHandleEntry;
import jdk.internal.classfile.constantpool.ModuleEntry;
import jdk.internal.classfile.constantpool.NameAndTypeEntry;
import jdk.internal.classfile.constantpool.PackageEntry;
public final class ClassReaderImpl
implements ClassReader {
@ -156,7 +155,7 @@ public final class ClassReaderImpl
@Override
public ClassEntry thisClassEntry() {
if (thisClass == null) {
thisClass = readClassEntry(thisClassPos);
thisClass = readEntry(thisClassPos, ClassEntry.class);
}
return thisClass;
}
@ -391,6 +390,13 @@ public final class ClassReaderImpl
return entryByIndex(readU2(pos));
}
@Override
public <T extends PoolEntry> T readEntry(int pos, Class<T> cls) {
var e = readEntry(pos);
if (cls.isInstance(e)) return cls.cast(e);
throw new ConstantPoolException("Not a " + cls.getSimpleName() + " at index: " + readU2(pos));
}
@Override
public PoolEntry readEntryOrNull(int pos) {
int index = readU2(pos);
@ -417,32 +423,27 @@ public final class ClassReaderImpl
@Override
public ModuleEntry readModuleEntry(int pos) {
if (readEntry(pos) instanceof ModuleEntry me) return me;
throw new ConstantPoolException("Not a module entry at pos: " + pos);
return readEntry(pos, ModuleEntry.class);
}
@Override
public PackageEntry readPackageEntry(int pos) {
if (readEntry(pos) instanceof PackageEntry pe) return pe;
throw new ConstantPoolException("Not a package entry at pos: " + pos);
return readEntry(pos, PackageEntry.class);
}
@Override
public ClassEntry readClassEntry(int pos) {
if (readEntry(pos) instanceof ClassEntry ce) return ce;
throw new ConstantPoolException("Not a class entry at pos: " + pos);
return readEntry(pos, ClassEntry.class);
}
@Override
public NameAndTypeEntry readNameAndTypeEntry(int pos) {
if (readEntry(pos) instanceof NameAndTypeEntry nate) return nate;
throw new ConstantPoolException("Not a name and type entry at pos: " + pos);
return readEntry(pos, NameAndTypeEntry.class);
}
@Override
public MethodHandleEntry readMethodHandleEntry(int pos) {
if (readEntry(pos) instanceof MethodHandleEntry mhe) return mhe;
throw new ConstantPoolException("Not a method handle entry at pos: " + pos);
return readEntry(pos, MethodHandleEntry.class);
}
@Override

View file

@ -186,15 +186,19 @@ module java.base {
java.logging;
exports jdk.internal.classfile to
jdk.jartool,
jdk.jdeps,
jdk.jlink,
jdk.jshell;
exports jdk.internal.classfile.attribute to
jdk.jartool,
jdk.jdeps,
jdk.jlink;
exports jdk.internal.classfile.constantpool to
jdk.jartool,
jdk.jdeps,
jdk.jlink;
exports jdk.internal.classfile.instruction to
jdk.jdeps,
jdk.jlink,
jdk.jshell;
exports jdk.internal.org.objectweb.asm to