diff --git a/jdk/src/share/classes/java/lang/reflect/Constructor.java b/jdk/src/share/classes/java/lang/reflect/Constructor.java index bdbd7c30e20..9d865cb6aaa 100644 --- a/jdk/src/share/classes/java/lang/reflect/Constructor.java +++ b/jdk/src/share/classes/java/lang/reflect/Constructor.java @@ -284,9 +284,13 @@ public final class Constructor extends Executable { * modifiers {@code public}, {@code protected} or * {@code private}. Only one of these may appear, or none if the * constructor has default (package) access. + * + * @return a string describing this {@code Constructor} + * @jls 8.8.3. Constructor Modifiers */ public String toString() { return sharedToString(Modifier.constructorModifiers(), + false, parameterTypes, exceptionTypes); } @@ -328,10 +332,11 @@ public final class Constructor extends Executable { * include type parameters * * @since 1.5 + * @jls 8.8.3. Constructor Modifiers */ @Override public String toGenericString() { - return sharedToGenericString(Modifier.constructorModifiers()); + return sharedToGenericString(Modifier.constructorModifiers(), false); } @Override diff --git a/jdk/src/share/classes/java/lang/reflect/Executable.java b/jdk/src/share/classes/java/lang/reflect/Executable.java index 1a89968b175..51e15f08363 100644 --- a/jdk/src/share/classes/java/lang/reflect/Executable.java +++ b/jdk/src/share/classes/java/lang/reflect/Executable.java @@ -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; - if (mod != 0) { + + if (mod != 0 && !isDefault) { 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, + boolean isDefault, Class[] parameterTypes, Class[] exceptionTypes) { try { StringBuilder sb = new StringBuilder(); - printModifiersIfNonzero(sb, modifierMask); + printModifiersIfNonzero(sb, modifierMask, isDefault); specificToStringHeader(sb); sb.append('('); @@ -124,11 +135,11 @@ public abstract class Executable extends AccessibleObject */ abstract void specificToStringHeader(StringBuilder sb); - String sharedToGenericString(int modifierMask) { + String sharedToGenericString(int modifierMask, boolean isDefault) { try { StringBuilder sb = new StringBuilder(); - printModifiersIfNonzero(sb, modifierMask); + printModifiersIfNonzero(sb, modifierMask, isDefault); TypeVariable[] typeparms = getTypeParameters(); if (typeparms.length > 0) { diff --git a/jdk/src/share/classes/java/lang/reflect/Field.java b/jdk/src/share/classes/java/lang/reflect/Field.java index be13b076832..947d042e17b 100644 --- a/jdk/src/share/classes/java/lang/reflect/Field.java +++ b/jdk/src/share/classes/java/lang/reflect/Field.java @@ -288,6 +288,9 @@ class Field extends AccessibleObject implements Member { * {@code protected} or {@code private} first, and then other * modifiers in the following order: {@code static}, {@code final}, * {@code transient}, {@code volatile}. + * + * @return a string describing this {@code Field} + * @jls 8.3.1 Field Modifiers */ public String toString() { int mod = getModifiers(); @@ -315,6 +318,7 @@ class Field extends AccessibleObject implements Member { * its generic type * * @since 1.5 + * @jls 8.3.1 Field Modifiers */ public String toGenericString() { int mod = getModifiers(); diff --git a/jdk/src/share/classes/java/lang/reflect/Method.java b/jdk/src/share/classes/java/lang/reflect/Method.java index 09438fbebe0..0dc3b244f9a 100644 --- a/jdk/src/share/classes/java/lang/reflect/Method.java +++ b/jdk/src/share/classes/java/lang/reflect/Method.java @@ -343,10 +343,16 @@ public final class Method extends Executable { * {@code public}, {@code protected} or {@code private} first, * and then other modifiers in the following order: * {@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() { return sharedToString(Modifier.methodModifiers(), + isDefault(), parameterTypes, exceptionTypes); } @@ -389,16 +395,19 @@ public final class Method extends Executable { * {@code public}, {@code protected} or {@code private} first, * and then other modifiers in the following order: * {@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}, * include type parameters * * @since 1.5 + * + * @jls 8.4.3 Method Modifiers */ @Override public String toGenericString() { - return sharedToGenericString(Modifier.methodModifiers()); + return sharedToGenericString(Modifier.methodModifiers(), isDefault()); } @Override diff --git a/jdk/src/share/classes/java/lang/reflect/Modifier.java b/jdk/src/share/classes/java/lang/reflect/Modifier.java index 8c2b2cc5f5e..9c0b2f40909 100644 --- a/jdk/src/share/classes/java/lang/reflect/Modifier.java +++ b/jdk/src/share/classes/java/lang/reflect/Modifier.java @@ -389,6 +389,12 @@ class Modifier { Modifier.STATIC | Modifier.FINAL | Modifier.TRANSIENT | Modifier.VOLATILE; + /** + * + */ + static final int ACCESS_MODIFIERS = + Modifier.PUBLIC | Modifier.PROTECTED | Modifier.PRIVATE; + /** * Return an {@code int} value OR-ing together the source language * modifiers that can be applied to a class. diff --git a/jdk/test/java/lang/reflect/Method/GenericStringTest.java b/jdk/test/java/lang/reflect/Method/GenericStringTest.java index 14c3c6226e6..9e50dca3aae 100644 --- a/jdk/test/java/lang/reflect/Method/GenericStringTest.java +++ b/jdk/test/java/lang/reflect/Method/GenericStringTest.java @@ -1,5 +1,5 @@ /* - * 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. * * This code is free software; you can redistribute it and/or modify it @@ -23,7 +23,7 @@ /* * @test - * @bug 5033583 6316717 6470106 + * @bug 5033583 6316717 6470106 8004979 * @summary Check toGenericString() and toString() methods * @author Joseph D. Darcy */ @@ -39,6 +39,7 @@ public class GenericStringTest { classList.add(TestClass1.class); classList.add(TestClass2.class); classList.add(Roebling.class); + classList.add(TestInterface1.class); for(Class clazz: classList) @@ -129,6 +130,27 @@ class Roebling implements Comparable { 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 TestInterface1.bar()") + default 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) @interface ExpectedGenericString { String value();