langtools/test/tools/apt/Basics/TestGetPackageApf.java
author schien
Thu, 26 May 2011 20:19:56 -0700
changeset 9806 d1779fd9f52f
parent 5520 86e4b9a9da40
permissions -rw-r--r--
Added tag jdk7-b144 for changeset 1a67669453f3
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) 2004, 2007, 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
import com.sun.mirror.apt.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
import com.sun.mirror.declaration.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
import com.sun.mirror.type.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.mirror.util.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import java.util.Collection;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.Set;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.Arrays;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import static java.util.Collections.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
import static com.sun.mirror.util.DeclarationVisitors.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * This class is used to test getPackage on classes that are
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * not already loaded.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
public class TestGetPackageApf implements AnnotationProcessorFactory {
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
    // Process any set of annotations
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
    private static final Collection<String> supportedAnnotations
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        = unmodifiableCollection(Arrays.asList("*"));
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
    // No supported options
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
    private static final Collection<String> supportedOptions = emptySet();
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
    public Collection<String> supportedAnnotationTypes() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
        return supportedAnnotations;
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    public Collection<String> supportedOptions() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
        return supportedOptions;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
    public AnnotationProcessor getProcessorFor(
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
            Set<AnnotationTypeDeclaration> atds,
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
            AnnotationProcessorEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        return new TestGetPackageAp(env);
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private static class TestGetPackageAp implements AnnotationProcessor {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        private final AnnotationProcessorEnvironment env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        TestGetPackageAp(AnnotationProcessorEnvironment env) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
            this.env = env;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
        public void process() {
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            boolean failed = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
            String packageNames[] = {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
                "", // unnamed package
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
                "java.lang.annotation",
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
                "java.lang",
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
                "java.util",
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                "java.awt.image.renderable",
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
                "foo.bar",
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
                "foo",
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
                "p1",
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
                // "p1.p2", // class p1.p2 obscures package p1.p2
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            for(String packageName: packageNames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
                PackageDeclaration p = env.getPackage(packageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
                if (p == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
                    failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
                    System.err.println("ERROR: No declaration found for ``" + packageName + "''.");
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
                else if (!packageName.equals(p.getQualifiedName())) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                    failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                    System.err.println("ERROR: Unexpected package name; expected " + packageName +
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                                       "got " + p.getQualifiedName());
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            String notPackageNames[] = {
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                "XXYZZY",
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
                "java.lang.String",
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
                "1",
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
                "1.2",
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
                "3.14159",
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
                "To be or not to be is a tautology",
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
                "1+2=3",
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
            };
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            for(String notPackageName: notPackageNames) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                PackageDeclaration p = env.getPackage(notPackageName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                if (p != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                    failed = true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
                    System.err.println("ERROR: Unexpected declaration: ``" + p + "''.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            if (failed)
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                throw new RuntimeException("Errors found testing getPackage.");
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
}