Skip to content

8356022: Migrate descriptor parsing from generics to BytecodeDescriptor #24978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 10 additions & 26 deletions src/java.base/share/classes/java/lang/Class.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@
import java.lang.reflect.Member;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.lang.reflect.RecordComponent;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
Expand Down Expand Up @@ -82,12 +81,11 @@
import jdk.internal.vm.annotation.IntrinsicCandidate;
import jdk.internal.vm.annotation.Stable;

import sun.invoke.util.BytecodeDescriptor;
import sun.invoke.util.Wrapper;
import sun.reflect.generics.factory.CoreReflectionFactory;
import sun.reflect.generics.factory.GenericsFactory;
import sun.reflect.generics.repository.ClassRepository;
import sun.reflect.generics.repository.MethodRepository;
import sun.reflect.generics.repository.ConstructorRepository;
import sun.reflect.generics.scope.ClassScope;
import sun.reflect.annotation.*;

Expand Down Expand Up @@ -1437,17 +1435,10 @@ public Method getEnclosingMethod() {
if (!enclosingInfo.isMethod())
return null;

MethodRepository typeInfo = MethodRepository.make(enclosingInfo.getDescriptor(),
getFactory());
Class<?> returnType = toClass(typeInfo.getReturnType());
Type [] parameterTypes = typeInfo.getParameterTypes();
Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];

// Convert Types to Classes; returned types *should*
// be class objects since the methodDescriptor's used
// don't have generics information
for(int i = 0; i < parameterClasses.length; i++)
parameterClasses[i] = toClass(parameterTypes[i]);
// Descriptor already validated by VM
List<Class<?>> types = BytecodeDescriptor.parseMethod(enclosingInfo.getDescriptor(), getClassLoader());
Class<?> returnType = types.removeLast();
Class<?>[] parameterClasses = types.toArray(EMPTY_CLASS_ARRAY);

