1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug  8888888
  27  * @summary Test basic modeling of a sealed type
  28  * @library /tools/javac/lib
  29  * @modules jdk.compiler
  30  * @build   JavacTestingAbstractProcessor TestSealed
  31  * @compile -processor TestSealed -proc:only TestSealed.java
  32  */
  33 
  34 import java.io.*;
  35 import javax.annotation.processing.*;
  36 import javax.lang.model.*;
  37 import javax.lang.model.element.*;
  38 import javax.lang.model.type.*;
  39 import javax.lang.model.util.*;
  40 import java.time.*;
  41 import java.util.*;
  42 
  43 /**
  44  * Basic tests of modeling a sealed types.
  45  */
  46 public class TestSealed extends JavacTestingAbstractProcessor {
  47 
  48     public boolean process(Set<? extends TypeElement> annotations,
  49                           RoundEnvironment roundEnv) {
  50        if (!roundEnv.processingOver()) {
  51            ElementScanner scanner = new SealedScanner();
  52            for(Element rootElement : roundEnv.getRootElements()) {
  53                scanner.visit(rootElement);
  54            }
  55        }
  56        return true;
  57     }
  58 
  59     sealed interface SealedI permits Sub1, Sub2 {}
  60 
  61     class Sub1 implements SealedI {}
  62     class Sub2 implements SealedI {}
  63 
  64     class SealedScanner extends ElementScanner<Void, Void> {
  65 
  66        public SealedScanner() {
  67            super();
  68        }
  69 
  70        @Override
  71        public Void visitType(TypeElement element, Void p) {
  72            System.out.println("Name: " + element.getSimpleName() +
  73                               "\tKind: " + element.getKind());
  74            if (element.getKind() == ElementKind.INTERFACE) {
  75                if (!element.getModifiers().contains(Modifier.SEALED)) {
  76                    throw new RuntimeException("sealed modifier expected");
  77                }
  78                List<? extends TypeMirror> permittedSubtypes = element.getPermittedSubtypes();
  79                if (permittedSubtypes.size() != 2) {
  80                    throw new RuntimeException("unexpected number of permitted subtypes");
  81                }
  82            }
  83            return super.visitType(element, p);
  84        }
  85     }
  86 }