8331744: java.lang.classfile.TypeKind improvements
Reviewed-by: asotona, redestad
This commit is contained in:
parent
dea8076a58
commit
784b8fce7a
src/java.base/share/classes
java/lang/classfile
jdk/internal/classfile/impl
test
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -58,7 +58,7 @@ public enum TypeKind {
|
||||
|
||||
private final String name;
|
||||
private final String descriptor;
|
||||
private final int newarraycode;
|
||||
private final int newarrayCode;
|
||||
|
||||
/** {@return the human-readable name corresponding to this type} */
|
||||
public String typeName() { return name; }
|
||||
@ -66,9 +66,12 @@ public enum TypeKind {
|
||||
/** {@return the field descriptor character corresponding to this type} */
|
||||
public String descriptor() { return descriptor; }
|
||||
|
||||
/** {@return the code used by the {@code newarray} opcode corresponding to this type} */
|
||||
public int newarraycode() {
|
||||
return newarraycode;
|
||||
/**
|
||||
* {@return the code used by the {@code newarray} opcode corresponding to this type}
|
||||
* @since 23
|
||||
*/
|
||||
public int newarrayCode() {
|
||||
return newarrayCode;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -94,19 +97,21 @@ public enum TypeKind {
|
||||
};
|
||||
}
|
||||
|
||||
TypeKind(String name, String descriptor, int newarraycode) {
|
||||
TypeKind(String name, String descriptor, int newarrayCode) {
|
||||
this.name = name;
|
||||
this.descriptor = descriptor;
|
||||
this.newarraycode = newarraycode;
|
||||
this.newarrayCode = newarrayCode;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the type kind associated with the array type described by the
|
||||
* array code used as an operand to {@code newarray}}
|
||||
* @param newarraycode the operand of the {@code newarray} instruction
|
||||
* @param newarrayCode the operand of the {@code newarray} instruction
|
||||
* @throws IllegalArgumentException if the code is invalid
|
||||
* @since 23
|
||||
*/
|
||||
public static TypeKind fromNewArrayCode(int newarraycode) {
|
||||
return switch (newarraycode) {
|
||||
public static TypeKind fromNewarrayCode(int newarrayCode) {
|
||||
return switch (newarrayCode) {
|
||||
case 4 -> TypeKind.BooleanType;
|
||||
case 5 -> TypeKind.CharType;
|
||||
case 6 -> TypeKind.FloatType;
|
||||
@ -115,15 +120,19 @@ public enum TypeKind {
|
||||
case 9 -> TypeKind.ShortType;
|
||||
case 10 -> TypeKind.IntType;
|
||||
case 11 -> TypeKind.LongType;
|
||||
default -> throw new IllegalArgumentException("Bad new array code: " + newarraycode);
|
||||
default -> throw new IllegalArgumentException("Bad newarray code: " + newarrayCode);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* {@return the type kind associated with the specified field descriptor}
|
||||
* @param s the field descriptor
|
||||
* @throws IllegalArgumentException only if the descriptor is not valid
|
||||
*/
|
||||
public static TypeKind fromDescriptor(CharSequence s) {
|
||||
if (s.isEmpty()) { // implicit null check
|
||||
throw new IllegalArgumentException("Empty descriptor");
|
||||
}
|
||||
return switch (s.charAt(0)) {
|
||||
case '[', 'L' -> TypeKind.ReferenceType;
|
||||
case 'B' -> TypeKind.ByteType;
|
||||
@ -144,6 +153,8 @@ public enum TypeKind {
|
||||
* @param descriptor the field descriptor
|
||||
*/
|
||||
public static TypeKind from(TypeDescriptor.OfField<?> descriptor) {
|
||||
return fromDescriptor(descriptor.descriptorString());
|
||||
return descriptor.isPrimitive() // implicit null check
|
||||
? fromDescriptor(descriptor.descriptorString())
|
||||
: TypeKind.ReferenceType;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2022, 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -56,7 +56,7 @@ public sealed interface NewPrimitiveArrayInstruction extends Instruction
|
||||
*/
|
||||
static NewPrimitiveArrayInstruction of(TypeKind typeKind) {
|
||||
// Implicit null-check:
|
||||
if (typeKind.newarraycode() < 0) {
|
||||
if (typeKind.newarrayCode() < 0) {
|
||||
throw new IllegalArgumentException("Illegal component type: " + typeKind.typeName());
|
||||
}
|
||||
return new AbstractInstruction.UnboundNewPrimitiveArrayInstruction(typeKind);
|
||||
|
@ -557,7 +557,7 @@ public abstract sealed class AbstractInstruction
|
||||
|
||||
@Override
|
||||
public TypeKind typeKind() {
|
||||
return TypeKind.fromNewArrayCode(code.classReader.readU1(pos + 1));
|
||||
return TypeKind.fromNewarrayCode(code.classReader.readU1(pos + 1));
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -1149,7 +1149,7 @@ public abstract sealed class AbstractInstruction
|
||||
|
||||
@Override
|
||||
public void writeTo(DirectCodeBuilder writer) {
|
||||
writer.writeNewPrimitiveArray(typeKind.newarraycode());
|
||||
writer.writeNewPrimitiveArray(typeKind.newarrayCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
48
test/jdk/jdk/classfile/TypeKindTest.java
Normal file
48
test/jdk/jdk/classfile/TypeKindTest.java
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Testing TypeKind.
|
||||
* @bug 8331744
|
||||
* @run junit TypeKindTest
|
||||
*/
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.classfile.TypeKind;
|
||||
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
class TypeKindTest {
|
||||
@Test
|
||||
void testContracts() {
|
||||
assertThrows(NullPointerException.class, () -> TypeKind.from(null));
|
||||
|
||||
assertThrows(NullPointerException.class, () -> TypeKind.fromDescriptor(null));
|
||||
assertThrows(IllegalArgumentException.class, () -> TypeKind.fromDescriptor(""));
|
||||
assertThrows(IllegalArgumentException.class, () -> TypeKind.fromDescriptor("int"));
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> TypeKind.fromNewarrayCode(-1));
|
||||
assertThrows(IllegalArgumentException.class, () -> TypeKind.fromNewarrayCode(21));
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
/*
|
||||
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package org.openjdk.bench.java.lang.classfile;
|
||||
|
||||
import org.openjdk.jmh.annotations.Benchmark;
|
||||
import org.openjdk.jmh.annotations.BenchmarkMode;
|
||||
import org.openjdk.jmh.annotations.Fork;
|
||||
import org.openjdk.jmh.annotations.Measurement;
|
||||
import org.openjdk.jmh.annotations.Mode;
|
||||
import org.openjdk.jmh.annotations.OutputTimeUnit;
|
||||
import org.openjdk.jmh.annotations.Param;
|
||||
import org.openjdk.jmh.annotations.Scope;
|
||||
import org.openjdk.jmh.annotations.Setup;
|
||||
import org.openjdk.jmh.annotations.State;
|
||||
import org.openjdk.jmh.annotations.Warmup;
|
||||
import org.openjdk.jmh.infra.Blackhole;
|
||||
|
||||
import java.lang.classfile.TypeKind;
|
||||
import java.lang.constant.ClassDesc;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Performance of conversion from type descriptor objects to type kind.
|
||||
*/
|
||||
@BenchmarkMode(Mode.AverageTime)
|
||||
@OutputTimeUnit(TimeUnit.NANOSECONDS)
|
||||
@Warmup(iterations = 3, time = 2)
|
||||
@Measurement(iterations = 6, time = 1)
|
||||
@Fork(jvmArgsAppend = "--enable-preview", value = 1)
|
||||
@State(Scope.Thread)
|
||||
public class TypeKindBench {
|
||||
|
||||
public enum ClassType {
|
||||
PRIMITIVE, REFERENCE, MIXED;
|
||||
}
|
||||
|
||||
@Param
|
||||
ClassType type;
|
||||
Class<?>[] classes;
|
||||
ClassDesc[] classDescs;
|
||||
|
||||
@Setup
|
||||
public void setup() {
|
||||
var references = List.of(Character.class, String.class, Integer.class,
|
||||
Long.class, Object.class, int[].class, TypeKindBench.class,
|
||||
Byte[].class, boolean[][].class);
|
||||
var primitives = List.of(int.class, long.class, void.class, double.class,
|
||||
float.class, boolean.class, char.class, short.class, byte.class);
|
||||
final List<Class<?>> candidates = switch (type) {
|
||||
case REFERENCE -> references;
|
||||
case PRIMITIVE -> primitives;
|
||||
case MIXED -> {
|
||||
var list = new ArrayList<Class<?>>(references.size() + primitives.size());
|
||||
list.addAll(references);
|
||||
list.addAll(primitives);
|
||||
yield list;
|
||||
}
|
||||
};
|
||||
|
||||
// Use fixed seed to ensure results are comparable across
|
||||
// different JVMs
|
||||
classes = new Random(0xbf5fe40dd887d9e2L)
|
||||
.ints(100, 0, candidates.size())
|
||||
.mapToObj(candidates::get)
|
||||
.toArray(Class<?>[]::new);
|
||||
classDescs = Arrays.stream(classes)
|
||||
.map(cl -> cl.describeConstable().orElseThrow())
|
||||
.toArray(ClassDesc[]::new);
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fromClasses(Blackhole bh) {
|
||||
for (var clz : classes) {
|
||||
bh.consume(TypeKind.from(clz));
|
||||
}
|
||||
}
|
||||
|
||||
@Benchmark
|
||||
public void fromClassDescs(Blackhole bh) {
|
||||
for (var clz : classDescs) {
|
||||
bh.consume(TypeKind.from(clz));
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user