1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/lambdaShapes/org/openjdk/tests/separate/SourceModel.java Tue Nov 20 09:58:55 2012 -0800 1.3 @@ -0,0 +1,582 @@ 1.4 +/* 1.5 + * Copyright (c) 2012 Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package org.openjdk.tests.separate; 1.30 + 1.31 +import java.util.*; 1.32 +import java.io.StringWriter; 1.33 +import java.io.PrintWriter; 1.34 + 1.35 +public class SourceModel { 1.36 + 1.37 + public static final String stdMethodName = "m"; 1.38 + 1.39 + public static interface SourceProcessor { 1.40 + // Called with a generated source file 1.41 + void process(String name, String content); 1.42 + } 1.43 + 1.44 + public static abstract class Element { 1.45 + 1.46 + protected abstract void generate(PrintWriter pw); 1.47 + 1.48 + public String toString() { 1.49 + StringWriter sw = new StringWriter(); 1.50 + PrintWriter pw = new PrintWriter(sw); 1.51 + generate(pw); 1.52 + return sw.toString(); 1.53 + } 1.54 + }; 1.55 + 1.56 + public static class AccessFlag extends Element { 1.57 + private String flag; 1.58 + 1.59 + public AccessFlag(String name) { flag = name; } 1.60 + 1.61 + protected void generate(PrintWriter pw) { 1.62 + pw.print(flag); 1.63 + } 1.64 + 1.65 + public String toString() { return flag; } 1.66 + 1.67 + public static final AccessFlag PUBLIC = new AccessFlag("public"); 1.68 + public static final AccessFlag PRIVATE = new AccessFlag("private"); 1.69 + public static final AccessFlag PROTECTED = new AccessFlag("protected"); 1.70 + public static final AccessFlag STATIC = new AccessFlag("static"); 1.71 + public static final AccessFlag FINAL = new AccessFlag("final"); 1.72 + public static final AccessFlag SYNCHRONIZED = new AccessFlag("synchronized"); 1.73 + public static final AccessFlag VOLATILE = new AccessFlag("volatile"); 1.74 + public static final AccessFlag NATIVE = new AccessFlag("native"); 1.75 + public static final AccessFlag ABSTRACT = new AccessFlag("abstract"); 1.76 + public static final AccessFlag STRICTFP = new AccessFlag("strictfp"); 1.77 + public static final AccessFlag DEFAULT = new AccessFlag("default"); 1.78 + } 1.79 + 1.80 + public static class TypeParameter extends Element { 1.81 + private String parameter; 1.82 + 1.83 + public TypeParameter(String str) { 1.84 + this.parameter = str; 1.85 + } 1.86 + 1.87 + protected void generate(PrintWriter pw) { 1.88 + pw.print(parameter); 1.89 + } 1.90 + } 1.91 + 1.92 + public static class TypeArgument extends Element { 1.93 + private String argument; 1.94 + 1.95 + public TypeArgument(String str) { 1.96 + this.argument = str; 1.97 + } 1.98 + 1.99 + protected void generate(PrintWriter pw) { 1.100 + pw.print(argument); 1.101 + } 1.102 + } 1.103 + 1.104 + public static class MethodParameter extends Element { 1.105 + private String type; 1.106 + private String name; 1.107 + 1.108 + public MethodParameter(String type, String name) { 1.109 + this.type = type; 1.110 + this.name = name; 1.111 + } 1.112 + 1.113 + protected void generate(PrintWriter pw) { 1.114 + pw.printf("%s %s", this.type, this.name); 1.115 + } 1.116 + 1.117 + public String toString() { return type + " " + name; } 1.118 + } 1.119 + 1.120 + public static abstract class Type extends Element { 1.121 + private String name; 1.122 + private List<AccessFlag> accessFlags; 1.123 + private List<TypeParameter> parameters; 1.124 + private List<Extends> supertypes; 1.125 + private List<Method> methods; 1.126 + 1.127 + // methods from superclasses that are required for compilation 1.128 + // (and thus will be present in stubs) 1.129 + private Set<Method> methodDependencies; 1.130 + private List<Type> typeDependencies; 1.131 + 1.132 + protected Type(String name, 1.133 + List<AccessFlag> flags, List<TypeParameter> params, 1.134 + List<Extends> ifaces, List<Method> methods) { 1.135 + this.name = name; 1.136 + this.accessFlags = flags == null ? new ArrayList<>() : flags; 1.137 + this.parameters = params == null ? new ArrayList<>() : params; 1.138 + this.supertypes = ifaces == null ? new ArrayList<>() : ifaces; 1.139 + this.methods = methods == null ? new ArrayList<>() : methods; 1.140 + this.methodDependencies = new HashSet<>(); 1.141 + this.typeDependencies = new ArrayList<>(); 1.142 + } 1.143 + 1.144 + public String getName() { return this.name; } 1.145 + public List<AccessFlag> getAccessFlags() { return this.accessFlags; } 1.146 + public List<TypeParameter> getParameters() { return this.parameters; } 1.147 + public List<Extends> getSupertypes() { return this.supertypes; } 1.148 + public List<Method> getMethods() { return this.methods; } 1.149 + public Set<Method> methodDependencies() { 1.150 + return this.methodDependencies; 1.151 + } 1.152 + 1.153 + public Class getSuperclass() { return null; } 1.154 + protected abstract void setSuperClass(Extends supertype); 1.155 + 1.156 + public void addSuperType(Extends sup) { 1.157 + assert sup.getType() instanceof Interface : "Must be an interface"; 1.158 + this.supertypes.add(sup); 1.159 + } 1.160 + public void addSuperType(Interface iface) { 1.161 + this.supertypes.add(new Extends(iface)); 1.162 + } 1.163 + 1.164 + public void addMethod(Method m) { 1.165 + this.methods.add(m); 1.166 + } 1.167 + 1.168 + public void addAccessFlag(AccessFlag f) { 1.169 + this.accessFlags.add(f); 1.170 + } 1.171 + 1.172 + // Convenience method for creation. Parameters are interpreted 1.173 + // according to their type. Class (or Extends with a Class type) is 1.174 + // considered a superclass (only one allowed). TypeParameters are 1.175 + // generic parameter names. Interface (or Extends with an Interface 1.176 + // type) is an implemented supertype. Methods are methods (duh!). 1.177 + protected void addComponent(Element p) { 1.178 + if (p instanceof Class) { 1.179 + setSuperClass(new Extends((Class)p)); 1.180 + } else if (p instanceof Extends) { 1.181 + Extends ext = (Extends)p; 1.182 + if (ext.supertype instanceof Class) { 1.183 + setSuperClass(ext); 1.184 + } else if (ext.supertype instanceof Interface) { 1.185 + addSuperType(ext); 1.186 + } else { 1.187 + assert false : "What is this thing?"; 1.188 + } 1.189 + } else if (p instanceof Interface) { 1.190 + addSuperType((Interface)p); 1.191 + } else if (p instanceof TypeParameter) { 1.192 + this.parameters.add((TypeParameter)p); 1.193 + } else if (p instanceof Method) { 1.194 + addMethod((Method)p); 1.195 + } else if (p instanceof AccessFlag) { 1.196 + addAccessFlag((AccessFlag)p); 1.197 + } else { 1.198 + assert false : "What is this thing?"; 1.199 + } 1.200 + } 1.201 + 1.202 + // Find and return the first method that has name 'name' 1.203 + public Method findMethod(String name) { 1.204 + for (Method m : methods) { 1.205 + if (m.name.equals(name)) { 1.206 + return m; 1.207 + } 1.208 + } 1.209 + return null; 1.210 + } 1.211 + 1.212 + public void addCompilationDependency(Type t) { 1.213 + typeDependencies.add(t); 1.214 + } 1.215 + 1.216 + public void addCompilationDependency(Method m) { 1.217 + methodDependencies.add(m); 1.218 + } 1.219 + 1.220 + // Convenience method for creating an Extends object using this 1.221 + // class and specified type arguments. 1.222 + public Extends with(String ... args) { 1.223 + return new Extends(this, args); 1.224 + } 1.225 + 1.226 + public abstract void generate(SourceProcessor sp); 1.227 + public abstract void generateAsDependency( 1.228 + SourceProcessor sp, Set<Method> neededMethods); 1.229 + 1.230 + protected void generateName(PrintWriter pw) { 1.231 + pw.print(this.name); 1.232 + toJoinedString(this.parameters, ",", "<", ">", ""); 1.233 + pw.print(toJoinedString(this.parameters, ",", "<", ">", "")); 1.234 + pw.print(" "); 1.235 + } 1.236 + 1.237 + protected void generateBody(PrintWriter pw, String superSpec) { 1.238 + pw.print(toJoinedString(this.supertypes, ",", superSpec + " ", " ", "")); 1.239 + pw.println("{ "); 1.240 + pw.print(toJoinedString(this.methods, "\n ", "\n ", "\n", "")); 1.241 + pw.println("}"); 1.242 + } 1.243 + 1.244 + protected void generateAccessFlags(PrintWriter pw) { 1.245 + pw.print(toJoinedString(this.accessFlags, " ", "", " ")); 1.246 + } 1.247 + 1.248 + protected void generateBodyAsDependency( 1.249 + PrintWriter pw, Set<Method> neededMethods) { 1.250 + pw.println(" {"); 1.251 + for (Method m : this.methods) { 1.252 + if (neededMethods.contains(m)) { 1.253 + pw.print(" "); 1.254 + m.generate(pw); 1.255 + pw.println(); 1.256 + } 1.257 + } 1.258 + pw.println("}"); 1.259 + } 1.260 + 1.261 + public Collection<Type> typeDependencies() { 1.262 + HashMap<String,Type> dependencies = new HashMap<>(); 1.263 + Type superclass = getSuperclass(); 1.264 + if (superclass != null) { 1.265 + dependencies.put(superclass.getName(), superclass); 1.266 + } 1.267 + for (Extends e : getSupertypes()) 1.268 + dependencies.put(e.getType().getName(), e.getType()); 1.269 + // Do these last so that they override 1.270 + for (Type t : this.typeDependencies) 1.271 + dependencies.put(t.getName(), t); 1.272 + return dependencies.values(); 1.273 + } 1.274 + } 1.275 + 1.276 + public static class Class extends Type { 1.277 + private Extends superClass; 1.278 + 1.279 + public Class(String name, List<AccessFlag> flags, 1.280 + List<TypeParameter> params, Extends sprClass, 1.281 + List<Extends> interfaces, List<Method> methods) { 1.282 + super(name, flags, params, interfaces, methods); 1.283 + this.superClass = sprClass; 1.284 + addAccessFlag(AccessFlag.PUBLIC); // should remove this 1.285 + } 1.286 + 1.287 + public Class(String name, Element ... components) { 1.288 + super(name, null, null, null, null); 1.289 + this.superClass = null; 1.290 + 1.291 + for (Element p : components) { 1.292 + addComponent(p); 1.293 + } 1.294 + addAccessFlag(AccessFlag.PUBLIC); // should remove this 1.295 + } 1.296 + 1.297 + public boolean isAbstract() { 1.298 + for (AccessFlag flag : getAccessFlags()) { 1.299 + if (flag == AccessFlag.ABSTRACT) { 1.300 + return true; 1.301 + } 1.302 + } 1.303 + return false; 1.304 + } 1.305 + 1.306 + @Override 1.307 + public void setSuperClass(Extends ext) { 1.308 + assert this.superClass == null : "Multiple superclasses defined"; 1.309 + assert ext.getType() instanceof Class : "Must be a class"; 1.310 + this.superClass = ext; 1.311 + } 1.312 + 1.313 + public void setSuperClass(Class c) { 1.314 + setSuperClass(new Extends(c)); 1.315 + } 1.316 + 1.317 + @Override 1.318 + public Class getSuperclass() { 1.319 + return superClass == null ? null : (Class)superClass.supertype; 1.320 + } 1.321 + 1.322 + public void generate(SourceProcessor processor) { 1.323 + StringWriter sw = new StringWriter(); 1.324 + PrintWriter pw = new PrintWriter(sw); 1.325 + generate(pw); 1.326 + processor.process(getName(), sw.toString()); 1.327 + } 1.328 + 1.329 + public void generate(PrintWriter pw) { 1.330 + generateAccessFlags(pw); 1.331 + pw.print("class "); 1.332 + generateName(pw); 1.333 + if (superClass != null) { 1.334 + pw.print("extends "); 1.335 + superClass.generate(pw); 1.336 + pw.print(" "); 1.337 + } 1.338 + generateBody(pw, "implements"); 1.339 + } 1.340 + 1.341 + public void generateAsDependency( 1.342 + SourceProcessor processor, Set<Method> neededMethods) { 1.343 + StringWriter sw = new StringWriter(); 1.344 + PrintWriter pw = new PrintWriter(sw); 1.345 + generateAccessFlags(pw); 1.346 + pw.print("class "); 1.347 + generateName(pw); 1.348 + pw.print(" "); 1.349 + generateBodyAsDependency(pw, neededMethods); 1.350 + 1.351 + processor.process(getName(), sw.toString()); 1.352 + } 1.353 + } 1.354 + 1.355 + public static class Interface extends Type { 1.356 + 1.357 + public Interface(String name, 1.358 + List<AccessFlag> flags, List<TypeParameter> params, 1.359 + List<Extends> interfaces, List<Method> methods) { 1.360 + super(name, flags, params, interfaces, methods); 1.361 + } 1.362 + 1.363 + public Interface(String name, Element ... components) { 1.364 + super(name, null, null, null, null); 1.365 + for (Element c : components) { 1.366 + addComponent(c); 1.367 + } 1.368 + } 1.369 + 1.370 + protected void setSuperClass(Extends ext) { 1.371 + assert false : "Interfaces cannot have Class supertypes"; 1.372 + } 1.373 + 1.374 + public void generate(SourceProcessor processor) { 1.375 + StringWriter sw = new StringWriter(); 1.376 + PrintWriter pw = new PrintWriter(sw); 1.377 + generate(pw); 1.378 + processor.process(getName(), sw.toString()); 1.379 + } 1.380 + 1.381 + public void generate(PrintWriter pw) { 1.382 + generateAccessFlags(pw); 1.383 + pw.print("interface "); 1.384 + generateName(pw); 1.385 + pw.print(" "); 1.386 + generateBody(pw, "extends"); 1.387 + } 1.388 + 1.389 + public void generateAsDependency( 1.390 + SourceProcessor processor, Set<Method> neededMethods) { 1.391 + StringWriter sw = new StringWriter(); 1.392 + PrintWriter pw = new PrintWriter(sw); 1.393 + 1.394 + generateAccessFlags(pw); 1.395 + pw.print("interface "); 1.396 + generateName(pw); 1.397 + pw.print(" "); 1.398 + generateBodyAsDependency(pw, neededMethods); 1.399 + 1.400 + processor.process(getName(), sw.toString()); 1.401 + } 1.402 + } 1.403 + 1.404 + /** 1.405 + * Represents a type extension that might contain type arguments 1.406 + */ 1.407 + public static class Extends extends Element { 1.408 + private final Type supertype; 1.409 + private final List<TypeArgument> arguments; 1.410 + 1.411 + public Type getType() { return supertype; } 1.412 + public List<TypeArgument> getArguments() { 1.413 + return arguments; 1.414 + } 1.415 + 1.416 + public Extends(Type supertype, String ... args) { 1.417 + assert supertype != null : "Null supertype"; 1.418 + this.supertype = supertype; 1.419 + this.arguments = new ArrayList<>(); 1.420 + for (String arg : args) { 1.421 + this.arguments.add(new TypeArgument(arg)); 1.422 + } 1.423 + } 1.424 + 1.425 + public void generate(PrintWriter pw) { 1.426 + pw.print(supertype.getName()); 1.427 + pw.print(toJoinedString(getArguments(), ",", "<", ">", "")); 1.428 + } 1.429 + } 1.430 + 1.431 + public static abstract class Method extends Element { 1.432 + private String name; 1.433 + private String returnType; 1.434 + private List<AccessFlag> accessFlags; 1.435 + private List<MethodParameter> parameters; 1.436 + private boolean emitSuppressWarnings; 1.437 + 1.438 + protected Method(String ret, String name, Element ... params) { 1.439 + this.name = name; 1.440 + this.returnType = ret; 1.441 + this.accessFlags = new ArrayList<>(); 1.442 + this.parameters = new ArrayList<>(); 1.443 + this.emitSuppressWarnings = false; 1.444 + 1.445 + for (Element e : params) { 1.446 + if (e instanceof MethodParameter) { 1.447 + this.parameters.add((MethodParameter) e); 1.448 + } else if (e instanceof AccessFlag) { 1.449 + this.accessFlags.add((AccessFlag) e); 1.450 + } 1.451 + } 1.452 + assert accessFlags.size() + parameters.size() == params.length : 1.453 + "Non method parameters or access flags in constructor"; 1.454 + } 1.455 + 1.456 + public String getName() { return this.name; } 1.457 + public String getReturnType() { return this.returnType; } 1.458 + public List<MethodParameter> getParameters() { 1.459 + return this.parameters; 1.460 + } 1.461 + public List<AccessFlag> getAccessFlags() { 1.462 + return this.accessFlags; 1.463 + } 1.464 + public Element[] getElements() { 1.465 + ArrayList<Element> elements = new ArrayList<>(); 1.466 + elements.addAll(getParameters()); 1.467 + elements.addAll(getAccessFlags()); 1.468 + return elements.toArray(new Element[0]); 1.469 + } 1.470 + 1.471 + public void suppressWarnings() { this.emitSuppressWarnings = true; } 1.472 + 1.473 + public void generateWarningSuppression(PrintWriter pw) { 1.474 + if (this.emitSuppressWarnings) { 1.475 + pw.printf("@SuppressWarnings(\"unchecked\")\n "); 1.476 + } 1.477 + } 1.478 + 1.479 + protected void generateDecl(PrintWriter pw) { 1.480 + generateWarningSuppression(pw); 1.481 + pw.print(toJoinedString(this.accessFlags, " ", "", " ")); 1.482 + pw.printf("%s %s(", returnType, name); 1.483 + pw.print(toJoinedString(parameters, ",")); 1.484 + pw.print(")"); 1.485 + } 1.486 + } 1.487 + 1.488 + public static class AbstractMethod extends Method { 1.489 + public AbstractMethod( 1.490 + String ret, String name, Element ... params) { 1.491 + super(ret, name, params); 1.492 + this.getAccessFlags().add(AccessFlag.ABSTRACT); 1.493 + } 1.494 + 1.495 + public void generate(PrintWriter pw) { 1.496 + generateDecl(pw); 1.497 + pw.print(";"); 1.498 + } 1.499 + 1.500 + public static AbstractMethod std() { 1.501 + return new AbstractMethod( 1.502 + "int", SourceModel.stdMethodName, AccessFlag.PUBLIC); 1.503 + } 1.504 + } 1.505 + 1.506 + public static class ConcreteMethod extends Method { 1.507 + protected String body; 1.508 + 1.509 + public ConcreteMethod(String ret, String name, 1.510 + String body, Element ... params) { 1.511 + super(ret, name, params); 1.512 + this.body = body; 1.513 + } 1.514 + 1.515 + public void generate(PrintWriter pw) { 1.516 + generateDecl(pw); 1.517 + pw.printf(" { %s }", this.body); 1.518 + } 1.519 + 1.520 + public static ConcreteMethod std(String value) { 1.521 + return new ConcreteMethod( 1.522 + "int", SourceModel.stdMethodName, "return " + value + ";", 1.523 + AccessFlag.PUBLIC); 1.524 + } 1.525 + } 1.526 + 1.527 + // When the default method flag gets moved into the traditional 1.528 + // access flags location, we can remove this class completely and 1.529 + // use a ConcreteMethod with an AccessFlag("default") in the constructor 1.530 + public static class DefaultMethod extends Method { 1.531 + protected String body; 1.532 + 1.533 + public DefaultMethod(String ret, String name, String body, 1.534 + Element ... params) { 1.535 + super(ret, name, params); 1.536 + this.body = body; 1.537 + this.getAccessFlags().add(AccessFlag.DEFAULT); 1.538 + } 1.539 + 1.540 + public void generate(PrintWriter pw) { 1.541 + generateDecl(pw); 1.542 + pw.printf(" { %s }", this.body); 1.543 + } 1.544 + 1.545 + public static DefaultMethod std(String value) { 1.546 + return new DefaultMethod( 1.547 + "int", SourceModel.stdMethodName, "return " + value + ";"); 1.548 + } 1.549 + } 1.550 + 1.551 + private static <T> String toJoinedString(List<T> list, String... p) { 1.552 + StringBuilder sb = new StringBuilder(); 1.553 + String sep = ""; 1.554 + String init = ""; 1.555 + String end = ""; 1.556 + String empty = null; 1.557 + switch (p.length) { 1.558 + case 4: 1.559 + empty = p[3]; 1.560 + /*fall-through*/ 1.561 + case 3: 1.562 + end = p[2]; 1.563 + /*fall-through*/ 1.564 + case 2: 1.565 + init = p[1]; 1.566 + /*fall-through*/ 1.567 + case 1: 1.568 + sep = p[0]; 1.569 + break; 1.570 + } 1.571 + if (empty != null && list.isEmpty()) { 1.572 + return empty; 1.573 + } else { 1.574 + sb.append(init); 1.575 + for (T x : list) { 1.576 + if (sb.length() != init.length()) { 1.577 + sb.append(sep); 1.578 + } 1.579 + sb.append(x.toString()); 1.580 + } 1.581 + sb.append(end); 1.582 + return sb.toString(); 1.583 + } 1.584 + } 1.585 +}