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