提交 e4b8584e 编写于 作者: D darcy

8004979: java.lang.reflect.Modifier.toString should include "default"

Reviewed-by: mduigou
上级 5eaeff5d
...@@ -284,9 +284,13 @@ public final class Constructor<T> extends Executable { ...@@ -284,9 +284,13 @@ public final class Constructor<T> extends Executable {
* modifiers {@code public}, {@code protected} or * modifiers {@code public}, {@code protected} or
* {@code private}. Only one of these may appear, or none if the * {@code private}. Only one of these may appear, or none if the
* constructor has default (package) access. * constructor has default (package) access.
*
* @return a string describing this {@code Constructor}
* @jls 8.8.3. Constructor Modifiers
*/ */
public String toString() { public String toString() {
return sharedToString(Modifier.constructorModifiers(), return sharedToString(Modifier.constructorModifiers(),
false,
parameterTypes, parameterTypes,
exceptionTypes); exceptionTypes);
} }
...@@ -328,10 +332,11 @@ public final class Constructor<T> extends Executable { ...@@ -328,10 +332,11 @@ public final class Constructor<T> extends Executable {
* include type parameters * include type parameters
* *
* @since 1.5 * @since 1.5
* @jls 8.8.3. Constructor Modifiers
*/ */
@Override @Override
public String toGenericString() { public String toGenericString() {
return sharedToGenericString(Modifier.constructorModifiers()); return sharedToGenericString(Modifier.constructorModifiers(), false);
} }
@Override @Override
......
...@@ -89,20 +89,31 @@ public abstract class Executable extends AccessibleObject ...@@ -89,20 +89,31 @@ public abstract class Executable extends AccessibleObject
} }
void printModifiersIfNonzero(StringBuilder sb, int mask) { void printModifiersIfNonzero(StringBuilder sb, int mask, boolean isDefault) {
int mod = getModifiers() & mask; int mod = getModifiers() & mask;
if (mod != 0) {
if (mod != 0 && !isDefault) {
sb.append(Modifier.toString(mod)).append(' '); sb.append(Modifier.toString(mod)).append(' ');
} else {
int access_mod = mod & Modifier.ACCESS_MODIFIERS;
if (access_mod != 0)
sb.append(Modifier.toString(access_mod)).append(' ');
if (isDefault)
sb.append("default ");
mod = (mod & ~Modifier.ACCESS_MODIFIERS);
if (mod != 0)
sb.append(Modifier.toString(mod)).append(' ');
} }
} }
String sharedToString(int modifierMask, String sharedToString(int modifierMask,
boolean isDefault,
Class<?>[] parameterTypes, Class<?>[] parameterTypes,
Class<?>[] exceptionTypes) { Class<?>[] exceptionTypes) {
try { try {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
printModifiersIfNonzero(sb, modifierMask); printModifiersIfNonzero(sb, modifierMask, isDefault);
specificToStringHeader(sb); specificToStringHeader(sb);
sb.append('('); sb.append('(');
...@@ -124,11 +135,11 @@ public abstract class Executable extends AccessibleObject ...@@ -124,11 +135,11 @@ public abstract class Executable extends AccessibleObject
*/ */
abstract void specificToStringHeader(StringBuilder sb); abstract void specificToStringHeader(StringBuilder sb);
String sharedToGenericString(int modifierMask) { String sharedToGenericString(int modifierMask, boolean isDefault) {
try { try {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
printModifiersIfNonzero(sb, modifierMask); printModifiersIfNonzero(sb, modifierMask, isDefault);
TypeVariable<?>[] typeparms = getTypeParameters(); TypeVariable<?>[] typeparms = getTypeParameters();
if (typeparms.length > 0) { if (typeparms.length > 0) {
......
...@@ -288,6 +288,9 @@ class Field extends AccessibleObject implements Member { ...@@ -288,6 +288,9 @@ class Field extends AccessibleObject implements Member {
* {@code protected} or {@code private} first, and then other * {@code protected} or {@code private} first, and then other
* modifiers in the following order: {@code static}, {@code final}, * modifiers in the following order: {@code static}, {@code final},
* {@code transient}, {@code volatile}. * {@code transient}, {@code volatile}.
*
* @return a string describing this {@code Field}
* @jls 8.3.1 Field Modifiers
*/ */
public String toString() { public String toString() {
int mod = getModifiers(); int mod = getModifiers();
...@@ -315,6 +318,7 @@ class Field extends AccessibleObject implements Member { ...@@ -315,6 +318,7 @@ class Field extends AccessibleObject implements Member {
* its generic type * its generic type
* *
* @since 1.5 * @since 1.5
* @jls 8.3.1 Field Modifiers
*/ */
public String toGenericString() { public String toGenericString() {
int mod = getModifiers(); int mod = getModifiers();
......
...@@ -343,10 +343,16 @@ public final class Method extends Executable { ...@@ -343,10 +343,16 @@ public final class Method extends Executable {
* {@code public}, {@code protected} or {@code private} first, * {@code public}, {@code protected} or {@code private} first,
* and then other modifiers in the following order: * and then other modifiers in the following order:
* {@code abstract}, {@code static}, {@code final}, * {@code abstract}, {@code static}, {@code final},
* {@code synchronized}, {@code native}, {@code strictfp}. * {@code synchronized}, {@code native}, {@code strictfp},
* {@code default}.
*
* @return a string describing this {@code Method}
*
* @jls 8.4.3 Method Modifiers
*/ */
public String toString() { public String toString() {
return sharedToString(Modifier.methodModifiers(), return sharedToString(Modifier.methodModifiers(),
isDefault(),
parameterTypes, parameterTypes,
exceptionTypes); exceptionTypes);
} }
...@@ -389,16 +395,19 @@ public final class Method extends Executable { ...@@ -389,16 +395,19 @@ public final class Method extends Executable {
* {@code public}, {@code protected} or {@code private} first, * {@code public}, {@code protected} or {@code private} first,
* and then other modifiers in the following order: * and then other modifiers in the following order:
* {@code abstract}, {@code static}, {@code final}, * {@code abstract}, {@code static}, {@code final},
* {@code synchronized}, {@code native}, {@code strictfp}. * {@code synchronized}, {@code native}, {@code strictfp},
* {@code default}.
* *
* @return a string describing this {@code Method}, * @return a string describing this {@code Method},
* include type parameters * include type parameters
* *
* @since 1.5 * @since 1.5
*
* @jls 8.4.3 Method Modifiers
*/ */
@Override @Override
public String toGenericString() { public String toGenericString() {
return sharedToGenericString(Modifier.methodModifiers()); return sharedToGenericString(Modifier.methodModifiers(), isDefault());
} }
@Override @Override
......
...@@ -389,6 +389,12 @@ class Modifier { ...@@ -389,6 +389,12 @@ class Modifier {
Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT |
Modifier.VOLATILE; Modifier.VOLATILE;
/**
*
*/
static final int ACCESS_MODIFIERS =
Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE;
/** /**
* Return an {@code int} value OR-ing together the source language * Return an {@code int} value OR-ing together the source language
* modifiers that can be applied to a class. * modifiers that can be applied to a class.
......
/* /*
* Copyright (c) 2004, 2006, Oracle and/or its affiliates. All rights reserved. * Copyright (c) 2004, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* *
* This code is free software; you can redistribute it and/or modify it * This code is free software; you can redistribute it and/or modify it
...@@ -23,7 +23,7 @@ ...@@ -23,7 +23,7 @@
/* /*
* @test * @test
* @bug 5033583 6316717 6470106 * @bug 5033583 6316717 6470106 8004979
* @summary Check toGenericString() and toString() methods * @summary Check toGenericString() and toString() methods
* @author Joseph D. Darcy * @author Joseph D. Darcy
*/ */
...@@ -39,6 +39,7 @@ public class GenericStringTest { ...@@ -39,6 +39,7 @@ public class GenericStringTest {
classList.add(TestClass1.class); classList.add(TestClass1.class);
classList.add(TestClass2.class); classList.add(TestClass2.class);
classList.add(Roebling.class); classList.add(Roebling.class);
classList.add(TestInterface1.class);
for(Class<?> clazz: classList) for(Class<?> clazz: classList)
...@@ -129,6 +130,27 @@ class Roebling implements Comparable<Roebling> { ...@@ -129,6 +130,27 @@ class Roebling implements Comparable<Roebling> {
void varArg(Object ... arg) {} void varArg(Object ... arg) {}
} }
interface TestInterface1 {
@ExpectedGenericString(
"public default void TestInterface1.foo()")
@ExpectedString(
"public default void TestInterface1.foo()")
public default void foo(){;}
@ExpectedString(
"public default java.lang.Object TestInterface1.bar()")
@ExpectedGenericString(
"public default <A> A TestInterface1.bar()")
default <A> A bar(){return null;}
@ExpectedString(
"public default strictfp double TestInterface1.quux()")
@ExpectedGenericString(
"public default strictfp double TestInterface1.quux()")
strictfp default double quux(){return 1.0;}
}
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@interface ExpectedGenericString { @interface ExpectedGenericString {
String value(); String value();
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册