|
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 */ |
|
25 |
|
26 package shapegen; |
|
27 |
|
28 import java.util.List; |
|
29 import java.util.HashMap; |
|
30 import java.util.HashSet; |
|
31 import java.util.Set; |
|
32 |
|
33 import static shapegen.ClassCase.Kind.*; |
|
34 |
|
35 /** |
|
36 * |
|
37 * @author Robert Field |
|
38 */ |
|
39 public class Hierarchy { |
|
40 |
|
41 public final ClassCase root; |
|
42 public final Set<ClassCase> all; |
|
43 |
|
44 public Hierarchy(ClassCase root) { |
|
45 this.root = root; |
|
46 root.init(new HashMap<String,Integer>()); |
|
47 Set<ClassCase> allClasses = new HashSet<>(); |
|
48 root.collectClasses(allClasses); |
|
49 this.all = allClasses; |
|
50 } |
|
51 |
|
52 public boolean anyDefaults() { |
|
53 for (ClassCase cc : all) { |
|
54 if (cc.kind == IDEFAULT) { |
|
55 return true; |
|
56 } |
|
57 } |
|
58 return false; |
|
59 } |
|
60 |
|
61 public boolean get_OK() { |
|
62 return root.get_OK(); |
|
63 } |
|
64 |
|
65 public String testName() { |
|
66 return root + "Test"; |
|
67 } |
|
68 |
|
69 private static void genInterfaceList(StringBuilder buf, String prefix, List<ClassCase> interfaces) { |
|
70 if (!interfaces.isEmpty()) { |
|
71 buf.append(" "); |
|
72 buf.append(prefix); |
|
73 buf.append(" "); |
|
74 buf.append(interfaces.get(0)); |
|
75 for (int i = 1; i < interfaces.size(); ++i) { |
|
76 buf.append(", " + interfaces.get(i)); |
|
77 } |
|
78 } |
|
79 } |
|
80 |
|
81 public static void genClassDef(StringBuilder buf, ClassCase cc, String implClass, List<ClassCase> defaultRef) { |
|
82 if (cc.isInterface()) { |
|
83 buf.append("interface "); |
|
84 buf.append(cc.getName() + " "); |
|
85 genInterfaceList(buf, "extends", cc.getInterfaces()); |
|
86 buf.append(" {\n"); |
|
87 |
|
88 switch (cc.kind) { |
|
89 case IDEFAULT: |
|
90 buf.append(" default String m() { return \"\"; }\n"); |
|
91 defaultRef.add(cc); |
|
92 break; |
|
93 case IPRESENT: |
|
94 buf.append(" String m();\n"); |
|
95 break; |
|
96 case IVAC: |
|
97 break; |
|
98 default: |
|
99 throw new AssertionError("Unexpected kind"); |
|
100 } |
|
101 buf.append("}\n\n"); |
|
102 } else { |
|
103 buf.append((cc.isAbstract()? "abstract " : "")); |
|
104 buf.append(" class " + cc.getName()); |
|
105 if (cc.getSuperclass() != null) { |
|
106 buf.append(" extends " + cc.getSuperclass()); |
|
107 } |
|
108 |
|
109 genInterfaceList(buf, "implements", cc.getInterfaces()); |
|
110 buf.append(" {\n"); |
|
111 |
|
112 switch (cc.kind) { |
|
113 case CCONCRETE: |
|
114 buf.append(" public String m() { return \"\"; }\n"); |
|
115 break; |
|
116 case CABSTRACT: |
|
117 buf.append(" public abstract String m();\n"); |
|
118 break; |
|
119 case CNONE: |
|
120 break; |
|
121 default: |
|
122 throw new AssertionError("Unexpected kind"); |
|
123 } |
|
124 buf.append("}\n\n"); |
|
125 } |
|
126 } |
|
127 |
|
128 @Override |
|
129 public boolean equals(Object obj) { |
|
130 return obj instanceof Hierarchy && root.getID().equals(((Hierarchy)obj).root.getID()); |
|
131 } |
|
132 |
|
133 @Override |
|
134 public int hashCode() { |
|
135 return root.getID().hashCode(); |
|
136 } |
|
137 |
|
138 @Override |
|
139 public String toString() { |
|
140 return root.getName(); |
|
141 } |
|
142 |
|
143 } |