langtools/test/tools/javac/defaultMethods/fd/shapegen/TTNode.java
author mcimadamore
Sun, 04 Nov 2012 10:59:42 +0000
changeset 14443 91c05eb19277
permissions -rw-r--r--
7192246: Add type-checking support for default methods Summary: Add type-checking support for default methods as per Featherweight-Defender document Reviewed-by: jjg, dlsmith
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14443
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     1
/*
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     4
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    10
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    15
 * accompanied this code).
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    16
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    20
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    23
 * questions.
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    24
 */
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    25
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    26
package shapegen;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    27
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    28
import shapegen.ClassCase.Kind;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    29
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    30
import java.util.ArrayList;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    31
import java.util.List;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    32
import java.util.Set;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    33
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    34
import static shapegen.ClassCase.Kind.*;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    35
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    36
/**
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    37
 * Type Template Node
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    38
 *
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    39
 * @author Robert Field
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    40
 */
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    41
public class TTNode {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    42
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    43
    final List<TTNode> supertypes;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    44
    final boolean canBeClass;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    45
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    46
    private int currentKindIndex;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    47
    private Kind[] kinds;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    48
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    49
    public TTNode(List<TTNode> subtypes, boolean canBeClass) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    50
        this.supertypes = subtypes;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    51
        this.canBeClass = canBeClass;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    52
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    53
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    54
    public void start(boolean includeClasses) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    55
        kinds =
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    56
             supertypes.isEmpty()?
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    57
                (new Kind[]{IDEFAULT, IPRESENT})
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    58
             :  ((includeClasses && canBeClass)?
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    59
                  new Kind[]{CNONE, IVAC, IDEFAULT, IPRESENT}
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    60
                : new Kind[]{IVAC, IDEFAULT, IPRESENT});
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    61
        currentKindIndex = 0;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    62
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    63
        for (TTNode sub : supertypes) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    64
            sub.start(includeClasses);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    65
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    66
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    67
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    68
    public boolean next() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    69
        ++currentKindIndex;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    70
        if (currentKindIndex >= kinds.length) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    71
            currentKindIndex = 0;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    72
            return false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    73
        } else {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    74
            return true;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    75
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    76
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    77
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    78
    public void collectAllSubtypes(Set<TTNode> subs) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    79
        subs.add(this);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    80
        for (TTNode n : supertypes) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    81
            n.collectAllSubtypes(subs);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    82
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    83
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    84
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    85
    private Kind getKind() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    86
        return kinds[currentKindIndex];
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    87
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    88
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    89
    boolean isInterface() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    90
        return getKind().isInterface;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    91
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    92
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    93
    boolean isClass() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    94
        return !isInterface();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    95
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    96
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    97
    boolean hasDefault() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    98
        return getKind() == IDEFAULT;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
    99
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   100
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   101
    public boolean isValid() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   102
        for (TTNode n : supertypes) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   103
            if (!n.isValid() || (isInterface() && n.isClass())) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   104
                return false;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   105
            }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   106
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   107
        return true;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   108
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   109
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   110
    public ClassCase genCase() {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   111
        ClassCase subclass;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   112
        List<TTNode> ttintfs;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   113
        if (isClass() && !supertypes.isEmpty() && supertypes.get(0).isClass()) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   114
            subclass = supertypes.get(0).genCase();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   115
            ttintfs = supertypes.subList(1, supertypes.size());
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   116
        } else {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   117
            subclass = null;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   118
            ttintfs = supertypes;
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   119
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   120
        List<ClassCase> intfs = new ArrayList<>();
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   121
        for (TTNode node : ttintfs) {
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   122
            intfs.add(node.genCase());
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   123
        }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   124
        return new ClassCase(getKind(), subclass, intfs);
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   125
    }
91c05eb19277 7192246: Add type-checking support for default methods
mcimadamore
parents:
diff changeset
   126
}