langtools/test/tools/javac/processing/model/util/types/TestPseudoTypeHandling.java
author darcy
Thu, 23 Feb 2017 13:28:55 -0800
changeset 44018 d7e24dd75175
parent 44014 56d15b6333fc
permissions -rw-r--r--
8175786: Fix small doc issues Reviewed-by: jjg, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44014
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     1
/*
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     4
 *
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     7
 * published by the Free Software Foundation.
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     8
 *
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    13
 * accompanied this code).
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    14
 *
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    18
 *
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    21
 * questions.
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    22
 */
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    23
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    24
/*
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    25
 * @test
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    26
 * @bug 8175335
44018
d7e24dd75175 8175786: Fix small doc issues
darcy
parents: 44014
diff changeset
    27
 * @summary Test Types methods on module and package TypeMirrors
44014
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    28
 * @author  Joseph D. Darcy
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    29
 * @library /tools/javac/lib
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    30
 * @modules jdk.compiler
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    31
 * @build   JavacTestingAbstractProcessor TestPseudoTypeHandling
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    32
 * @compile -processor TestPseudoTypeHandling -proc:only TestPseudoTypeHandling.java
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    33
 */
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    34
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    35
import java.util.*;
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    36
import java.util.function.*;
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    37
import static java.util.Objects.*;
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    38
import javax.annotation.processing.*;
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    39
import javax.lang.model.element.*;
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    40
import javax.lang.model.type.*;
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    41
import javax.lang.model.util.*;
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    42
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    43
/**
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    44
 * Test basic handling of module type.
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    45
 */
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    46
public class TestPseudoTypeHandling extends JavacTestingAbstractProcessor {
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    47
    public boolean process(Set<? extends TypeElement> annotations,
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    48
                           RoundEnvironment roundEnv) {
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    49
        if (!roundEnv.processingOver()) {
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    50
            TypeMirror objectType  = requireNonNull(eltUtils.getTypeElement("java.lang.Object")).asType();
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    51
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    52
            List<TypeMirror> typeMirrorsToTest =
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    53
                List.of(requireNonNull(eltUtils.getModuleElement("java.base")).asType(),
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    54
                        requireNonNull(eltUtils.getPackageElement("java.lang")).asType());
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    55
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    56
            for (TypeMirror type : typeMirrorsToTest) {
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    57
                expectException(t -> typeUtils.isSubtype(t, objectType), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    58
                expectException(t -> typeUtils.isSubtype(objectType, t), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    59
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    60
                expectException(t -> typeUtils.isAssignable(t, objectType), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    61
                expectException(t -> typeUtils.isAssignable(objectType, t), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    62
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    63
                expectException(t -> typeUtils.contains(t, objectType), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    64
                expectException(t -> typeUtils.contains(objectType, t), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    65
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    66
                expectException(t -> typeUtils.capture(t), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    67
                expectException(t -> typeUtils.erasure(t), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    68
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    69
                expectException(t -> typeUtils.getArrayType(t), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    70
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    71
                expectException(t -> typeUtils.directSupertypes(t), type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    72
            }
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    73
        }
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    74
        return true;
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    75
    }
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    76
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    77
    void expectException(Consumer<TypeMirror> argument, TypeMirror type) {
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    78
        try {
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    79
            argument.accept(type);
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    80
            throw new RuntimeException("Should not reach " + type.toString());
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    81
        } catch (IllegalArgumentException e) {
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    82
            ; // Expected
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    83
        }
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    84
    }
56d15b6333fc 8175335: Improve handling of module types in javax.lang.model.util.Types
darcy
parents:
diff changeset
    85
}