test/tools/javac/enum/EnumImplicitPrivateConstructor.java

Mon, 29 Sep 2008 12:00:29 +0100

author
mcimadamore
date
Mon, 29 Sep 2008 12:00:29 +0100
changeset 122
1a9276e7cb18
parent 1
9a66ca7c79fa
child 289
84061bd68019
permissions
-rw-r--r--

6747671: -Xlint:rawtypes
Summary: add an Xlint option for detecting all raw types usages (ccc-approved)
Reviewed-by: jjg

duke@1 1 /*
duke@1 2 * Copyright 2004 Sun Microsystems, Inc. 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 *
duke@1 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 21 * have any 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 * @compile -source 1.5 EnumImplicitPrivateConstructor.java
duke@1 31 * @run main EnumImplicitPrivateConstructor
duke@1 32 */
duke@1 33
duke@1 34 import java.util.*;
duke@1 35 import java.lang.reflect.*;
duke@1 36 import java.lang.annotation.*;
duke@1 37
duke@1 38 /*
duke@1 39 * Arguably, only the final and abstract should be held in
duke@1 40 * ExpectedModifiers; whether or not an enum should be static could be
duke@1 41 * inferred from getDeclaringClass and working versions of
duke@1 42 * getEnclosingMethod and getEnclosingConstructor. I.e. if
duke@1 43 * getDeclaringClass, getEnclosingMethod, and getEnclosingConstructor
duke@1 44 * were all null, the enum is a top-level class and should not be
duke@1 45 * static; otherwise, it should be static.
duke@1 46 */
duke@1 47
duke@1 48 @ExpectedModifiers(Modifier.FINAL)
duke@1 49 public enum EnumImplicitPrivateConstructor {
duke@1 50 RED(255, 0, 0),
duke@1 51 GREEN(0, 255, 0),
duke@1 52 BLUE(0, 0, 255);
duke@1 53
duke@1 54 private int r, g, b;
duke@1 55 EnumImplicitPrivateConstructor(int r, int g, int b) {
duke@1 56 this.r = r;
duke@1 57 this.g = g;
duke@1 58 this.b = b;
duke@1 59 }
duke@1 60
duke@1 61 /*
duke@1 62 * Using reflection, Verify that
duke@1 63 * 1. all non-synthetic constructors of enum classes are marked as private.
duke@1 64 * 2. top-level enum classes are marked as static
duke@1 65 * 3. enum's are marked final and abstract as appropriate
duke@1 66 * 4. enum constructors *cannot* be invoked reflectively
duke@1 67 */
duke@1 68 public static void main(String argv[]) throws Exception {
duke@1 69 boolean passed = true;
duke@1 70
duke@1 71 Collection<Class> classes = new LinkedHashSet<Class>();
duke@1 72
duke@1 73 classes.add(Class.forName("EnumImplicitPrivateConstructor"));
duke@1 74 classes.add(Class.forName("EnumImplicitPrivateConstructor$AnotherEnum"));
duke@1 75 classes.add(Class.forName("EnumImplicitPrivateConstructor$YetAnotherEnum"));
duke@1 76 classes.add(Class.forName("EnumImplicitPrivateConstructor$OneMoreEnum"));
duke@1 77
duke@1 78 // Add classes of specialized enum constants
duke@1 79 for(Enum e: YetAnotherEnum.values())
duke@1 80 classes.add(e.getClass());
duke@1 81
duke@1 82 for(Class clazz: classes) {
duke@1 83 System.out.println("Testing class " + clazz);
duke@1 84
duke@1 85 int classModifiers = clazz.getModifiers();
duke@1 86
duke@1 87 // Why is this cast needed?
duke@1 88 ExpectedModifiers em = (ExpectedModifiers)clazz.getAnnotation(ExpectedModifiers.class);
duke@1 89 if (em != null) {
duke@1 90 System.out.println("\tTesting expected modifiers");
duke@1 91 int expected = em.value();
duke@1 92
duke@1 93 if (expected != (classModifiers & (Modifier.ABSTRACT|Modifier.FINAL|Modifier.STATIC))) {
duke@1 94 passed = false;
duke@1 95 System.out.println("\tFAILED: Expected 0x" + Integer.toHexString(expected) +
duke@1 96 " got 0x" +Integer.toHexString(classModifiers));
duke@1 97 }
duke@1 98 }
duke@1 99
duke@1 100 for(Constructor ctor: clazz.getDeclaredConstructors() ) {
duke@1 101 System.out.println("\tTesting constructor " + ctor);
duke@1 102
duke@1 103 // We don't need no stinkin' access rules
duke@1 104 try {
duke@1 105 ctor.setAccessible(true);
duke@1 106 } catch (java.security.AccessControlException ex) {
duke@1 107 }
duke@1 108
duke@1 109 int modifiers = ctor.getModifiers();
duke@1 110
duke@1 111 /*
duke@1 112 * If clazz is for a specialized enum constant, the
duke@1 113 * class will have the ENUM bit set but clazz.isEnum()
duke@1 114 * will be false. A constructor in such a class must
duke@1 115 * be non-private to allow the parent class to call
duke@1 116 * the constructor. Therefore, only impose the
duke@1 117 * private constructor check for genuine isEnum
duke@1 118 * classes.
duke@1 119 */
duke@1 120 if (clazz.isEnum()) {
duke@1 121 if ((modifiers & Modifier.PRIVATE) == 0 &&
duke@1 122 ! ctor.isSynthetic() ) {
duke@1 123 passed = false;
duke@1 124 System.out.println("\tFAILED: Constructor not marked private: modifiers 0x" +
duke@1 125 Integer.toHexString(modifiers));
duke@1 126 }
duke@1 127 }
duke@1 128
duke@1 129 try {
duke@1 130 // Should get exception trying to invoke
duke@1 131 Object o = null;
duke@1 132 try {
duke@1 133 o = ctor.newInstance("abc", 123);
duke@1 134 } catch (IllegalAccessException ex) {
duke@1 135 }
duke@1 136
duke@1 137 /*
duke@1 138 * A better test would query the number (and type)
duke@1 139 * of parameters and create an appropriate
duke@1 140 * argument list since IllegalArgumentException can be
duke@1 141 * thrown for just using the wrong number of arguments.
duke@1 142 */
duke@1 143
duke@1 144 if (o != null) {
duke@1 145 passed = false;
duke@1 146 System.err.println("Error: Created new enum object!");
duke@1 147 System.err.println(o.getClass());
duke@1 148 System.err.println(o.toString());
duke@1 149 }
duke@1 150 } catch (IllegalArgumentException iae) {}
duke@1 151
duke@1 152 }
duke@1 153 }
duke@1 154
duke@1 155 if (!passed)
duke@1 156 throw new RuntimeException("Error during testing.");
duke@1 157 }
duke@1 158
duke@1 159
duke@1 160 /*
duke@1 161 * Should be final and not abstract.
duke@1 162 */
duke@1 163 @ExpectedModifiers(Modifier.FINAL|Modifier.STATIC)
duke@1 164 enum AnotherEnum {
duke@1 165 YELLOW,
duke@1 166 CYAN,
duke@1 167 MAGENTA;
duke@1 168 }
duke@1 169
duke@1 170 /*
duke@1 171 * Should be neither final nor abstract.
duke@1 172 */
duke@1 173 @ExpectedModifiers(Modifier.STATIC)
duke@1 174 enum YetAnotherEnum {
duke@1 175 GREEN {
duke@1 176 int value(){ return 1;}
duke@1 177 },
duke@1 178
duke@1 179 ORANGE {
duke@1 180 int value(){ return 2;}
duke@1 181 },
duke@1 182
duke@1 183 VIOLET {
duke@1 184 int value(){ return 3;}
duke@1 185 };
duke@1 186
duke@1 187 int value(){ return 0;}
duke@1 188 }
duke@1 189
duke@1 190 /*
duke@1 191 * Should be abstract and not final.
duke@1 192 */
duke@1 193 @ExpectedModifiers(Modifier.ABSTRACT|Modifier.STATIC)
duke@1 194 enum OneMoreEnum {
duke@1 195 SANGUINE {
duke@1 196 int value(){ return 1;}
duke@1 197 },
duke@1 198
duke@1 199 VERDANT {
duke@1 200 int value(){ return 2;}
duke@1 201 },
duke@1 202
duke@1 203 CERULEAN {
duke@1 204 int value(){ return 3;}
duke@1 205 };
duke@1 206
duke@1 207 abstract int value();
duke@1 208 }
duke@1 209 }
duke@1 210
duke@1 211 @Retention(RetentionPolicy.RUNTIME)
duke@1 212 @interface ExpectedModifiers {
duke@1 213 int value();
duke@1 214 }

mercurial