Skip to content

Commit 1203e11

Browse files
committed
8294969: Convert jdk.jdeps javap to use the Classfile API
Reviewed-by: vromero
1 parent fbc766e commit 1203e11

29 files changed

+1977
-2835
lines changed

src/java.base/share/classes/jdk/internal/classfile/ClassReader.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ public sealed interface ClassReader extends ConstantPool
102102
*/
103103
PoolEntry readEntry(int offset);
104104

105+
/**
106+
* {@return the constant pool entry of a given type whose index is given
107+
* at the specified offset within the classfile}
108+
* @param offset the offset of the index within the classfile
109+
* @param cls the entry type
110+
* @throws ConstantPoolException if the index is out of range of the
111+
* constant pool size, or zero, or the entry is not of the given type
112+
*/
113+
<T extends PoolEntry> T readEntry(int offset, Class<T> cls);
114+
105115
/**
106116
* {@return the constant pool entry whose index is given at the specified
107117
* offset within the classfile, or null if the index at the specified

src/java.base/share/classes/jdk/internal/classfile/constantpool/DynamicConstantPoolEntry.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ public sealed interface DynamicConstantPoolEntry extends PoolEntry
3838
*/
3939
BootstrapMethodEntry bootstrap();
4040

41+
/**
42+
* {@return index of the entry in the bootstrap method table for this constant}
43+
*/
44+
int bootstrapMethodIndex();
45+
4146
/**
4247
* {@return the invocation name and type}
4348
*/

