langtools/test/tools/apt/mirror/declaration/ClassDecl.java
author jjg
Tue, 07 Oct 2008 15:39:19 -0700
changeset 1467 290837884931
parent 10 06bc494ca11e
child 1652 1324f96f3883
permissions -rw-r--r--
6749967: regression tests for apt should be same-vm friendly Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 2004-2007 Sun Microsystems, Inc.  All Rights Reserved.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * have any questions.
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
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
 * @test
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
 * @bug 4853450 4997614
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
 * @summary ClassDeclaration tests
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 * @library ../../lib
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 * @compile -source 1.5 ClassDecl.java
1467
290837884931 6749967: regression tests for apt should be same-vm friendly
jjg
parents: 10
diff changeset
    31
 * @run main/othervm ClassDecl
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import java.io.Serializable;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import java.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import com.sun.mirror.declaration.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
import com.sun.mirror.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
import com.sun.mirror.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * Sed Quis custodiet ipsos custodes?
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
@AT1
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
@AT2
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
public class ClassDecl extends Tester {
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    public static void main(String[] args) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        (new ClassDecl()).run();
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    private ClassDeclaration nested = null;     // a nested type
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    private ClassDeclaration object = null;     // java.lang.object
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    // A constructor to be found
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    private ClassDecl() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    // Another constructor to be found
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    private ClassDecl(int i) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        this();
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    // An extra field to be found
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    static int i;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    // Static initializer isn't among this class's methods.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    static {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
        i = 7;
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    // A nested class with some accoutrements
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    private static strictfp class NestedClass<T> implements Serializable {
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        void m1() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
        void m2() {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        void m2(int i) {}
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    protected void init() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        nested = (ClassDeclaration)
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            thisClassDecl.getNestedTypes().iterator().next();
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        object = (ClassDeclaration)
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            env.getTypeDeclaration("java.lang.Object");
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
    // Declaration methods
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
    @Test(result="class")
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
    Collection<String> accept() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        final Collection<String> res = new ArrayList<String>();
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        thisClassDecl.accept(new SimpleDeclarationVisitor() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            public void visitTypeDeclaration(TypeDeclaration t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                res.add("type");
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
            public void visitClassDeclaration(ClassDeclaration c) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                res.add("class");
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            public void visitEnumDeclaration(EnumDeclaration e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                res.add("enum");
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        });
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
        return res;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    @Test(result={"@AT1", "@AT2"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
    Collection<AnnotationMirror> getAnnotationMirrors() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        return thisClassDecl.getAnnotationMirrors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    @Test(result=" Sed Quis custodiet ipsos custodes?\n")
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    String getDocComment() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        return thisClassDecl.getDocComment();
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    @Test(result={"public"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
    Collection<Modifier> getModifiers1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
        return thisClassDecl.getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
    // Check that static nested class has "static" modifier, even though
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    // the VM doesn't set that bit.
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
    @Test(result={"private", "static", "strictfp"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    Collection<Modifier> getModifiers2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
        return nested.getModifiers();
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
    @Test(result="ClassDecl.java")
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    String getPosition() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        return thisClassDecl.getPosition().file().getName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
    @Test(result="ClassDecl")
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    String getSimpleName1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
        return thisClassDecl.getSimpleName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
    @Test(result="NestedClass")
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    String getSimpleName2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        return nested.getSimpleName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    // MemberDeclaration method
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
    @Test(result="null")
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
    TypeDeclaration getDeclaringType1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
        return thisClassDecl.getDeclaringType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    @Test(result="ClassDecl")
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    TypeDeclaration getDeclaringType2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
        return nested.getDeclaringType();
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    // TypeDeclaration methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
    @Test(result={"nested", "object", "i"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
    Collection<FieldDeclaration> getFields() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
        return thisClassDecl.getFields();
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    @Test(result={})
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    Collection<TypeParameterDeclaration> getFormalTypeParameters1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        return thisClassDecl.getFormalTypeParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    @Test(result="T")
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
    Collection<TypeParameterDeclaration> getFormalTypeParameters2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        return nested.getFormalTypeParameters();
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    @Test(result="ClassDecl.NestedClass<T>")
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
    Collection<TypeDeclaration> getNestedTypes() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
        return thisClassDecl.getNestedTypes();
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    @Test(result="")
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
    PackageDeclaration getPackage1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        return thisClassDecl.getPackage();
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
    @Test(result="java.lang")
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    PackageDeclaration getPackage2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        return object.getPackage();
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
    @Test(result="ClassDecl")
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    String getQualifiedName1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        return thisClassDecl.getQualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
    @Test(result="ClassDecl.NestedClass")
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
    String getQualifiedName2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
        return nested.getQualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
    @Test(result="java.lang.Object")
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
    String getQualifiedName3() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
        return object.getQualifiedName();
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    @Test(result="java.io.Serializable")
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    Collection<InterfaceType> getSuperinterfaces() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        return nested.getSuperinterfaces();
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    // ClassDeclaration methods
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    @Test(result={"ClassDecl()", "ClassDecl(int)"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
    Collection<ConstructorDeclaration> getConstructors1() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
        return thisClassDecl.getConstructors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
    // Check for default constructor.
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
    // 4997614: visitConstructionDeclaration reports info when there is no ctor
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    @Test(result={"NestedClass()"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    Collection<ConstructorDeclaration> getConstructors2() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        return nested.getConstructors();
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    @Test(result={"m1()", "m2()", "m2(int)"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
    Collection<MethodDeclaration> getMethods() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
        return nested.getMethods();
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
    @Test(result={"Tester"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    ClassType getSuperclass() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        return thisClassDecl.getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    @Test(result={"null"})
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    ClassType objectHasNoSuperclass() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        return object.getSuperclass();
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
// Annotations used for testing.
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
@interface AT1 {
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
}
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
@interface AT2 {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
}