Sat, 22 Dec 2018 13:10:38 -0800
Added tag jdk8u202-b25 for changeset 15d8c732039b
duke@1 | 1 | /* |
ohair@554 | 2 | * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. |
duke@1 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
duke@1 | 4 | * |
duke@1 | 5 | * This code is free software; you can redistribute it and/or modify it |
duke@1 | 6 | * under the terms of the GNU General Public License version 2 only, as |
duke@1 | 7 | * published by the Free Software Foundation. |
duke@1 | 8 | * |
duke@1 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
duke@1 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
duke@1 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
duke@1 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
duke@1 | 13 | * accompanied this code). |
duke@1 | 14 | * |
duke@1 | 15 | * You should have received a copy of the GNU General Public License version |
duke@1 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
duke@1 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
duke@1 | 18 | * |
ohair@554 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@554 | 20 | * or visit www.oracle.com if you need additional information or have any |
ohair@554 | 21 | * questions. |
duke@1 | 22 | */ |
duke@1 | 23 | |
duke@1 | 24 | /* |
duke@1 | 25 | * @test |
duke@1 | 26 | * @bug 5009601 5010455 5005748 |
duke@1 | 27 | * @summary enum constructors can be declared private |
duke@1 | 28 | * @author Joseph D. Darcy |
duke@1 | 29 | */ |
duke@1 | 30 | |
duke@1 | 31 | import java.util.*; |
duke@1 | 32 | import java.lang.reflect.*; |
duke@1 | 33 | import java.lang.annotation.*; |
duke@1 | 34 | |
duke@1 | 35 | /* |
duke@1 | 36 | * Arguably, only the final and abstract should be held in |
duke@1 | 37 | * ExpectedModifiers; whether or not an enum should be static could be |
duke@1 | 38 | * inferred from getDeclaringClass and working versions of |
duke@1 | 39 | * getEnclosingMethod and getEnclosingConstructor. I.e. if |
duke@1 | 40 | * getDeclaringClass, getEnclosingMethod, and getEnclosingConstructor |
duke@1 | 41 | * were all null, the enum is a top-level class and should not be |
duke@1 | 42 | * static; otherwise, it should be static. |
duke@1 | 43 | */ |
duke@1 | 44 | |
duke@1 | 45 | @ExpectedModifiers(Modifier.FINAL) |
duke@1 | 46 | public enum EnumImplicitPrivateConstructor { |
duke@1 | 47 | RED(255, 0, 0), |
duke@1 | 48 | GREEN(0, 255, 0), |
duke@1 | 49 | BLUE(0, 0, 255); |
duke@1 | 50 | |
duke@1 | 51 | private int r, g, b; |
duke@1 | 52 | EnumImplicitPrivateConstructor(int r, int g, int b) { |
duke@1 | 53 | this.r = r; |
duke@1 | 54 | this.g = g; |
duke@1 | 55 | this.b = b; |
duke@1 | 56 | } |
duke@1 | 57 | |
duke@1 | 58 | /* |
duke@1 | 59 | * Using reflection, Verify that |
duke@1 | 60 | * 1. all non-synthetic constructors of enum classes are marked as private. |
duke@1 | 61 | * 2. top-level enum classes are marked as static |
duke@1 | 62 | * 3. enum's are marked final and abstract as appropriate |
duke@1 | 63 | * 4. enum constructors *cannot* be invoked reflectively |
duke@1 | 64 | */ |
duke@1 | 65 | public static void main(String argv[]) throws Exception { |
duke@1 | 66 | boolean passed = true; |
duke@1 | 67 | |
duke@1 | 68 | Collection<Class> classes = new LinkedHashSet<Class>(); |
duke@1 | 69 | |
duke@1 | 70 | classes.add(Class.forName("EnumImplicitPrivateConstructor")); |
duke@1 | 71 | classes.add(Class.forName("EnumImplicitPrivateConstructor$AnotherEnum")); |
duke@1 | 72 | classes.add(Class.forName("EnumImplicitPrivateConstructor$YetAnotherEnum")); |
duke@1 | 73 | classes.add(Class.forName("EnumImplicitPrivateConstructor$OneMoreEnum")); |
duke@1 | 74 | |
duke@1 | 75 | // Add classes of specialized enum constants |
duke@1 | 76 | for(Enum e: YetAnotherEnum.values()) |
duke@1 | 77 | classes.add(e.getClass()); |
duke@1 | 78 | |
duke@1 | 79 | for(Class clazz: classes) { |
duke@1 | 80 | System.out.println("Testing class " + clazz); |
duke@1 | 81 | |
duke@1 | 82 | int classModifiers = clazz.getModifiers(); |
duke@1 | 83 | |
duke@1 | 84 | // Why is this cast needed? |
duke@1 | 85 | ExpectedModifiers em = (ExpectedModifiers)clazz.getAnnotation(ExpectedModifiers.class); |
duke@1 | 86 | if (em != null) { |
duke@1 | 87 | System.out.println("\tTesting expected modifiers"); |
duke@1 | 88 | int expected = em.value(); |
duke@1 | 89 | |
duke@1 | 90 | if (expected != (classModifiers & (Modifier.ABSTRACT|Modifier.FINAL|Modifier.STATIC))) { |
duke@1 | 91 | passed = false; |
duke@1 | 92 | System.out.println("\tFAILED: Expected 0x" + Integer.toHexString(expected) + |
duke@1 | 93 | " got 0x" +Integer.toHexString(classModifiers)); |
duke@1 | 94 | } |
duke@1 | 95 | } |
duke@1 | 96 | |
duke@1 | 97 | for(Constructor ctor: clazz.getDeclaredConstructors() ) { |
duke@1 | 98 | System.out.println("\tTesting constructor " + ctor); |
duke@1 | 99 | |
duke@1 | 100 | // We don't need no stinkin' access rules |
duke@1 | 101 | try { |
duke@1 | 102 | ctor.setAccessible(true); |
duke@1 | 103 | } catch (java.security.AccessControlException ex) { |
duke@1 | 104 | } |
duke@1 | 105 | |
duke@1 | 106 | int modifiers = ctor.getModifiers(); |
duke@1 | 107 | |
duke@1 | 108 | /* |
duke@1 | 109 | * If clazz is for a specialized enum constant, the |
duke@1 | 110 | * class will have the ENUM bit set but clazz.isEnum() |
duke@1 | 111 | * will be false. A constructor in such a class must |
duke@1 | 112 | * be non-private to allow the parent class to call |
duke@1 | 113 | * the constructor. Therefore, only impose the |
duke@1 | 114 | * private constructor check for genuine isEnum |
duke@1 | 115 | * classes. |
duke@1 | 116 | */ |
duke@1 | 117 | if (clazz.isEnum()) { |
duke@1 | 118 | if ((modifiers & Modifier.PRIVATE) == 0 && |
duke@1 | 119 | ! ctor.isSynthetic() ) { |
duke@1 | 120 | passed = false; |
duke@1 | 121 | System.out.println("\tFAILED: Constructor not marked private: modifiers 0x" + |
duke@1 | 122 | Integer.toHexString(modifiers)); |
duke@1 | 123 | } |
duke@1 | 124 | } |
duke@1 | 125 | |
duke@1 | 126 | try { |
duke@1 | 127 | // Should get exception trying to invoke |
duke@1 | 128 | Object o = null; |
duke@1 | 129 | try { |
duke@1 | 130 | o = ctor.newInstance("abc", 123); |
duke@1 | 131 | } catch (IllegalAccessException ex) { |
duke@1 | 132 | } |
duke@1 | 133 | |
duke@1 | 134 | /* |
duke@1 | 135 | * A better test would query the number (and type) |
duke@1 | 136 | * of parameters and create an appropriate |
duke@1 | 137 | * argument list since IllegalArgumentException can be |
duke@1 | 138 | * thrown for just using the wrong number of arguments. |
duke@1 | 139 | */ |
duke@1 | 140 | |
duke@1 | 141 | if (o != null) { |
duke@1 | 142 | passed = false; |
duke@1 | 143 | System.err.println("Error: Created new enum object!"); |
duke@1 | 144 | System.err.println(o.getClass()); |
duke@1 | 145 | System.err.println(o.toString()); |
duke@1 | 146 | } |
duke@1 | 147 | } catch (IllegalArgumentException iae) {} |
duke@1 | 148 | |
duke@1 | 149 | } |
duke@1 | 150 | } |
duke@1 | 151 | |
duke@1 | 152 | if (!passed) |
duke@1 | 153 | throw new RuntimeException("Error during testing."); |
duke@1 | 154 | } |
duke@1 | 155 | |
duke@1 | 156 | |
duke@1 | 157 | /* |
duke@1 | 158 | * Should be final and not abstract. |
duke@1 | 159 | */ |
duke@1 | 160 | @ExpectedModifiers(Modifier.FINAL|Modifier.STATIC) |
duke@1 | 161 | enum AnotherEnum { |
duke@1 | 162 | YELLOW, |
duke@1 | 163 | CYAN, |
duke@1 | 164 | MAGENTA; |
duke@1 | 165 | } |
duke@1 | 166 | |
duke@1 | 167 | /* |
duke@1 | 168 | * Should be neither final nor abstract. |
duke@1 | 169 | */ |
duke@1 | 170 | @ExpectedModifiers(Modifier.STATIC) |
duke@1 | 171 | enum YetAnotherEnum { |
duke@1 | 172 | GREEN { |
duke@1 | 173 | int value(){ return 1;} |
duke@1 | 174 | }, |
duke@1 | 175 | |
duke@1 | 176 | ORANGE { |
duke@1 | 177 | int value(){ return 2;} |
duke@1 | 178 | }, |
duke@1 | 179 | |
duke@1 | 180 | VIOLET { |
duke@1 | 181 | int value(){ return 3;} |
duke@1 | 182 | }; |
duke@1 | 183 | |
duke@1 | 184 | int value(){ return 0;} |
duke@1 | 185 | } |
duke@1 | 186 | |
duke@1 | 187 | /* |
duke@1 | 188 | * Should be abstract and not final. |
duke@1 | 189 | */ |
duke@1 | 190 | @ExpectedModifiers(Modifier.ABSTRACT|Modifier.STATIC) |
duke@1 | 191 | enum OneMoreEnum { |
duke@1 | 192 | SANGUINE { |
duke@1 | 193 | int value(){ return 1;} |
duke@1 | 194 | }, |
duke@1 | 195 | |
duke@1 | 196 | VERDANT { |
duke@1 | 197 | int value(){ return 2;} |
duke@1 | 198 | }, |
duke@1 | 199 | |
duke@1 | 200 | CERULEAN { |
duke@1 | 201 | int value(){ return 3;} |
duke@1 | 202 | }; |
duke@1 | 203 | |
duke@1 | 204 | abstract int value(); |
duke@1 | 205 | } |
duke@1 | 206 | } |
duke@1 | 207 | |
duke@1 | 208 | @Retention(RetentionPolicy.RUNTIME) |
duke@1 | 209 | @interface ExpectedModifiers { |
duke@1 | 210 | int value(); |
duke@1 | 211 | } |