test/tools/javac/enum/EnumImplicitPrivateConstructor.java

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial