mirror of
https://github.com/openjdk/jdk.git
synced 2026-02-19 06:45:17 +00:00
8016285: Add java.lang.reflect.Parameter.isNamePresent()
Add isNamePresent method to parameter reflection library, which indicates whether or real parameter data is available Reviewed-by: darcy
This commit is contained in:
parent
c70d472774
commit
a58f094d40
@ -326,8 +326,12 @@ public abstract class Executable extends AccessibleObject
|
||||
tmp = getParameters0();
|
||||
|
||||
// If we get back nothing, then synthesize parameters
|
||||
if (tmp == null)
|
||||
if (tmp == null) {
|
||||
hasRealParameterData = false;
|
||||
tmp = synthesizeAllParams();
|
||||
} else {
|
||||
hasRealParameterData = true;
|
||||
}
|
||||
|
||||
parameters = tmp;
|
||||
}
|
||||
@ -335,6 +339,16 @@ public abstract class Executable extends AccessibleObject
|
||||
return tmp;
|
||||
}
|
||||
|
||||
boolean hasRealParameterData() {
|
||||
// If this somehow gets called before parameters gets
|
||||
// initialized, force it into existence.
|
||||
if (parameters == null) {
|
||||
privateGetParameters();
|
||||
}
|
||||
return hasRealParameterData;
|
||||
}
|
||||
|
||||
private transient volatile boolean hasRealParameterData;
|
||||
private transient volatile Parameter[] parameters;
|
||||
|
||||
private native Parameter[] getParameters0();
|
||||
|
||||
@ -94,6 +94,19 @@ public final class Parameter implements AnnotatedElement {
|
||||
return executable.hashCode() ^ index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the parameter has a name according to the class
|
||||
* file; returns false otherwise. Whether a parameter has a name
|
||||
* is determined by the {@literal MethodParameters} attribute of
|
||||
* the method which declares the parameter.
|
||||
*
|
||||
* @return true if and only if the parameter has a name according
|
||||
* to the class file.
|
||||
*/
|
||||
public boolean isNamePresent() {
|
||||
return executable.hasRealParameterData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string describing this parameter. The format is the
|
||||
* modifiers for the parameter, if any, in canonical order as
|
||||
@ -149,11 +162,15 @@ public final class Parameter implements AnnotatedElement {
|
||||
|
||||
/**
|
||||
* Returns the name of the parameter. If the parameter's name is
|
||||
* defined in a class file, then that name will be returned by
|
||||
* this method. Otherwise, this method will synthesize a name of
|
||||
* the form argN, where N is the index of the parameter.
|
||||
* {@linkplain isNamePresent() present}, then this method returns
|
||||
* the name provided by the class file. Otherwise, this method
|
||||
* synthesizes a name of the form argN, where N is the index of
|
||||
* the parameter in the descriptor of the method which declares
|
||||
* the parameter.
|
||||
*
|
||||
* @return the name of the parameter
|
||||
* @return The name of the parameter, either provided by the class
|
||||
* file or synthesized if the class file does not provide
|
||||
* a name.
|
||||
*/
|
||||
public String getName() {
|
||||
// Note: empty strings as paramete names are now outlawed.
|
||||
|
||||
@ -73,6 +73,10 @@ public class WithParameters {
|
||||
}
|
||||
for(int i = 0; i < parameters.length; i++) {
|
||||
Parameter p = parameters[i];
|
||||
if(!p.isNamePresent()) {
|
||||
System.err.println(p + ".isNamePresent == false");
|
||||
error++;
|
||||
}
|
||||
if(!parameters[i].getName().equals(qux_names[i])) {
|
||||
System.err.println("Wrong parameter name for " + parameters[i]);
|
||||
error++;
|
||||
|
||||
@ -69,6 +69,7 @@ public class WithoutParameters {
|
||||
|
||||
for(int i = 0; i < parameters.length; i++) {
|
||||
Parameter p = parameters[i];
|
||||
errorIfTrue(p.isNamePresent(), p + ".isNamePresent == true");
|
||||
errorIfTrue(!p.getDeclaringExecutable().equals(e), p + ".getDeclaringExecutable != " + e);
|
||||
Objects.requireNonNull(p.getType(), "getType() should not be null");
|
||||
Objects.requireNonNull(p.getParameterizedType(), "getParameterizedType() should not be null");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user