final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
Method[] candidates = enclosingCandidate.privateGetDeclaredMethods(false);
Expand Down Expand Up @@ -1566,17 +1557,10 @@ public Constructor<?> getEnclosingConstructor() {
if (!enclosingInfo.isConstructor())
return null;

ConstructorRepository typeInfo = ConstructorRepository.make(enclosingInfo.getDescriptor(),
getFactory());
Type [] parameterTypes = typeInfo.getParameterTypes();
Class<?>[] parameterClasses = new Class<?>[parameterTypes.length];

// Convert Types to Classes; returned types *should*
// be class objects since the methodDescriptor's used
// don't have generics information
for (int i = 0; i < parameterClasses.length; i++)
parameterClasses[i] = toClass(parameterTypes[i]);

// Descriptor already validated by VM
List<Class<?>> types = BytecodeDescriptor.parseMethod(enclosingInfo.getDescriptor(), getClassLoader());
types.removeLast();
Class<?>[] parameterClasses = types.toArray(EMPTY_CLASS_ARRAY);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Class<?>[] parameterClasses = types.toArray(EMPTY_CLASS_ARRAY);
var parameterClasses = types.toArray(EMPTY_CLASS_ARRAY);

Here the type of parameterClasses is very clear, or you can use var


final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
final Class<?> enclosingCandidate = enclosingInfo.getEnclosingClass();
Constructor<?>[] candidates = enclosingInfo.getEnclosingClass()
.privateGetDeclaredConstructors(false);

Here the local variable enclosingInfo is only used once, the above code may be simpler

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch intentionally did not change this part - and this part can get much more in-depth optimization; for example, arrayContentsEq can be shared for all Class arrays, copy of reflective objects can be simplified, etc.

Constructor<?>[] candidates = enclosingCandidate
Expand Down Expand Up @@ -1882,7 +1866,7 @@ public Class<?>[] getClasses() {
}
currentClass = currentClass.getSuperclass();
}
return list.toArray(new Class<?>[0]);
return list.toArray(EMPTY_CLASS_ARRAY);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, 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
Expand Down Expand Up @@ -37,9 +37,26 @@ public class BytecodeDescriptor {

private BytecodeDescriptor() { } // cannot instantiate

/**
* @throws IllegalArgumentException if the descriptor is invalid
* @throws TypeNotPresentException if the descriptor is valid, but
* the class cannot be found by the loader
*/
public static Class<?> parseClass(String descriptor, ClassLoader loader) {
int[] i = {0};
var ret = parseSig(descriptor, i, descriptor.length(), loader);
if (i[0] != descriptor.length() || ret == null) {
parseError("not a class descriptor", descriptor);
}
return ret;
}

/**
* @param loader the class loader in which to look up the types (null means
* bootstrap class loader)
* @throws IllegalArgumentException if the descriptor is invalid
* @throws TypeNotPresentException if the descriptor is valid, but
* a reference type cannot be found by the loader
*/
public static List<Class<?>> parseMethod(String bytecodeSignature, ClassLoader loader) {
return parseMethod(bytecodeSignature, 0, bytecodeSignature.length(), loader);
Expand Down Expand Up @@ -78,6 +95,13 @@ private static void parseError(String str, String msg) {
}

/**
* Parse a single type in a descriptor. Results can be:
* <ul>
* <li>A {@code Class} for successful parsing
* <li>{@code null} for malformed descriptor format
* <li>Throwing a {@link TypeNotPresentException} for valid class name,
* but class cannot be discovered
* </ul>
* @param loader the class loader in which to look up the types (null means
* bootstrap class loader)
*/
Expand All @@ -100,7 +124,8 @@ private static Class<?> parseSig(String str, int[] i, int end, ClassLoader loade
t = t.arrayType();
return t;
} else {
return Wrapper.forBasicType(c).primitiveType();
var w = Wrapper.forPrimitiveTypeOrNull(c);
return w == null ? null : w.primitiveType();
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/java.base/share/classes/sun/invoke/util/Wrapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2008, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2008, 2025, 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
Expand Down Expand Up @@ -352,6 +352,19 @@ public static Wrapper forBasicType(char type) {
throw basicTypeError(type);
}

/**
* Return the primitive wrapper for the given char. Does not return
* {@code OBJECT}. Returns {@code null} to allow flexible error messages.
* Dedicated for {@link BytecodeDescriptor}.
*/
static Wrapper forPrimitiveTypeOrNull(char type) {
Wrapper w = FROM_CHAR[(type + (type >> 1)) & 0xf];
if (w != null && w != OBJECT && w.basicTypeChar == type) {
return w;
}
return null;
}

@DontInline
private static RuntimeException basicTypeError(char type) {
for (Wrapper x : values()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2024, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2025, 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
Expand Down Expand Up @@ -34,12 +34,7 @@

import jdk.internal.reflect.ConstantPool;

import sun.reflect.generics.parser.SignatureParser;
import sun.reflect.generics.tree.TypeSignature;
import sun.reflect.generics.factory.GenericsFactory;
import sun.reflect.generics.factory.CoreReflectionFactory;
import sun.reflect.generics.visitor.Reifier;
import sun.reflect.generics.scope.ClassScope;
import sun.invoke.util.BytecodeDescriptor;

/**
* Parser for Java programming language annotations. Translates
Expand Down Expand Up @@ -429,19 +424,11 @@ private static Object parseClassValue(ByteBuffer buf,
}

private static Class<?> parseSig(String sig, Class<?> container) {
if (sig.equals("V")) return void.class;
SignatureParser parser = SignatureParser.make();
TypeSignature typeSig = parser.parseTypeSig(sig);
GenericsFactory factory = CoreReflectionFactory.make(container, ClassScope.make(container));
Reifier reify = Reifier.make(factory);
typeSig.accept(reify);
Type result = reify.getResult();
return toClass(result);
}
static Class<?> toClass(Type o) {
if (o instanceof GenericArrayType gat)
return toClass(gat.getGenericComponentType()).arrayType();
return (Class<?>) o;
try {
return BytecodeDescriptor.parseClass(sig, container.getClassLoader());
} catch (IllegalArgumentException ex) {
throw new GenericSignatureFormatError(ex.getMessage());
}
}

/**
Expand Down