src/java.base/share/classes/jdk/internal/classfile/impl/AbstractInstruction.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ public BoundFieldInstruction(Opcode op, CodeImpl code, int pos) {
383383
@Override
384384
public FieldRefEntry field() {
385385
if (fieldEntry == null)
386-
fieldEntry = (FieldRefEntry) code.classReader.readEntry(pos + 1);
386+
fieldEntry = code.classReader.readEntry(pos + 1, FieldRefEntry.class);
387387
return fieldEntry;
388388
}
389389

@@ -413,7 +413,7 @@ public BoundInvokeInstruction(Opcode op, CodeImpl code, int pos) {
413413
@Override
414414
public MemberRefEntry method() {
415415
if (methodEntry == null)
416-
methodEntry = (MemberRefEntry) code.classReader.readEntry(pos + 1);
416+
methodEntry = code.classReader.readEntry(pos + 1, MemberRefEntry.class);
417417
return methodEntry;
418418
}
419419

@@ -453,7 +453,7 @@ public BoundInvokeInterfaceInstruction(Opcode op, CodeImpl code, int pos) {
453453
@Override
454454
public MemberRefEntry method() {
455455
if (methodEntry == null)
456-
methodEntry = (InterfaceMethodRefEntry) code.classReader.readEntry(pos + 1);
456+
methodEntry = code.classReader.readEntry(pos + 1, InterfaceMethodRefEntry.class);
457457
return methodEntry;
458458
}
459459

@@ -493,7 +493,7 @@ public static final class BoundInvokeDynamicInstruction
493493
@Override
494494
public InvokeDynamicEntry invokedynamic() {
495495
if (indyEntry == null)
496-
indyEntry = (InvokeDynamicEntry) code.classReader.readEntry(pos + 1);
496+
indyEntry = code.classReader.readEntry(pos + 1, InvokeDynamicEntry.class);
497497
return indyEntry;
498498
}
499499

src/java.base/share/classes/jdk/internal/classfile/impl/AbstractPoolEntry.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,13 @@ public BootstrapMethodEntryImpl bootstrap() {
815815
return bootstrapMethod;
816816
}
817817

818+
/**
819+
* @return the bsmIndex
820+
*/
821+
public int bootstrapMethodIndex() {
822+
return bsmIndex;
823+
}
824+
818825
/**
819826
* @return the nameAndType
820827
*/

src/java.base/share/classes/jdk/internal/classfile/impl/AnnotationReader.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,14 @@ public static AnnotationValue readElementValue(ClassReader classReader, int p) {
5858
char tag = (char) classReader.readU1(p);
5959
++p;
6060
return switch (tag) {
61-
case AEV_BYTE -> new AnnotationImpl.OfByteImpl((IntegerEntry)classReader.readEntry(p));
62-
case AEV_CHAR -> new AnnotationImpl.OfCharacterImpl((IntegerEntry)classReader.readEntry(p));
63-
case AEV_DOUBLE -> new AnnotationImpl.OfDoubleImpl((DoubleEntry)classReader.readEntry(p));
64-
case AEV_FLOAT -> new AnnotationImpl.OfFloatImpl((FloatEntry)classReader.readEntry(p));
65-
case AEV_INT -> new AnnotationImpl.OfIntegerImpl((IntegerEntry)classReader.readEntry(p));
66-
case AEV_LONG -> new AnnotationImpl.OfLongImpl((LongEntry)classReader.readEntry(p));
67-
case AEV_SHORT -> new AnnotationImpl.OfShortImpl((IntegerEntry)classReader.readEntry(p));
68-
case AEV_BOOLEAN -> new AnnotationImpl.OfBooleanImpl((IntegerEntry)classReader.readEntry(p));
61+
case AEV_BYTE -> new AnnotationImpl.OfByteImpl(classReader.readEntry(p, IntegerEntry.class));
62+
case AEV_CHAR -> new AnnotationImpl.OfCharacterImpl(classReader.readEntry(p, IntegerEntry.class));
63+
case AEV_DOUBLE -> new AnnotationImpl.OfDoubleImpl(classReader.readEntry(p, DoubleEntry.class));
64+
case AEV_FLOAT -> new AnnotationImpl.OfFloatImpl(classReader.readEntry(p, FloatEntry.class));
65+
case AEV_INT -> new AnnotationImpl.OfIntegerImpl(classReader.readEntry(p, IntegerEntry.class));
66+
case AEV_LONG -> new AnnotationImpl.OfLongImpl(classReader.readEntry(p, LongEntry.class));
67+
case AEV_SHORT -> new AnnotationImpl.OfShortImpl(classReader.readEntry(p, IntegerEntry.class));
68+
case AEV_BOOLEAN -> new AnnotationImpl.OfBooleanImpl(classReader.readEntry(p, IntegerEntry.class));
6969
case AEV_STRING -> new AnnotationImpl.OfStringImpl(classReader.readUtf8Entry(p));
7070
case AEV_ENUM -> new AnnotationImpl.OfEnumImpl(classReader.readUtf8Entry(p), classReader.readUtf8Entry(p + 2));
7171
case AEV_CLASS -> new AnnotationImpl.OfClassImpl(classReader.readUtf8Entry(p));

src/java.base/share/classes/jdk/internal/classfile/impl/BoundAttribute.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ public BoundConstantValueAttribute(ClassReader cf, AttributeMapper<ConstantValue
483483

484484
@Override
485485
public ConstantValueEntry constant() {
486-
return (ConstantValueEntry) classReader.readEntry(payloadStart);
486+
return classReader.readEntry(payloadStart, ConstantValueEntry.class);
487487
}
488488

489489
}

src/java.base/share/classes/jdk/internal/classfile/impl/ClassReaderImpl.java

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.util.ArrayList;
2929
import java.util.Arrays;
30-
import java.util.Collection;
3130
import java.util.List;
3231
import java.util.Optional;
3332
import java.util.function.Function;
@@ -37,10 +36,6 @@
3736
import jdk.internal.classfile.constantpool.ClassEntry;
3837
import jdk.internal.classfile.constantpool.ConstantPoolException;
3938
import jdk.internal.classfile.constantpool.LoadableConstantEntry;
40-
import jdk.internal.classfile.constantpool.MethodHandleEntry;
41-
import jdk.internal.classfile.constantpool.ModuleEntry;
42-
import jdk.internal.classfile.constantpool.NameAndTypeEntry;
43-
import jdk.internal.classfile.constantpool.PackageEntry;
4439
import jdk.internal.classfile.constantpool.PoolEntry;
4540
import jdk.internal.classfile.constantpool.Utf8Entry;
4641

@@ -61,6 +56,10 @@
6156
import static jdk.internal.classfile.Classfile.TAG_PACKAGE;
6257
import static jdk.internal.classfile.Classfile.TAG_STRING;
6358
import static jdk.internal.classfile.Classfile.TAG_UTF8;
59+
import jdk.internal.classfile.constantpool.MethodHandleEntry;
60+
import jdk.internal.classfile.constantpool.ModuleEntry;
61+
import jdk.internal.classfile.constantpool.NameAndTypeEntry;
62+
import jdk.internal.classfile.constantpool.PackageEntry;
6463

6564
public final class ClassReaderImpl
6665
implements ClassReader {
@@ -156,7 +155,7 @@ public int flags() {
156155
@Override
157156
public ClassEntry thisClassEntry() {
158157
if (thisClass == null) {
159-
thisClass = readClassEntry(thisClassPos);
158+
thisClass = readEntry(thisClassPos, ClassEntry.class);
160159
}
161160
return thisClass;
162161
}
@@ -391,6 +390,13 @@ public PoolEntry readEntry(int pos) {
391390
return entryByIndex(readU2(pos));
392391
}
393392

393+
@Override
394+
public <T extends PoolEntry> T readEntry(int pos, Class<T> cls) {
395+
var e = readEntry(pos);
396+
if (cls.isInstance(e)) return cls.cast(e);
397+
throw new ConstantPoolException("Not a " + cls.getSimpleName() + " at index: " + readU2(pos));
398+
}
399+
394400
@Override
395401
public PoolEntry readEntryOrNull(int pos) {
396402
int index = readU2(pos);
@@ -417,32 +423,27 @@ public Utf8Entry readUtf8EntryOrNull(int pos) {
417423

418424
@Override
419425
public ModuleEntry readModuleEntry(int pos) {
420-
if (readEntry(pos) instanceof ModuleEntry me) return me;
421-
throw new ConstantPoolException("Not a module entry at pos: " + pos);
426+
return readEntry(pos, ModuleEntry.class);
422427
}
423428

424429
@Override
425430
public PackageEntry readPackageEntry(int pos) {
426-
if (readEntry(pos) instanceof PackageEntry pe) return pe;
427-
throw new ConstantPoolException("Not a package entry at pos: " + pos);
431+
return readEntry(pos, PackageEntry.class);
428432
}
429433

430434
@Override
431435
public ClassEntry readClassEntry(int pos) {
432-
if (readEntry(pos) instanceof ClassEntry ce) return ce;
433-
throw new ConstantPoolException("Not a class entry at pos: " + pos);
436+
return readEntry(pos, ClassEntry.class);
434437
}
435438

436439
@Override
437440
public NameAndTypeEntry readNameAndTypeEntry(int pos) {
438-
if (readEntry(pos) instanceof NameAndTypeEntry nate) return nate;
439-
throw new ConstantPoolException("Not a name and type entry at pos: " + pos);
441+
return readEntry(pos, NameAndTypeEntry.class);
440442
}
441443

442444
@Override
443445
public MethodHandleEntry readMethodHandleEntry(int pos) {
444-
if (readEntry(pos) instanceof MethodHandleEntry mhe) return mhe;
445-
throw new ConstantPoolException("Not a method handle entry at pos: " + pos);
446+
return readEntry(pos, MethodHandleEntry.class);
446447
}
447448

448449
@Override

src/java.base/share/classes/module-info.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,19 @@
186186
java.logging;
187187
exports jdk.internal.classfile to
188188
jdk.jartool,
189+
jdk.jdeps,
189190
jdk.jlink,
190191
jdk.jshell;
191192
exports jdk.internal.classfile.attribute to
192193
jdk.jartool,
194+
jdk.jdeps,
193195
jdk.jlink;
194196
exports jdk.internal.classfile.constantpool to
195197
jdk.jartool,
198+
jdk.jdeps,
196199
jdk.jlink;
197200
exports jdk.internal.classfile.instruction to
201+
jdk.jdeps,
198202
jdk.jlink,
199203
jdk.jshell;
200204
exports jdk.internal.org.objectweb.asm to

0 commit comments

Comments
 (0)