langtools/test/tools/javac/AccessMethods/ConstructorAccess.java
author never
Fri, 25 Mar 2011 18:50:10 -0700
changeset 8867 6febcc945802
parent 5520 86e4b9a9da40
permissions -rw-r--r--
7022204: LogFile wildcarding should use %p instead of star Reviewed-by: coleenp, jrose
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: 10
diff changeset
     2
 * Copyright (c) 1998, 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: 10
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 10
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 4116460
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @summary Test access methods for private constructors.
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @author William Maddox (maddox)
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 * @compile ConstructorAccess.java
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
 * @run main ConstructorAccess
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
public class ConstructorAccess {
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
    int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
    char c = 'x';
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
    private ConstructorAccess() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
        this.i = 42;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    private ConstructorAccess(int i, char c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
        this.i = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        this.c = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    class Inner {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
        int j;
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
        char c;
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        boolean b;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
        private Inner() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
            this.j = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
            this.b = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            this.c = 'x';
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        private Inner(int i, char c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            this.j = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            this.b = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
            this.c = c;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            ConstructorAccess.this.i = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        private Inner(int i, boolean b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            this.j = i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            this.b = b;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
            this.c = 'x';
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
        void foo() throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
            ConstructorAccess x = new ConstructorAccess();
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            if (x.i != 42 || x.c != 'x') {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                throw new Exception("error 1");
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            ConstructorAccess y = new ConstructorAccess(555, 'y');
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            if (y.i != 555 || y.c != 'y') {
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
                throw new Exception("error2");
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        void check(int j, char c, boolean b) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
            if (this.j != j || this.c != c || this.b != b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                throw new Exception("error3");
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    void bar() throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        Inner x = new Inner();
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        x.check(0, 'x', false);
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        x.foo();
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        Inner y = new Inner(747, 'z');
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        y.check(747, 'z', true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
        if (this.i != 747) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            throw new Exception("error 4");
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        Inner z = new Inner(777, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        z.check(777, 'x' , true);
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    class InnerSub extends Inner {
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        private InnerSub() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            super();
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
        private InnerSub(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            super(i, 'w');
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        private InnerSub(int i, boolean b) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            super(i, b);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    public static void main(String[] args) throws Exception {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        ConstructorAccess o = new ConstructorAccess();
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        o.bar();
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        InnerSub x = o.new InnerSub();
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        x.check(0, 'x', false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        x.foo();
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        InnerSub y = o.new InnerSub(767);
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        y.check(767, 'w', true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        if (o.i != 767) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
            throw new Exception("error 5");
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        InnerSub z = o.new InnerSub(777, true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        z.check(777, 'x' , true);
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
}