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 Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,600 @@ 1.4 +/* 1.5 + * Copyright (c) 2012, 2013, 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 + private boolean fullCompilation; 1.132 + 1.133 + protected Type(String name, 1.134 + List<AccessFlag> flags, List<TypeParameter> params, 1.135 + List<Extends> ifaces, List<Method> methods) { 1.136 + this.name = name; 1.137 + this.accessFlags = flags == null ? new ArrayList<>() : flags; 1.138 + this.parameters = params == null ? new ArrayList<>() : params; 1.139 + this.supertypes = ifaces == null ? new ArrayList<>() : ifaces; 1.140 + this.methods = methods == null ? new ArrayList<>() : methods; 1.141 + this.methodDependencies = new HashSet<>(); 1.142 + this.typeDependencies = new ArrayList<>(); 1.143 + } 1.144 + 1.145 + public String getName() { return this.name; } 1.146 + public List<AccessFlag> getAccessFlags() { return this.accessFlags; } 1.147 + public List<TypeParameter> getParameters() { return this.parameters; } 1.148 + public List<Extends> getSupertypes() { return this.supertypes; } 1.149 + public List<Method> getMethods() { return this.methods; } 1.150 + public Set<Method> methodDependencies() { 1.151 + return this.methodDependencies; 1.152 + } 1.153 + 1.154 + public Class getSuperclass() { return null; } 1.155 + protected abstract void setSuperClass(Extends supertype); 1.156 + 1.157 + public void addSuperType(Extends sup) { 1.158 + assert sup.getType() instanceof Interface : "Must be an interface"; 1.159 + this.supertypes.add(sup); 1.160 + } 1.161 + public void addSuperType(Interface iface) { 1.162 + this.supertypes.add(new Extends(iface)); 1.163 + } 1.164 + 1.165 + public void addMethod(Method m) { 1.166 + this.methods.add(m); 1.167 + } 1.168 + 1.169 + public void addAccessFlag(AccessFlag f) { 1.170 + this.accessFlags.add(f); 1.171 + } 1.172 + 1.173 + // Convenience method for creation. Parameters are interpreted 1.174 + // according to their type. Class (or Extends with a Class type) is 1.175 + // considered a superclass (only one allowed). TypeParameters are 1.176 + // generic parameter names. Interface (or Extends with an Interface 1.177 + // type) is an implemented supertype. Methods are methods (duh!). 1.178 + protected void addComponent(Element p) { 1.179 + if (p instanceof Class) { 1.180 + setSuperClass(new Extends((Class)p)); 1.181 + } else if (p instanceof Extends) { 1.182 + Extends ext = (Extends)p; 1.183 + if (ext.supertype instanceof Class) { 1.184 + setSuperClass(ext); 1.185 + } else if (ext.supertype instanceof Interface) { 1.186 + addSuperType(ext); 1.187 + } else { 1.188 + assert false : "What is this thing?"; 1.189 + } 1.190 + } else if (p instanceof Interface) { 1.191 + addSuperType((Interface)p); 1.192 + } else if (p instanceof TypeParameter) { 1.193 + this.parameters.add((TypeParameter)p); 1.194 + } else if (p instanceof Method) { 1.195 + addMethod((Method)p); 1.196 + } else if (p instanceof AccessFlag) { 1.197 + addAccessFlag((AccessFlag)p); 1.198 + } else { 1.199 + assert false : "What is this thing?"; 1.200 + } 1.201 + } 1.202 + 1.203 + // Find and return the first method that has name 'name' 1.204 + public Method findMethod(String name) { 1.205 + for (Method m : methods) { 1.206 + if (m.name.equals(name)) { 1.207 + return m; 1.208 + } 1.209 + } 1.210 + return null; 1.211 + } 1.212 + 1.213 + public void addCompilationDependency(Type t) { 1.214 + typeDependencies.add(t); 1.215 + } 1.216 + 1.217 + public void addCompilationDependency(Method m) { 1.218 + methodDependencies.add(m); 1.219 + } 1.220 + 1.221 + public boolean isFullCompilation() { 1.222 + return fullCompilation; 1.223 + } 1.224 + 1.225 + public void setFullCompilation(boolean fullCompilation) { 1.226 + this.fullCompilation = fullCompilation; 1.227 + } 1.228 + 1.229 + // Convenience method for creating an Extends object using this 1.230 + // class and specified type arguments. 1.231 + public Extends with(String ... args) { 1.232 + return new Extends(this, args); 1.233 + } 1.234 + 1.235 + public abstract void generate(SourceProcessor sp); 1.236 + public abstract void generateAsDependency( 1.237 + SourceProcessor sp, Set<Method> neededMethods); 1.238 + 1.239 + protected void generateName(PrintWriter pw) { 1.240 + pw.print(this.name); 1.241 + toJoinedString(this.parameters, ",", "<", ">", ""); 1.242 + pw.print(toJoinedString(this.parameters, ",", "<", ">", "")); 1.243 + pw.print(" "); 1.244 + } 1.245 + 1.246 + protected void generateBody(PrintWriter pw, String superSpec) { 1.247 + pw.print(toJoinedString(this.supertypes, ",", superSpec + " ", " ", "")); 1.248 + pw.println("{ "); 1.249 + pw.print(toJoinedString(this.methods, "\n ", "\n ", "\n", "")); 1.250 + pw.println("}"); 1.251 + } 1.252 + 1.253 + protected void generateAccessFlags(PrintWriter pw) { 1.254 + pw.print(toJoinedString(this.accessFlags, " ", "", " ")); 1.255 + } 1.256 + 1.257 + protected void generateBodyAsDependency( 1.258 + PrintWriter pw, Set<Method> neededMethods) { 1.259 + pw.println(" {"); 1.260 + for (Method m : this.methods) { 1.261 + if (neededMethods.contains(m)) { 1.262 + pw.print(" "); 1.263 + m.generate(pw); 1.264 + pw.println(); 1.265 + } 1.266 + } 1.267 + pw.println("}"); 1.268 + } 1.269 + 1.270 + public Collection<Type> typeDependencies(boolean recursive) { 1.271 + HashMap<String,Type> dependencies = new HashMap<>(); 1.272 + Type superclass = getSuperclass(); 1.273 + if (superclass != null) { 1.274 + dependencies.put(superclass.getName(), superclass); 1.275 + if (recursive) { 1.276 + for (Type t : superclass.typeDependencies(true)) 1.277 + dependencies.put(t.getName(), t); 1.278 + } 1.279 + } 1.280 + for (Extends e : getSupertypes()) { 1.281 + dependencies.put(e.getType().getName(), e.getType()); 1.282 + if (recursive) { 1.283 + for (Type t : e.getType().typeDependencies(true)) 1.284 + dependencies.put(t.getName(), t); 1.285 + } 1.286 + } 1.287 + // Do these last so that they override 1.288 + for (Type t : this.typeDependencies) 1.289 + dependencies.put(t.getName(), t); 1.290 + return dependencies.values(); 1.291 + } 1.292 + } 1.293 + 1.294 + public static class Class extends Type { 1.295 + private Extends superClass; 1.296 + 1.297 + public Class(String name, List<AccessFlag> flags, 1.298 + List<TypeParameter> params, Extends sprClass, 1.299 + List<Extends> interfaces, List<Method> methods) { 1.300 + super(name, flags, params, interfaces, methods); 1.301 + this.superClass = sprClass; 1.302 + addAccessFlag(AccessFlag.PUBLIC); // should remove this 1.303 + } 1.304 + 1.305 + public Class(String name, Element ... components) { 1.306 + super(name, null, null, null, null); 1.307 + this.superClass = null; 1.308 + 1.309 + for (Element p : components) { 1.310 + addComponent(p); 1.311 + } 1.312 + addAccessFlag(AccessFlag.PUBLIC); // should remove this 1.313 + } 1.314 + 1.315 + public boolean isAbstract() { 1.316 + for (AccessFlag flag : getAccessFlags()) { 1.317 + if (flag == AccessFlag.ABSTRACT) { 1.318 + return true; 1.319 + } 1.320 + } 1.321 + return false; 1.322 + } 1.323 + 1.324 + @Override 1.325 + public void setSuperClass(Extends ext) { 1.326 + assert this.superClass == null : "Multiple superclasses defined"; 1.327 + assert ext.getType() instanceof Class : "Must be a class"; 1.328 + this.superClass = ext; 1.329 + } 1.330 + 1.331 + public void setSuperClass(Class c) { 1.332 + setSuperClass(new Extends(c)); 1.333 + } 1.334 + 1.335 + @Override 1.336 + public Class getSuperclass() { 1.337 + return superClass == null ? null : (Class)superClass.supertype; 1.338 + } 1.339 + 1.340 + public void generate(SourceProcessor processor) { 1.341 + StringWriter sw = new StringWriter(); 1.342 + PrintWriter pw = new PrintWriter(sw); 1.343 + generate(pw); 1.344 + processor.process(getName(), sw.toString()); 1.345 + } 1.346 + 1.347 + public void generate(PrintWriter pw) { 1.348 + generateAccessFlags(pw); 1.349 + pw.print("class "); 1.350 + generateName(pw); 1.351 + if (superClass != null) { 1.352 + pw.print("extends "); 1.353 + superClass.generate(pw); 1.354 + pw.print(" "); 1.355 + } 1.356 + generateBody(pw, "implements"); 1.357 + } 1.358 + 1.359 + public void generateAsDependency( 1.360 + SourceProcessor processor, Set<Method> neededMethods) { 1.361 + StringWriter sw = new StringWriter(); 1.362 + PrintWriter pw = new PrintWriter(sw); 1.363 + generateAccessFlags(pw); 1.364 + pw.print("class "); 1.365 + generateName(pw); 1.366 + pw.print(" "); 1.367 + generateBodyAsDependency(pw, neededMethods); 1.368 + 1.369 + processor.process(getName(), sw.toString()); 1.370 + } 1.371 + } 1.372 + 1.373 + public static class Interface extends Type { 1.374 + 1.375 + public Interface(String name, 1.376 + List<AccessFlag> flags, List<TypeParameter> params, 1.377 + List<Extends> interfaces, List<Method> methods) { 1.378 + super(name, flags, params, interfaces, methods); 1.379 + } 1.380 + 1.381 + public Interface(String name, Element ... components) { 1.382 + super(name, null, null, null, null); 1.383 + for (Element c : components) { 1.384 + addComponent(c); 1.385 + } 1.386 + } 1.387 + 1.388 + protected void setSuperClass(Extends ext) { 1.389 + assert false : "Interfaces cannot have Class supertypes"; 1.390 + } 1.391 + 1.392 + public void generate(SourceProcessor processor) { 1.393 + StringWriter sw = new StringWriter(); 1.394 + PrintWriter pw = new PrintWriter(sw); 1.395 + generate(pw); 1.396 + processor.process(getName(), sw.toString()); 1.397 + } 1.398 + 1.399 + public void generate(PrintWriter pw) { 1.400 + generateAccessFlags(pw); 1.401 + pw.print("interface "); 1.402 + generateName(pw); 1.403 + pw.print(" "); 1.404 + generateBody(pw, "extends"); 1.405 + } 1.406 + 1.407 + public void generateAsDependency( 1.408 + SourceProcessor processor, Set<Method> neededMethods) { 1.409 + StringWriter sw = new StringWriter(); 1.410 + PrintWriter pw = new PrintWriter(sw); 1.411 + 1.412 + generateAccessFlags(pw); 1.413 + pw.print("interface "); 1.414 + generateName(pw); 1.415 + pw.print(" "); 1.416 + generateBodyAsDependency(pw, neededMethods); 1.417 + 1.418 + processor.process(getName(), sw.toString()); 1.419 + } 1.420 + } 1.421 + 1.422 + /** 1.423 + * Represents a type extension that might contain type arguments 1.424 + */ 1.425 + public static class Extends extends Element { 1.426 + private final Type supertype; 1.427 + private final List<TypeArgument> arguments; 1.428 + 1.429 + public Type getType() { return supertype; } 1.430 + public List<TypeArgument> getArguments() { 1.431 + return arguments; 1.432 + } 1.433 + 1.434 + public Extends(Type supertype, String ... args) { 1.435 + assert supertype != null : "Null supertype"; 1.436 + this.supertype = supertype; 1.437 + this.arguments = new ArrayList<>(); 1.438 + for (String arg : args) { 1.439 + this.arguments.add(new TypeArgument(arg)); 1.440 + } 1.441 + } 1.442 + 1.443 + public void generate(PrintWriter pw) { 1.444 + pw.print(supertype.getName()); 1.445 + pw.print(toJoinedString(getArguments(), ",", "<", ">", "")); 1.446 + } 1.447 + } 1.448 + 1.449 + public static abstract class Method extends Element { 1.450 + private String name; 1.451 + private String returnType; 1.452 + private List<AccessFlag> accessFlags; 1.453 + private List<MethodParameter> parameters; 1.454 + private boolean emitSuppressWarnings; 1.455 + 1.456 + protected Method(String ret, String name, Element ... params) { 1.457 + this.name = name; 1.458 + this.returnType = ret; 1.459 + this.accessFlags = new ArrayList<>(); 1.460 + this.parameters = new ArrayList<>(); 1.461 + this.emitSuppressWarnings = false; 1.462 + 1.463 + for (Element e : params) { 1.464 + if (e instanceof MethodParameter) { 1.465 + this.parameters.add((MethodParameter) e); 1.466 + } else if (e instanceof AccessFlag) { 1.467 + this.accessFlags.add((AccessFlag) e); 1.468 + } 1.469 + } 1.470 + assert accessFlags.size() + parameters.size() == params.length : 1.471 + "Non method parameters or access flags in constructor"; 1.472 + } 1.473 + 1.474 + public String getName() { return this.name; } 1.475 + public String getReturnType() { return this.returnType; } 1.476 + public List<MethodParameter> getParameters() { 1.477 + return this.parameters; 1.478 + } 1.479 + public List<AccessFlag> getAccessFlags() { 1.480 + return this.accessFlags; 1.481 + } 1.482 + public Element[] getElements() { 1.483 + ArrayList<Element> elements = new ArrayList<>(); 1.484 + elements.addAll(getParameters()); 1.485 + elements.addAll(getAccessFlags()); 1.486 + return elements.toArray(new Element[0]); 1.487 + } 1.488 + 1.489 + public void suppressWarnings() { this.emitSuppressWarnings = true; } 1.490 + 1.491 + public void generateWarningSuppression(PrintWriter pw) { 1.492 + if (this.emitSuppressWarnings) { 1.493 + pw.printf("@SuppressWarnings(\"unchecked\")\n "); 1.494 + } 1.495 + } 1.496 + 1.497 + protected void generateDecl(PrintWriter pw) { 1.498 + generateWarningSuppression(pw); 1.499 + pw.print(toJoinedString(this.accessFlags, " ", "", " ")); 1.500 + pw.printf("%s %s(", returnType, name); 1.501 + pw.print(toJoinedString(parameters, ",")); 1.502 + pw.print(")"); 1.503 + } 1.504 + } 1.505 + 1.506 + public static class AbstractMethod extends Method { 1.507 + public AbstractMethod( 1.508 + String ret, String name, Element ... params) { 1.509 + super(ret, name, params); 1.510 + this.getAccessFlags().add(AccessFlag.ABSTRACT); 1.511 + } 1.512 + 1.513 + public void generate(PrintWriter pw) { 1.514 + generateDecl(pw); 1.515 + pw.print(";"); 1.516 + } 1.517 + 1.518 + public static AbstractMethod std() { 1.519 + return new AbstractMethod( 1.520 + "int", SourceModel.stdMethodName, AccessFlag.PUBLIC); 1.521 + } 1.522 + } 1.523 + 1.524 + public static class ConcreteMethod extends Method { 1.525 + protected String body; 1.526 + 1.527 + public ConcreteMethod(String ret, String name, 1.528 + String body, Element ... params) { 1.529 + super(ret, name, params); 1.530 + this.body = body; 1.531 + } 1.532 + 1.533 + public void generate(PrintWriter pw) { 1.534 + generateDecl(pw); 1.535 + pw.printf(" { %s }", this.body); 1.536 + } 1.537 + 1.538 + public static ConcreteMethod std(String value) { 1.539 + return new ConcreteMethod( 1.540 + "int", SourceModel.stdMethodName, "return " + value + ";", 1.541 + AccessFlag.PUBLIC); 1.542 + } 1.543 + } 1.544 + 1.545 + // When the default method flag gets moved into the traditional 1.546 + // access flags location, we can remove this class completely and 1.547 + // use a ConcreteMethod with an AccessFlag("default") in the constructor 1.548 + public static class DefaultMethod extends Method { 1.549 + protected String body; 1.550 + 1.551 + public DefaultMethod(String ret, String name, String body, 1.552 + Element ... params) { 1.553 + super(ret, name, params); 1.554 + this.body = body; 1.555 + this.getAccessFlags().add(AccessFlag.DEFAULT); 1.556 + } 1.557 + 1.558 + public void generate(PrintWriter pw) { 1.559 + generateDecl(pw); 1.560 + pw.printf(" { %s }", this.body); 1.561 + } 1.562 + 1.563 + public static DefaultMethod std(String value) { 1.564 + return new DefaultMethod( 1.565 + "int", SourceModel.stdMethodName, "return " + value + ";"); 1.566 + } 1.567 + } 1.568 + 1.569 + private static <T> String toJoinedString(List<T> list, String... p) { 1.570 + StringBuilder sb = new StringBuilder(); 1.571 + String sep = ""; 1.572 + String init = ""; 1.573 + String end = ""; 1.574 + String empty = null; 1.575 + switch (p.length) { 1.576 + case 4: 1.577 + empty = p[3]; 1.578 + /*fall-through*/ 1.579 + case 3: 1.580 + end = p[2]; 1.581 + /*fall-through*/ 1.582 + case 2: 1.583 + init = p[1]; 1.584 + /*fall-through*/ 1.585 + case 1: 1.586 + sep = p[0]; 1.587 + break; 1.588 + } 1.589 + if (empty != null && list.isEmpty()) { 1.590 + return empty; 1.591 + } else { 1.592 + sb.append(init); 1.593 + for (T x : list) { 1.594 + if (sb.length() != init.length()) { 1.595 + sb.append(sep); 1.596 + } 1.597 + sb.append(x.toString()); 1.598 + } 1.599 + sb.append(end); 1.600 + return sb.toString(); 1.601 + } 1.602 + } 1.603 +}