langtools/test/tools/javac/processing/model/element/TestModuleElementNames.java
author darcy
Mon, 19 Jun 2017 15:06:01 -0700
changeset 45687 07463ccee73b
permissions -rw-r--r--
8163989: Clarify ModuleElement spec Reviewed-by: abuckley, jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45687
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     1
/*
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     4
 *
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     8
 *
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    13
 * accompanied this code).
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    14
 *
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    18
 *
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    21
 * questions.
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    22
 */
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    23
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    24
/*
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    25
 * @test
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    26
 * @bug 8163989
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    27
 * @summary Test basic workings of naming methods on ModuleElement
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    28
 * @library /tools/javac/lib
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    29
 * @modules java.compiler
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    30
 *          jdk.compiler
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    31
 * @build   JavacTestingAbstractProcessor TestModuleElementNames
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    32
 * @compile -processor TestModuleElementNames -proc:only TestModuleElementNames.java
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    33
 */
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    34
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    35
import java.util.Set;
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    36
import javax.annotation.processing.*;
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    37
import javax.lang.model.element.*;
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    38
import javax.lang.model.util.*;
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    39
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    40
/**
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    41
 * Test basic workings of names of ModuleElement.
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    42
 */
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    43
public class TestModuleElementNames extends JavacTestingAbstractProcessor {
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    44
    public boolean process(Set<? extends TypeElement> annotations,
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    45
                           RoundEnvironment roundEnv) {
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    46
        if (!roundEnv.processingOver()) {
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    47
            checkNames(eltUtils.getModuleElement(""),          "",          "",          true);
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    48
            checkNames(eltUtils.getModuleElement("java.base"), "base",      "java.base", false);
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    49
        }
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    50
        return true;
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    51
    }
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    52
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    53
    private void checkNames(ModuleElement mod, String expectedSimple, String expectedQual, boolean expectedUnnamed) {
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    54
        boolean unnamed = mod.isUnnamed();
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    55
        String simpleName    = mod.getSimpleName().toString();
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    56
        String qualifiedName = mod.getQualifiedName().toString();
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    57
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    58
        if (unnamed != expectedUnnamed) {
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    59
            throw new RuntimeException("Unnamed mismatch on " + qualifiedName);
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    60
        }
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    61
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    62
        if (!simpleName.equals(expectedSimple) ||
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    63
            !qualifiedName.equals(expectedQual)) {
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    64
            throw new RuntimeException("Unexpected name,\tqualitifed ``" + qualifiedName +
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    65
                                       "''\tsimmple ``" + simpleName + "''");
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    66
        }
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    67
    }
07463ccee73b 8163989: Clarify ModuleElement spec
darcy
parents:
diff changeset
    68
}