8016285: Add java.lang.reflect.Parameter.isNamePresent()
Summary: Add isNamePresent method to parameter reflection library, which indicates whether or real parameter data is available
Reviewed-by: darcy
--- a/jdk/src/share/classes/java/lang/reflect/Executable.java Wed Jul 03 13:30:46 2013 -0700
+++ b/jdk/src/share/classes/java/lang/reflect/Executable.java Wed Jul 03 19:47:15 2013 -0400
@@ -326,8 +326,12 @@
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 @@
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();
--- a/jdk/src/share/classes/java/lang/reflect/Parameter.java Wed Jul 03 13:30:46 2013 -0700
+++ b/jdk/src/share/classes/java/lang/reflect/Parameter.java Wed Jul 03 19:47:15 2013 -0400
@@ -95,6 +95,19 @@
}
/**
+ * 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
* recommended by <cite>The Java™ Language
@@ -149,11 +162,15 @@
/**
* 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.
--- a/jdk/test/java/lang/reflect/Parameter/WithParameters.java Wed Jul 03 13:30:46 2013 -0700
+++ b/jdk/test/java/lang/reflect/Parameter/WithParameters.java Wed Jul 03 19:47:15 2013 -0400
@@ -73,6 +73,10 @@
}
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++;
--- a/jdk/test/java/lang/reflect/Parameter/WithoutParameters.java Wed Jul 03 13:30:46 2013 -0700
+++ b/jdk/test/java/lang/reflect/Parameter/WithoutParameters.java Wed Jul 03 19:47:15 2013 -0400
@@ -69,6 +69,7 @@
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");