Tue, 20 Nov 2012 09:58:55 -0800
8003639: convert lambda testng tests to jtreg and add them
Reviewed-by: mcimadamore
1 /*
2 * Copyright (c) 2012, 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. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
26 package org.openjdk.tests.shapegen;
28 import java.util.HashSet;
29 import java.util.Set;
31 import static org.openjdk.tests.shapegen.ClassCase.Kind.*;
33 /**
34 *
35 * @author Robert Field
36 */
37 public class RuleGroup {
39 final String name;
40 private final Rule[] rules;
42 public RuleGroup(String name, Rule[] rules) {
43 this.name = name;
44 this.rules = rules;
45 }
47 public boolean exec(ClassCase cc) {
48 boolean found = false;
49 for (Rule rule : rules) {
50 if (rule.guard(cc)) {
51 if (found) {
52 throw new RuntimeException("Bad rules -- multiple matches " + toString() + " for " + cc);
53 } else {
54 rule.eval(cc);
55 found = true;
56 }
57 }
58 }
59 return found;
60 }
62 @Override
63 public String toString() {
64 return name;
65 }
67 public static RuleGroup PROVENENCE = new RuleGroup("Provenence", new Rule[] {
68 new Rule("P-CDeclare") {
69 boolean guard(ClassCase cc) {
70 return cc.isa(CCONCRETE, CABSTRACT);
71 }
73 void eval(ClassCase cc) {
74 cc.set_mprov(cc);
75 cc.set_HasClassMethod(true);
76 }
77 },
79 new Rule("P-IDeclare") {
80 boolean guard(ClassCase cc) {
81 return cc.isa(IDEFAULT, IPRESENT);
82 }
84 void eval(ClassCase cc) {
85 cc.set_mprov(cc);
86 }
87 },
89 new Rule("P-IntfInh") {
90 boolean guard(ClassCase cc) {
91 return cc.isa(IVAC, CNONE) && !(cc.hasSuperclass() && cc.getSuperclass().get_HasClassMethod());
92 }
94 void eval(ClassCase cc) {
95 Set<ClassCase> _S = new HashSet<>();
96 for (ClassCase t : cc.getSupertypes()) {
97 _S.addAll(t.get_mprov());
98 }
99 Set<ClassCase> tops = new HashSet<>();
100 for (ClassCase _W : _S) {
101 for (ClassCase _V : _S) {
102 if (_V.equals(_W) || !(_V.isSubtypeOf(_W))) {
103 tops.add(_W);
104 }
105 }
106 }
107 cc.set_mprov(tops);
108 }
109 },
111 new Rule("P-ClassInh") {
112 boolean guard(ClassCase cc) {
113 return cc.isa(CNONE) && (cc.hasSuperclass() && cc.getSuperclass().get_HasClassMethod());
114 }
116 void eval(ClassCase cc) {
117 cc.set_mprov(cc.getSuperclass());
118 cc.set_HasClassMethod(true);
119 }
120 },
122 });
124 public static RuleGroup MARKER = new RuleGroup("Marker", new Rule[] {
125 new Rule("M-Default") {
126 boolean guard(ClassCase cc) {
127 return cc.isa(IDEFAULT);
128 }
130 void eval(ClassCase cc) {
131 cc.set_HasDefault(true);
132 }
133 },
135 new Rule("M-Conc") {
136 boolean guard(ClassCase cc) {
137 return cc.isa(CCONCRETE);
138 }
140 void eval(ClassCase cc) {
141 cc.set_IsConcrete(true);
142 }
143 },
145 });
147 public static RuleGroup RESOLUTION = new RuleGroup("Resolution", new Rule[] {
148 new Rule("R-Resolve") {
149 boolean guard(ClassCase cc) {
150 if (!(cc.isClass() && cc.get_mprov().size() == 1)) {
151 return false;
152 }
153 ClassCase _V = cc.get_mprov().iterator().next();
154 return _V.get_IsConcrete() || _V.get_HasDefault();
155 }
157 void eval(ClassCase cc) {
158 ClassCase _V = cc.get_mprov().iterator().next();
159 cc.set_mres(_V);
160 }
161 },
163 });
165 public static RuleGroup DEFENDER = new RuleGroup("Defender", new Rule[] {
166 new Rule("D-Defend") {
167 boolean guard(ClassCase cc) {
168 ClassCase mresSuper = cc.hasSuperclass() ? cc.getSuperclass().get_mres() : null;
169 boolean eq = cc.get_mres() == null ? mresSuper == null : cc.get_mres().equals(mresSuper);
170 return cc.isa(CNONE) && !eq;
171 }
173 void eval(ClassCase cc) {
174 cc.set_mdefend(cc.get_mres());
175 }
176 },
178 });
180 public static RuleGroup CHECKING = new RuleGroup("Checking", new Rule[] {
181 new Rule("C-Check") {
182 boolean guard(ClassCase cc) {
183 for (ClassCase t : cc.getSupertypes()) {
184 if (! t.get_OK()) {
185 return false;
186 }
187 }
188 int defenderCount = 0;
189 int provCount = 0;
190 for (ClassCase prov : cc.get_mprov()) {
191 if (prov.get_HasDefault()) {
192 defenderCount++;
193 }
194 provCount++;
195 }
196 return provCount <= 1 || defenderCount == 0;
197 }
199 void eval(ClassCase cc) {
200 cc.set_OK(true);
201 }
202 },
204 });
206 }