aoqi@0: /* aoqi@0: * Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 5009601 5010455 5005748 aoqi@0: * @summary enum constructors can be declared private aoqi@0: * @author Joseph D. Darcy aoqi@0: */ aoqi@0: aoqi@0: import java.util.*; aoqi@0: import java.lang.reflect.*; aoqi@0: import java.lang.annotation.*; aoqi@0: aoqi@0: /* aoqi@0: * Arguably, only the final and abstract should be held in aoqi@0: * ExpectedModifiers; whether or not an enum should be static could be aoqi@0: * inferred from getDeclaringClass and working versions of aoqi@0: * getEnclosingMethod and getEnclosingConstructor. I.e. if aoqi@0: * getDeclaringClass, getEnclosingMethod, and getEnclosingConstructor aoqi@0: * were all null, the enum is a top-level class and should not be aoqi@0: * static; otherwise, it should be static. aoqi@0: */ aoqi@0: aoqi@0: @ExpectedModifiers(Modifier.FINAL) aoqi@0: public enum EnumImplicitPrivateConstructor { aoqi@0: RED(255, 0, 0), aoqi@0: GREEN(0, 255, 0), aoqi@0: BLUE(0, 0, 255); aoqi@0: aoqi@0: private int r, g, b; aoqi@0: EnumImplicitPrivateConstructor(int r, int g, int b) { aoqi@0: this.r = r; aoqi@0: this.g = g; aoqi@0: this.b = b; aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Using reflection, Verify that aoqi@0: * 1. all non-synthetic constructors of enum classes are marked as private. aoqi@0: * 2. top-level enum classes are marked as static aoqi@0: * 3. enum's are marked final and abstract as appropriate aoqi@0: * 4. enum constructors *cannot* be invoked reflectively aoqi@0: */ aoqi@0: public static void main(String argv[]) throws Exception { aoqi@0: boolean passed = true; aoqi@0: aoqi@0: Collection classes = new LinkedHashSet(); aoqi@0: aoqi@0: classes.add(Class.forName("EnumImplicitPrivateConstructor")); aoqi@0: classes.add(Class.forName("EnumImplicitPrivateConstructor$AnotherEnum")); aoqi@0: classes.add(Class.forName("EnumImplicitPrivateConstructor$YetAnotherEnum")); aoqi@0: classes.add(Class.forName("EnumImplicitPrivateConstructor$OneMoreEnum")); aoqi@0: aoqi@0: // Add classes of specialized enum constants aoqi@0: for(Enum e: YetAnotherEnum.values()) aoqi@0: classes.add(e.getClass()); aoqi@0: aoqi@0: for(Class clazz: classes) { aoqi@0: System.out.println("Testing class " + clazz); aoqi@0: aoqi@0: int classModifiers = clazz.getModifiers(); aoqi@0: aoqi@0: // Why is this cast needed? aoqi@0: ExpectedModifiers em = (ExpectedModifiers)clazz.getAnnotation(ExpectedModifiers.class); aoqi@0: if (em != null) { aoqi@0: System.out.println("\tTesting expected modifiers"); aoqi@0: int expected = em.value(); aoqi@0: aoqi@0: if (expected != (classModifiers & (Modifier.ABSTRACT|Modifier.FINAL|Modifier.STATIC))) { aoqi@0: passed = false; aoqi@0: System.out.println("\tFAILED: Expected 0x" + Integer.toHexString(expected) + aoqi@0: " got 0x" +Integer.toHexString(classModifiers)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: for(Constructor ctor: clazz.getDeclaredConstructors() ) { aoqi@0: System.out.println("\tTesting constructor " + ctor); aoqi@0: aoqi@0: // We don't need no stinkin' access rules aoqi@0: try { aoqi@0: ctor.setAccessible(true); aoqi@0: } catch (java.security.AccessControlException ex) { aoqi@0: } aoqi@0: aoqi@0: int modifiers = ctor.getModifiers(); aoqi@0: aoqi@0: /* aoqi@0: * If clazz is for a specialized enum constant, the aoqi@0: * class will have the ENUM bit set but clazz.isEnum() aoqi@0: * will be false. A constructor in such a class must aoqi@0: * be non-private to allow the parent class to call aoqi@0: * the constructor. Therefore, only impose the aoqi@0: * private constructor check for genuine isEnum aoqi@0: * classes. aoqi@0: */ aoqi@0: if (clazz.isEnum()) { aoqi@0: if ((modifiers & Modifier.PRIVATE) == 0 && aoqi@0: ! ctor.isSynthetic() ) { aoqi@0: passed = false; aoqi@0: System.out.println("\tFAILED: Constructor not marked private: modifiers 0x" + aoqi@0: Integer.toHexString(modifiers)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: try { aoqi@0: // Should get exception trying to invoke aoqi@0: Object o = null; aoqi@0: try { aoqi@0: o = ctor.newInstance("abc", 123); aoqi@0: } catch (IllegalAccessException ex) { aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * A better test would query the number (and type) aoqi@0: * of parameters and create an appropriate aoqi@0: * argument list since IllegalArgumentException can be aoqi@0: * thrown for just using the wrong number of arguments. aoqi@0: */ aoqi@0: aoqi@0: if (o != null) { aoqi@0: passed = false; aoqi@0: System.err.println("Error: Created new enum object!"); aoqi@0: System.err.println(o.getClass()); aoqi@0: System.err.println(o.toString()); aoqi@0: } aoqi@0: } catch (IllegalArgumentException iae) {} aoqi@0: aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (!passed) aoqi@0: throw new RuntimeException("Error during testing."); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: /* aoqi@0: * Should be final and not abstract. aoqi@0: */ aoqi@0: @ExpectedModifiers(Modifier.FINAL|Modifier.STATIC) aoqi@0: enum AnotherEnum { aoqi@0: YELLOW, aoqi@0: CYAN, aoqi@0: MAGENTA; aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Should be neither final nor abstract. aoqi@0: */ aoqi@0: @ExpectedModifiers(Modifier.STATIC) aoqi@0: enum YetAnotherEnum { aoqi@0: GREEN { aoqi@0: int value(){ return 1;} aoqi@0: }, aoqi@0: aoqi@0: ORANGE { aoqi@0: int value(){ return 2;} aoqi@0: }, aoqi@0: aoqi@0: VIOLET { aoqi@0: int value(){ return 3;} aoqi@0: }; aoqi@0: aoqi@0: int value(){ return 0;} aoqi@0: } aoqi@0: aoqi@0: /* aoqi@0: * Should be abstract and not final. aoqi@0: */ aoqi@0: @ExpectedModifiers(Modifier.ABSTRACT|Modifier.STATIC) aoqi@0: enum OneMoreEnum { aoqi@0: SANGUINE { aoqi@0: int value(){ return 1;} aoqi@0: }, aoqi@0: aoqi@0: VERDANT { aoqi@0: int value(){ return 2;} aoqi@0: }, aoqi@0: aoqi@0: CERULEAN { aoqi@0: int value(){ return 3;} aoqi@0: }; aoqi@0: aoqi@0: abstract int value(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: @Retention(RetentionPolicy.RUNTIME) aoqi@0: @interface ExpectedModifiers { aoqi@0: int value(); aoqi@0: }