Mon, 13 Dec 2010 14:08:01 -0800
Merge
src/share/classes/com/sun/tools/apt/main/JavaCompiler.java | file | annotate | diff | comparison | revisions |
1.1 --- a/.hgtags Mon Dec 13 13:44:47 2010 -0800 1.2 +++ b/.hgtags Mon Dec 13 14:08:01 2010 -0800 1.3 @@ -95,3 +95,4 @@ 1.4 c491eec0acc73fa41b77e1619ed03e56d8a75b83 jdk7-b118 1.5 814561077c448292f78de461b6de5d78d57c2089 jdk7-b119 1.6 1dd813a529cfa06d850ee7dc42da42efb8f6af0e jdk7-b120 1.7 +1bf969e9792f977a8593496681539840a54fe644 jdk7-b121
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/share/classes/com/sun/tools/apt/main/AptJavaCompiler.java Mon Dec 13 14:08:01 2010 -0800 2.3 @@ -0,0 +1,292 @@ 2.4 +/* 2.5 + * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. Oracle designates this 2.11 + * particular file as subject to the "Classpath" exception as provided 2.12 + * by Oracle in the LICENSE file that accompanied this code. 2.13 + * 2.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.17 + * version 2 for more details (a copy is included in the LICENSE file that 2.18 + * accompanied this code). 2.19 + * 2.20 + * You should have received a copy of the GNU General Public License version 2.21 + * 2 along with this work; if not, write to the Free Software Foundation, 2.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.23 + * 2.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.25 + * or visit www.oracle.com if you need additional information or have any 2.26 + * questions. 2.27 + */ 2.28 + 2.29 +package com.sun.tools.apt.main; 2.30 + 2.31 +import java.io.*; 2.32 +import java.util.Map; 2.33 + 2.34 +import javax.tools.JavaFileManager; 2.35 +import javax.tools.JavaFileObject; 2.36 + 2.37 +import com.sun.tools.javac.file.JavacFileManager; 2.38 +import com.sun.tools.javac.util.*; 2.39 +import com.sun.tools.javac.code.*; 2.40 +import com.sun.tools.javac.jvm.*; 2.41 + 2.42 +import com.sun.tools.javac.code.Symbol.*; 2.43 +import com.sun.tools.javac.tree.JCTree.*; 2.44 + 2.45 +import com.sun.tools.apt.comp.*; 2.46 +import com.sun.tools.apt.util.Bark; 2.47 +import com.sun.mirror.apt.AnnotationProcessorFactory; 2.48 +import com.sun.tools.javac.parser.DocCommentScanner; 2.49 + 2.50 +/** 2.51 + * <p><b>This is NOT part of any supported API. 2.52 + * If you write code that depends on this, you do so at your own 2.53 + * risk. This code and its internal interfaces are subject to change 2.54 + * or deletion without notice.</b> 2.55 + */ 2.56 +@SuppressWarnings("deprecation") 2.57 +public class AptJavaCompiler extends com.sun.tools.javac.main.JavaCompiler { 2.58 + /** The context key for the compiler. */ 2.59 + protected static final Context.Key<AptJavaCompiler> compilerKey = 2.60 + new Context.Key<AptJavaCompiler>(); 2.61 + 2.62 + /** Get the JavaCompiler instance for this context. */ 2.63 + public static AptJavaCompiler instance(Context context) { 2.64 + AptJavaCompiler instance = context.get(compilerKey); 2.65 + if (instance == null) 2.66 + instance = new AptJavaCompiler(context); 2.67 + return instance; 2.68 + } 2.69 + 2.70 + 2.71 + java.util.Set<String> genSourceFileNames; 2.72 + java.util.Set<String> genClassFileNames; 2.73 + 2.74 + public java.util.Set<String> getSourceFileNames() { 2.75 + return genSourceFileNames; 2.76 + } 2.77 + 2.78 + /** List of names of generated class files. 2.79 + */ 2.80 + public java.util.Set<String> getClassFileNames() { 2.81 + return genClassFileNames; 2.82 + } 2.83 + 2.84 + java.util.Set<java.io.File> aggregateGenFiles = java.util.Collections.emptySet(); 2.85 + 2.86 + public java.util.Set<java.io.File> getAggregateGenFiles() { 2.87 + return aggregateGenFiles; 2.88 + } 2.89 + 2.90 + /** The bark to be used for error reporting. 2.91 + */ 2.92 + Bark bark; 2.93 + 2.94 + /** The log to be used for error reporting. 2.95 + */ 2.96 + Log log; 2.97 + 2.98 + /** The annotation framework 2.99 + */ 2.100 + Apt apt; 2.101 + 2.102 + private static Context preRegister(Context context) { 2.103 + Bark.preRegister(context); 2.104 + 2.105 + if (context.get(JavaFileManager.class) == null) 2.106 + JavacFileManager.preRegister(context); 2.107 + 2.108 + return context; 2.109 + } 2.110 + 2.111 + /** Construct a new compiler from a shared context. 2.112 + */ 2.113 + public AptJavaCompiler(Context context) { 2.114 + super(preRegister(context)); 2.115 + 2.116 + context.put(compilerKey, this); 2.117 + apt = Apt.instance(context); 2.118 + 2.119 + ClassReader classReader = ClassReader.instance(context); 2.120 + classReader.preferSource = true; 2.121 + 2.122 + // TEMPORARY NOTE: bark==log, but while refactoring, we maintain their 2.123 + // original identities, to remember the original intent. 2.124 + log = Log.instance(context); 2.125 + bark = Bark.instance(context); 2.126 + 2.127 + Options options = Options.instance(context); 2.128 + classOutput = options.get("-retrofit") == null; 2.129 + nocompile = options.get("-nocompile") != null; 2.130 + print = options.get("-print") != null; 2.131 + classesAsDecls= options.get("-XclassesAsDecls") != null; 2.132 + 2.133 + genSourceFileNames = new java.util.LinkedHashSet<String>(); 2.134 + genClassFileNames = new java.util.LinkedHashSet<String>(); 2.135 + 2.136 + // this forces a copy of the line map to be kept in the tree, 2.137 + // for use by com.sun.mirror.util.SourcePosition. 2.138 + lineDebugInfo = true; 2.139 + } 2.140 + 2.141 + /* Switches: 2.142 + */ 2.143 + 2.144 + /** Emit class files. This switch is always set, except for the first 2.145 + * phase of retrofitting, where signatures are parsed. 2.146 + */ 2.147 + public boolean classOutput; 2.148 + 2.149 + /** The internal printing annotation processor should be used. 2.150 + */ 2.151 + public boolean print; 2.152 + 2.153 + /** Compilation should not be done after annotation processing. 2.154 + */ 2.155 + public boolean nocompile; 2.156 + 2.157 + /** Are class files being treated as declarations 2.158 + */ 2.159 + public boolean classesAsDecls; 2.160 + 2.161 + /** Try to open input stream with given name. 2.162 + * Report an error if this fails. 2.163 + * @param filename The file name of the input stream to be opened. 2.164 + */ 2.165 + // PROVIDED FOR EXTREME BACKWARDS COMPATIBILITY 2.166 + // There are some very obscure errors that can arise while translating 2.167 + // the contents of a file from bytes to characters. In Tiger, these 2.168 + // diagnostics were ignored. This method provides compatibility with 2.169 + // that behavior. It would be better to honor those diagnostics, in which 2.170 + // case, this method can be deleted. 2.171 + @Override 2.172 + public CharSequence readSource(JavaFileObject filename) { 2.173 + try { 2.174 + inputFiles.add(filename); 2.175 + boolean prev = bark.setDiagnosticsIgnored(true); 2.176 + try { 2.177 + return filename.getCharContent(false); 2.178 + } 2.179 + finally { 2.180 + bark.setDiagnosticsIgnored(prev); 2.181 + } 2.182 + } catch (IOException e) { 2.183 + bark.error(Position.NOPOS, "cant.read.file", filename); 2.184 + return null; 2.185 + } 2.186 + } 2.187 + 2.188 + /** Parse contents of input stream. 2.189 + * @param filename The name of the file from which input stream comes. 2.190 + * @param input The input stream to be parsed. 2.191 + */ 2.192 + // PROVIDED FOR BACKWARDS COMPATIBILITY 2.193 + // In Tiger, diagnostics from the scanner and parser were ignored. 2.194 + // This method provides compatibility with that behavior. 2.195 + // It would be better to honor those diagnostics, in which 2.196 + // case, this method can be deleted. 2.197 + @Override 2.198 + protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) { 2.199 + boolean prev = bark.setDiagnosticsIgnored(true); 2.200 + try { 2.201 + return super.parse(filename, content); 2.202 + } 2.203 + finally { 2.204 + bark.setDiagnosticsIgnored(prev); 2.205 + } 2.206 + } 2.207 + 2.208 + @Override 2.209 + protected boolean keepComments() { 2.210 + return true; // make doc comments available to mirror API impl. 2.211 + } 2.212 + 2.213 + /** Track when the JavaCompiler has been used to compile something. */ 2.214 + private boolean hasBeenUsed = false; 2.215 + 2.216 + /** Main method: compile a list of files, return all compiled classes 2.217 + * @param filenames The names of all files to be compiled. 2.218 + */ 2.219 + public List<ClassSymbol> compile(List<String> filenames, 2.220 + Map<String, String> origOptions, 2.221 + ClassLoader aptCL, 2.222 + AnnotationProcessorFactory providedFactory, 2.223 + java.util.Set<Class<? extends AnnotationProcessorFactory> > productiveFactories, 2.224 + java.util.Set<java.io.File> aggregateGenFiles) 2.225 + throws Throwable { 2.226 + // as a JavaCompiler can only be used once, throw an exception if 2.227 + // it has been used before. 2.228 + assert !hasBeenUsed : "attempt to reuse JavaCompiler"; 2.229 + hasBeenUsed = true; 2.230 + 2.231 + this.aggregateGenFiles = aggregateGenFiles; 2.232 + 2.233 + long msec = System.currentTimeMillis(); 2.234 + 2.235 + ListBuffer<ClassSymbol> classes = new ListBuffer<ClassSymbol>(); 2.236 + try { 2.237 + JavacFileManager fm = (JavacFileManager)fileManager; 2.238 + //parse all files 2.239 + ListBuffer<JCCompilationUnit> trees = new ListBuffer<JCCompilationUnit>(); 2.240 + for (List<String> l = filenames; l.nonEmpty(); l = l.tail) { 2.241 + if (classesAsDecls) { 2.242 + if (! l.head.endsWith(".java") ) { // process as class file 2.243 + ClassSymbol cs = reader.enterClass(names.fromString(l.head)); 2.244 + try { 2.245 + cs.complete(); 2.246 + } catch(Symbol.CompletionFailure cf) { 2.247 + bark.aptError("CantFindClass", l); 2.248 + continue; 2.249 + } 2.250 + 2.251 + classes.append(cs); // add to list of classes 2.252 + continue; 2.253 + } 2.254 + } 2.255 + JavaFileObject fo = fm.getJavaFileObjectsFromStrings(List.of(l.head)).iterator().next(); 2.256 + trees.append(parse(fo)); 2.257 + } 2.258 + 2.259 + //enter symbols for all files 2.260 + List<JCCompilationUnit> roots = trees.toList(); 2.261 + 2.262 + if (errorCount() == 0) { 2.263 + boolean prev = bark.setDiagnosticsIgnored(true); 2.264 + try { 2.265 + enter.main(roots); 2.266 + } 2.267 + finally { 2.268 + bark.setDiagnosticsIgnored(prev); 2.269 + } 2.270 + } 2.271 + 2.272 + if (errorCount() == 0) { 2.273 + apt.main(roots, 2.274 + classes, 2.275 + origOptions, aptCL, 2.276 + providedFactory, 2.277 + productiveFactories); 2.278 + genSourceFileNames.addAll(apt.getSourceFileNames()); 2.279 + genClassFileNames.addAll(apt.getClassFileNames()); 2.280 + } 2.281 + 2.282 + } catch (Abort ex) { 2.283 + } 2.284 + 2.285 + if (verbose) 2.286 + printVerbose("total", Long.toString(System.currentTimeMillis() - msec)); 2.287 + 2.288 + chk.reportDeferredDiagnostics(); 2.289 + 2.290 + printCount("error", errorCount()); 2.291 + printCount("warn", warningCount()); 2.292 + 2.293 + return classes.toList(); 2.294 + } 2.295 +}
3.1 --- a/src/share/classes/com/sun/tools/apt/main/JavaCompiler.java Mon Dec 13 13:44:47 2010 -0800 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,292 +0,0 @@ 3.4 -/* 3.5 - * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 3.6 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.7 - * 3.8 - * This code is free software; you can redistribute it and/or modify it 3.9 - * under the terms of the GNU General Public License version 2 only, as 3.10 - * published by the Free Software Foundation. Oracle designates this 3.11 - * particular file as subject to the "Classpath" exception as provided 3.12 - * by Oracle in the LICENSE file that accompanied this code. 3.13 - * 3.14 - * This code is distributed in the hope that it will be useful, but WITHOUT 3.15 - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 3.16 - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 3.17 - * version 2 for more details (a copy is included in the LICENSE file that 3.18 - * accompanied this code). 3.19 - * 3.20 - * You should have received a copy of the GNU General Public License version 3.21 - * 2 along with this work; if not, write to the Free Software Foundation, 3.22 - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 3.23 - * 3.24 - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 3.25 - * or visit www.oracle.com if you need additional information or have any 3.26 - * questions. 3.27 - */ 3.28 - 3.29 -package com.sun.tools.apt.main; 3.30 - 3.31 -import java.io.*; 3.32 -import java.util.Map; 3.33 - 3.34 -import javax.tools.JavaFileManager; 3.35 -import javax.tools.JavaFileObject; 3.36 - 3.37 -import com.sun.tools.javac.file.JavacFileManager; 3.38 -import com.sun.tools.javac.util.*; 3.39 -import com.sun.tools.javac.code.*; 3.40 -import com.sun.tools.javac.jvm.*; 3.41 - 3.42 -import com.sun.tools.javac.code.Symbol.*; 3.43 -import com.sun.tools.javac.tree.JCTree.*; 3.44 - 3.45 -import com.sun.tools.apt.comp.*; 3.46 -import com.sun.tools.apt.util.Bark; 3.47 -import com.sun.mirror.apt.AnnotationProcessorFactory; 3.48 -import com.sun.tools.javac.parser.DocCommentScanner; 3.49 - 3.50 -/** 3.51 - * <p><b>This is NOT part of any supported API. 3.52 - * If you write code that depends on this, you do so at your own 3.53 - * risk. This code and its internal interfaces are subject to change 3.54 - * or deletion without notice.</b> 3.55 - */ 3.56 -@SuppressWarnings("deprecation") 3.57 -public class JavaCompiler extends com.sun.tools.javac.main.JavaCompiler { 3.58 - /** The context key for the compiler. */ 3.59 - protected static final Context.Key<JavaCompiler> compilerKey = 3.60 - new Context.Key<JavaCompiler>(); 3.61 - 3.62 - /** Get the JavaCompiler instance for this context. */ 3.63 - public static JavaCompiler instance(Context context) { 3.64 - JavaCompiler instance = context.get(compilerKey); 3.65 - if (instance == null) 3.66 - instance = new JavaCompiler(context); 3.67 - return instance; 3.68 - } 3.69 - 3.70 - 3.71 - java.util.Set<String> genSourceFileNames; 3.72 - java.util.Set<String> genClassFileNames; 3.73 - 3.74 - public java.util.Set<String> getSourceFileNames() { 3.75 - return genSourceFileNames; 3.76 - } 3.77 - 3.78 - /** List of names of generated class files. 3.79 - */ 3.80 - public java.util.Set<String> getClassFileNames() { 3.81 - return genClassFileNames; 3.82 - } 3.83 - 3.84 - java.util.Set<java.io.File> aggregateGenFiles = java.util.Collections.emptySet(); 3.85 - 3.86 - public java.util.Set<java.io.File> getAggregateGenFiles() { 3.87 - return aggregateGenFiles; 3.88 - } 3.89 - 3.90 - /** The bark to be used for error reporting. 3.91 - */ 3.92 - Bark bark; 3.93 - 3.94 - /** The log to be used for error reporting. 3.95 - */ 3.96 - Log log; 3.97 - 3.98 - /** The annotation framework 3.99 - */ 3.100 - Apt apt; 3.101 - 3.102 - private static Context preRegister(Context context) { 3.103 - Bark.preRegister(context); 3.104 - 3.105 - if (context.get(JavaFileManager.class) == null) 3.106 - JavacFileManager.preRegister(context); 3.107 - 3.108 - return context; 3.109 - } 3.110 - 3.111 - /** Construct a new compiler from a shared context. 3.112 - */ 3.113 - public JavaCompiler(Context context) { 3.114 - super(preRegister(context)); 3.115 - 3.116 - context.put(compilerKey, this); 3.117 - apt = Apt.instance(context); 3.118 - 3.119 - ClassReader classReader = ClassReader.instance(context); 3.120 - classReader.preferSource = true; 3.121 - 3.122 - // TEMPORARY NOTE: bark==log, but while refactoring, we maintain their 3.123 - // original identities, to remember the original intent. 3.124 - log = Log.instance(context); 3.125 - bark = Bark.instance(context); 3.126 - 3.127 - Options options = Options.instance(context); 3.128 - classOutput = options.get("-retrofit") == null; 3.129 - nocompile = options.get("-nocompile") != null; 3.130 - print = options.get("-print") != null; 3.131 - classesAsDecls= options.get("-XclassesAsDecls") != null; 3.132 - 3.133 - genSourceFileNames = new java.util.LinkedHashSet<String>(); 3.134 - genClassFileNames = new java.util.LinkedHashSet<String>(); 3.135 - 3.136 - // this forces a copy of the line map to be kept in the tree, 3.137 - // for use by com.sun.mirror.util.SourcePosition. 3.138 - lineDebugInfo = true; 3.139 - } 3.140 - 3.141 - /* Switches: 3.142 - */ 3.143 - 3.144 - /** Emit class files. This switch is always set, except for the first 3.145 - * phase of retrofitting, where signatures are parsed. 3.146 - */ 3.147 - public boolean classOutput; 3.148 - 3.149 - /** The internal printing annotation processor should be used. 3.150 - */ 3.151 - public boolean print; 3.152 - 3.153 - /** Compilation should not be done after annotation processing. 3.154 - */ 3.155 - public boolean nocompile; 3.156 - 3.157 - /** Are class files being treated as declarations 3.158 - */ 3.159 - public boolean classesAsDecls; 3.160 - 3.161 - /** Try to open input stream with given name. 3.162 - * Report an error if this fails. 3.163 - * @param filename The file name of the input stream to be opened. 3.164 - */ 3.165 - // PROVIDED FOR EXTREME BACKWARDS COMPATIBILITY 3.166 - // There are some very obscure errors that can arise while translating 3.167 - // the contents of a file from bytes to characters. In Tiger, these 3.168 - // diagnostics were ignored. This method provides compatibility with 3.169 - // that behavior. It would be better to honor those diagnostics, in which 3.170 - // case, this method can be deleted. 3.171 - @Override 3.172 - public CharSequence readSource(JavaFileObject filename) { 3.173 - try { 3.174 - inputFiles.add(filename); 3.175 - boolean prev = bark.setDiagnosticsIgnored(true); 3.176 - try { 3.177 - return filename.getCharContent(false); 3.178 - } 3.179 - finally { 3.180 - bark.setDiagnosticsIgnored(prev); 3.181 - } 3.182 - } catch (IOException e) { 3.183 - bark.error(Position.NOPOS, "cant.read.file", filename); 3.184 - return null; 3.185 - } 3.186 - } 3.187 - 3.188 - /** Parse contents of input stream. 3.189 - * @param filename The name of the file from which input stream comes. 3.190 - * @param input The input stream to be parsed. 3.191 - */ 3.192 - // PROVIDED FOR BACKWARDS COMPATIBILITY 3.193 - // In Tiger, diagnostics from the scanner and parser were ignored. 3.194 - // This method provides compatibility with that behavior. 3.195 - // It would be better to honor those diagnostics, in which 3.196 - // case, this method can be deleted. 3.197 - @Override 3.198 - protected JCCompilationUnit parse(JavaFileObject filename, CharSequence content) { 3.199 - boolean prev = bark.setDiagnosticsIgnored(true); 3.200 - try { 3.201 - return super.parse(filename, content); 3.202 - } 3.203 - finally { 3.204 - bark.setDiagnosticsIgnored(prev); 3.205 - } 3.206 - } 3.207 - 3.208 - @Override 3.209 - protected boolean keepComments() { 3.210 - return true; // make doc comments available to mirror API impl. 3.211 - } 3.212 - 3.213 - /** Track when the JavaCompiler has been used to compile something. */ 3.214 - private boolean hasBeenUsed = false; 3.215 - 3.216 - /** Main method: compile a list of files, return all compiled classes 3.217 - * @param filenames The names of all files to be compiled. 3.218 - */ 3.219 - public List<ClassSymbol> compile(List<String> filenames, 3.220 - Map<String, String> origOptions, 3.221 - ClassLoader aptCL, 3.222 - AnnotationProcessorFactory providedFactory, 3.223 - java.util.Set<Class<? extends AnnotationProcessorFactory> > productiveFactories, 3.224 - java.util.Set<java.io.File> aggregateGenFiles) 3.225 - throws Throwable { 3.226 - // as a JavaCompiler can only be used once, throw an exception if 3.227 - // it has been used before. 3.228 - assert !hasBeenUsed : "attempt to reuse JavaCompiler"; 3.229 - hasBeenUsed = true; 3.230 - 3.231 - this.aggregateGenFiles = aggregateGenFiles; 3.232 - 3.233 - long msec = System.currentTimeMillis(); 3.234 - 3.235 - ListBuffer<ClassSymbol> classes = new ListBuffer<ClassSymbol>(); 3.236 - try { 3.237 - JavacFileManager fm = (JavacFileManager)fileManager; 3.238 - //parse all files 3.239 - ListBuffer<JCCompilationUnit> trees = new ListBuffer<JCCompilationUnit>(); 3.240 - for (List<String> l = filenames; l.nonEmpty(); l = l.tail) { 3.241 - if (classesAsDecls) { 3.242 - if (! l.head.endsWith(".java") ) { // process as class file 3.243 - ClassSymbol cs = reader.enterClass(names.fromString(l.head)); 3.244 - try { 3.245 - cs.complete(); 3.246 - } catch(Symbol.CompletionFailure cf) { 3.247 - bark.aptError("CantFindClass", l); 3.248 - continue; 3.249 - } 3.250 - 3.251 - classes.append(cs); // add to list of classes 3.252 - continue; 3.253 - } 3.254 - } 3.255 - JavaFileObject fo = fm.getJavaFileObjectsFromStrings(List.of(l.head)).iterator().next(); 3.256 - trees.append(parse(fo)); 3.257 - } 3.258 - 3.259 - //enter symbols for all files 3.260 - List<JCCompilationUnit> roots = trees.toList(); 3.261 - 3.262 - if (errorCount() == 0) { 3.263 - boolean prev = bark.setDiagnosticsIgnored(true); 3.264 - try { 3.265 - enter.main(roots); 3.266 - } 3.267 - finally { 3.268 - bark.setDiagnosticsIgnored(prev); 3.269 - } 3.270 - } 3.271 - 3.272 - if (errorCount() == 0) { 3.273 - apt.main(roots, 3.274 - classes, 3.275 - origOptions, aptCL, 3.276 - providedFactory, 3.277 - productiveFactories); 3.278 - genSourceFileNames.addAll(apt.getSourceFileNames()); 3.279 - genClassFileNames.addAll(apt.getClassFileNames()); 3.280 - } 3.281 - 3.282 - } catch (Abort ex) { 3.283 - } 3.284 - 3.285 - if (verbose) 3.286 - printVerbose("total", Long.toString(System.currentTimeMillis() - msec)); 3.287 - 3.288 - chk.reportDeferredDiagnostics(); 3.289 - 3.290 - printCount("error", errorCount()); 3.291 - printCount("warn", warningCount()); 3.292 - 3.293 - return classes.toList(); 3.294 - } 3.295 -}
4.1 --- a/src/share/classes/com/sun/tools/apt/main/Main.java Mon Dec 13 13:44:47 2010 -0800 4.2 +++ b/src/share/classes/com/sun/tools/apt/main/Main.java Mon Dec 13 14:08:01 2010 -0800 4.3 @@ -421,7 +421,7 @@ 4.4 }, 4.5 new AptOption("-version", "opt.version") { 4.6 boolean process(String option) { 4.7 - Bark.printLines(out, ownName + " " + JavaCompiler.version()); 4.8 + Bark.printLines(out, ownName + " " + AptJavaCompiler.version()); 4.9 return super.process(option); 4.10 } 4.11 }, 4.12 @@ -1111,11 +1111,11 @@ 4.13 } 4.14 int exitCode = EXIT_OK; 4.15 4.16 - JavaCompiler comp = null; 4.17 + AptJavaCompiler comp = null; 4.18 try { 4.19 context.put(Bark.outKey, out); 4.20 4.21 - comp = JavaCompiler.instance(context); 4.22 + comp = AptJavaCompiler.instance(context); 4.23 if (comp == null) 4.24 return EXIT_SYSERR; 4.25 4.26 @@ -1184,7 +1184,7 @@ 4.27 */ 4.28 void bugMessage(Throwable ex) { 4.29 Bark.printLines(out, getLocalizedString("msg.bug", 4.30 - JavaCompiler.version())); 4.31 + AptJavaCompiler.version())); 4.32 ex.printStackTrace(out); 4.33 } 4.34
5.1 --- a/src/share/classes/com/sun/tools/apt/mirror/apt/FilerImpl.java Mon Dec 13 13:44:47 2010 -0800 5.2 +++ b/src/share/classes/com/sun/tools/apt/mirror/apt/FilerImpl.java Mon Dec 13 14:08:01 2010 -0800 5.3 @@ -120,7 +120,7 @@ 5.4 5.5 private final Options opts; 5.6 private final DeclarationMaker declMaker; 5.7 - private final com.sun.tools.apt.main.JavaCompiler comp; 5.8 + private final com.sun.tools.apt.main.AptJavaCompiler comp; 5.9 5.10 // Platform's default encoding 5.11 private final static String DEFAULT_ENCODING = 5.12 @@ -177,7 +177,7 @@ 5.13 opts = Options.instance(context); 5.14 declMaker = DeclarationMaker.instance(context); 5.15 bark = Bark.instance(context); 5.16 - comp = com.sun.tools.apt.main.JavaCompiler.instance(context); 5.17 + comp = com.sun.tools.apt.main.AptJavaCompiler.instance(context); 5.18 roundOver = false; 5.19 this.filesCreated = comp.getAggregateGenFiles(); 5.20
6.1 --- a/src/share/classes/com/sun/tools/javac/code/Type.java Mon Dec 13 13:44:47 2010 -0800 6.2 +++ b/src/share/classes/com/sun/tools/javac/code/Type.java Mon Dec 13 14:08:01 2010 -0800 6.3 @@ -245,7 +245,7 @@ 6.4 public String argtypes(boolean varargs) { 6.5 List<Type> args = getParameterTypes(); 6.6 if (!varargs) return args.toString(); 6.7 - StringBuffer buf = new StringBuffer(); 6.8 + StringBuilder buf = new StringBuilder(); 6.9 while (args.tail.nonEmpty()) { 6.10 buf.append(args.head); 6.11 args = args.tail; 6.12 @@ -935,7 +935,7 @@ 6.13 6.14 public static class TypeVar extends Type implements TypeVariable { 6.15 6.16 - /** The bound of this type variable; set from outside. 6.17 + /** The upper bound of this type variable; set from outside. 6.18 * Must be nonempty once it is set. 6.19 * For a bound, `bound' is the bound type itself. 6.20 * Multiple bounds are expressed as a single class type which has the 6.21 @@ -946,6 +946,12 @@ 6.22 * points to the first class or interface bound. 6.23 */ 6.24 public Type bound = null; 6.25 + 6.26 + /** The lower bound of this type variable. 6.27 + * TypeVars don't normally have a lower bound, so it is normally set 6.28 + * to syms.botType. 6.29 + * Subtypes, such as CapturedType, may provide a different value. 6.30 + */ 6.31 public Type lower; 6.32 6.33 public TypeVar(Name name, Symbol owner, Type lower) { 6.34 @@ -965,10 +971,12 @@ 6.35 return v.visitTypeVar(this, s); 6.36 } 6.37 6.38 + @Override 6.39 public Type getUpperBound() { return bound; } 6.40 6.41 int rank_field = -1; 6.42 6.43 + @Override 6.44 public Type getLowerBound() { 6.45 return lower; 6.46 } 6.47 @@ -992,7 +1000,6 @@ 6.48 */ 6.49 public static class CapturedType extends TypeVar { 6.50 6.51 - public Type lower; 6.52 public WildcardType wildcard; 6.53 6.54 public CapturedType(Name name, 6.55 @@ -1012,10 +1019,6 @@ 6.56 return v.visitCapturedType(this, s); 6.57 } 6.58 6.59 - public Type getLowerBound() { 6.60 - return lower; 6.61 - } 6.62 - 6.63 @Override 6.64 public boolean isCaptured() { 6.65 return true;
7.1 --- a/src/share/classes/com/sun/tools/javac/code/Types.java Mon Dec 13 13:44:47 2010 -0800 7.2 +++ b/src/share/classes/com/sun/tools/javac/code/Types.java Mon Dec 13 14:08:01 2010 -0800 7.3 @@ -641,7 +641,7 @@ 7.4 if (!set.remove(new SingletonType(x))) 7.5 return false; 7.6 } 7.7 - return (set.size() == 0); 7.8 + return (set.isEmpty()); 7.9 } 7.10 return t.tsym == s.tsym 7.11 && visit(t.getEnclosingType(), s.getEnclosingType()) 7.12 @@ -838,26 +838,26 @@ 7.13 return isSameType(t, s); 7.14 } 7.15 7.16 - void debugContainsType(WildcardType t, Type s) { 7.17 - System.err.println(); 7.18 - System.err.format(" does %s contain %s?%n", t, s); 7.19 - System.err.format(" %s U(%s) <: U(%s) %s = %s%n", 7.20 - upperBound(s), s, t, U(t), 7.21 - t.isSuperBound() 7.22 - || isSubtypeNoCapture(upperBound(s), U(t))); 7.23 - System.err.format(" %s L(%s) <: L(%s) %s = %s%n", 7.24 - L(t), t, s, lowerBound(s), 7.25 - t.isExtendsBound() 7.26 - || isSubtypeNoCapture(L(t), lowerBound(s))); 7.27 - System.err.println(); 7.28 - } 7.29 +// void debugContainsType(WildcardType t, Type s) { 7.30 +// System.err.println(); 7.31 +// System.err.format(" does %s contain %s?%n", t, s); 7.32 +// System.err.format(" %s U(%s) <: U(%s) %s = %s%n", 7.33 +// upperBound(s), s, t, U(t), 7.34 +// t.isSuperBound() 7.35 +// || isSubtypeNoCapture(upperBound(s), U(t))); 7.36 +// System.err.format(" %s L(%s) <: L(%s) %s = %s%n", 7.37 +// L(t), t, s, lowerBound(s), 7.38 +// t.isExtendsBound() 7.39 +// || isSubtypeNoCapture(L(t), lowerBound(s))); 7.40 +// System.err.println(); 7.41 +// } 7.42 7.43 @Override 7.44 public Boolean visitWildcardType(WildcardType t, Type s) { 7.45 if (s.tag >= firstPartialTag) 7.46 return containedBy(s, t); 7.47 else { 7.48 - // debugContainsType(t, s); 7.49 +// debugContainsType(t, s); 7.50 return isSameWildcard(t, s) 7.51 || isCaptureOf(s, t) 7.52 || ((t.isExtendsBound() || isSubtypeNoCapture(L(t), lowerBound(s))) &&
8.1 --- a/src/share/classes/com/sun/tools/javac/comp/Resolve.java Mon Dec 13 13:44:47 2010 -0800 8.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Resolve.java Mon Dec 13 14:08:01 2010 -0800 8.3 @@ -197,7 +197,7 @@ 8.4 } 8.5 return (checkInner == false || c.type.getEnclosingType() == Type.noType) ? 8.6 isAccessible : 8.7 - isAccessible & isAccessible(env, c.type.getEnclosingType(), checkInner); 8.8 + isAccessible && isAccessible(env, c.type.getEnclosingType(), checkInner); 8.9 } 8.10 //where 8.11 /** Is given class a subclass of given base class, or an inner class 8.12 @@ -234,7 +234,6 @@ 8.13 } 8.14 public boolean isAccessible(Env<AttrContext> env, Type site, Symbol sym, boolean checkInner) { 8.15 if (sym.name == names.init && sym.owner != site.tsym) return false; 8.16 - ClassSymbol sub; 8.17 switch ((short)(sym.flags() & AccessFlags)) { 8.18 case PRIVATE: 8.19 return
9.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 9.2 +++ b/test/tools/javac/types/BoxingConversionTest.java Mon Dec 13 14:08:01 2010 -0800 9.3 @@ -0,0 +1,254 @@ 9.4 +/* 9.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 9.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 9.7 + * 9.8 + * This code is free software; you can redistribute it and/or modify it 9.9 + * under the terms of the GNU General Public License version 2 only, as 9.10 + * published by the Free Software Foundation. 9.11 + * 9.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 9.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 9.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 9.15 + * version 2 for more details (a copy is included in the LICENSE file that 9.16 + * accompanied this code). 9.17 + * 9.18 + * You should have received a copy of the GNU General Public License version 9.19 + * 2 along with this work; if not, write to the Free Software Foundation, 9.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 9.21 + * 9.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 9.23 + * or visit www.oracle.com if you need additional information or have any 9.24 + * questions. 9.25 + */ 9.26 + 9.27 +/* 9.28 + * @test 9.29 + * @bug 7006109 9.30 + * @summary Add test library to simplify the task of writing automated type-system tests 9.31 + * @author mcimadamore 9.32 + * @library . 9.33 + * @run main BoxingConversionTest 9.34 + */ 9.35 + 9.36 +import com.sun.tools.javac.code.Type; 9.37 +import com.sun.tools.javac.code.Type.*; 9.38 +import com.sun.tools.javac.code.Symbol.*; 9.39 +import java.lang.reflect.Array; 9.40 +import java.util.EnumSet; 9.41 + 9.42 +/** 9.43 + * Check invariants in assignment/method conversion involving boxing conversions 9.44 + */ 9.45 +public class BoxingConversionTest extends TypeHarness { 9.46 + 9.47 + Type[] types1; 9.48 + Type[] types2; 9.49 + Type[] types3; 9.50 + 9.51 + enum Result { 9.52 + OK_BOTH(true), 9.53 + FAIL_BOTH(false), 9.54 + OK_ASSIGN_ONLY(true); 9.55 + 9.56 + boolean value; 9.57 + 9.58 + Result(boolean value) { 9.59 + this.value = value; 9.60 + } 9.61 + } 9.62 + 9.63 + enum ConversionKind { 9.64 + ASSIGNMENT_CONVERSION(EnumSet.of(Result.OK_BOTH, Result.OK_ASSIGN_ONLY)) { 9.65 + @Override 9.66 + void check(TypeHarness harness, Type from, Type to, Result expected) { 9.67 + harness.assertAssignable(from, to, resSet.contains(expected)); 9.68 + } 9.69 + }, 9.70 + METHOD_CONVERSION(EnumSet.of(Result.OK_BOTH)) { 9.71 + @Override 9.72 + void check(TypeHarness harness, Type from, Type to, Result expected) { 9.73 + harness.assertConvertible(from, to, resSet.contains(expected)); 9.74 + } 9.75 + }; 9.76 + 9.77 + EnumSet<Result> resSet; 9.78 + 9.79 + private ConversionKind(EnumSet<Result> resSet) { 9.80 + this.resSet = resSet; 9.81 + } 9.82 + 9.83 + abstract void check(TypeHarness harness, Type from, Type to, Result expected); 9.84 + } 9.85 + 9.86 + enum TestKind { 9.87 + SIMPLE { 9.88 + @Override 9.89 + Type[] getFromTypes(BoxingConversionTest harness) { 9.90 + return harness.types1; 9.91 + } 9.92 + @Override 9.93 + Type[] getToTypes(BoxingConversionTest harness) { 9.94 + return harness.types1; 9.95 + } 9.96 + @Override 9.97 + Result[][] getResults(BoxingConversionTest harness) { 9.98 + return harness.results1; 9.99 + } 9.100 + }, 9.101 + CONSTANT_TYPES { 9.102 + @Override 9.103 + Type[] getFromTypes(BoxingConversionTest harness) { 9.104 + return harness.types2; 9.105 + } 9.106 + @Override 9.107 + Type[] getToTypes(BoxingConversionTest harness) { 9.108 + return harness.types3; 9.109 + } 9.110 + @Override 9.111 + Result[][] getResults(BoxingConversionTest harness) { 9.112 + return harness.results2; 9.113 + } 9.114 + }; 9.115 + 9.116 + abstract Type[] getFromTypes(BoxingConversionTest harness); 9.117 + abstract Type[] getToTypes(BoxingConversionTest harness); 9.118 + abstract Result[][] getResults(BoxingConversionTest harness); 9.119 + } 9.120 + 9.121 + static final Result T = Result.OK_BOTH; 9.122 + static final Result F = Result.FAIL_BOTH; 9.123 + static final Result A = Result.OK_ASSIGN_ONLY; 9.124 + static final Result X = Result.FAIL_BOTH.FAIL_BOTH; 9.125 + 9.126 + Result[][] results1 = { 9.127 + //byte, short, int, long, float, double, char, bool, Byte, Short, Integer, Long, Float, Double, Character, Boolean 9.128 + /*byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F }, 9.129 + /*short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F }, 9.130 + /*int*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F }, 9.131 + /*long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F }, 9.132 + /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F }, 9.133 + /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F }, 9.134 + /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F }, 9.135 + /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }, 9.136 + /*Byte*/ { T , T , T , T , T , T , F , F , T , F , F , F , F , F , F , F }, 9.137 + /*Short*/ { F , T , T , T , T , T , F , F , F , T , F , F , F , F , F , F }, 9.138 + /*Integer*/ { F , F , T , T , T , T , F , F , F , F , T , F , F , F , F , F }, 9.139 + /*Long*/ { F , F , F , T , T , T , F , F , F , F , F , T , F , F , F , F }, 9.140 + /*Float*/ { F , F , F , F , T , T , F , F , F , F , F , F , T , F , F , F }, 9.141 + /*Double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , T , F , F }, 9.142 + /*Character*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , T , F }, 9.143 + /*Boolean*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , T }}; 9.144 + 9.145 + Result[][] results2 = { 9.146 + //Byte, Short, Integer, Long, Float, Double, Chararacter, Boolean 9.147 + /*byte*/ { T , F , F , F , F , F , F , F }, 9.148 + /*short*/ { F , T , F , F , F , F , F , F }, 9.149 + /*short1*/ { A , T , F , F , F , F , A , F }, 9.150 + /*short2*/ { F , T , F , F , F , F , A , F }, 9.151 + /*int*/ { F , F , T , F , F , F , F , F }, 9.152 + /*int1*/ { A , A , T , F , F , F , A , F }, 9.153 + /*int2*/ { F , A , T , F , F , F , A , F }, 9.154 + /*int4*/ { F , F , T , F , F , F , F , F }, 9.155 + /*long*/ { F , F , F , T , F , F , F , F }, 9.156 + /*long1*/ { F , F , F , T , F , F , F , F }, 9.157 + /*long2*/ { F , F , F , T , F , F , F , F }, 9.158 + /*long4*/ { F , F , F , T , F , F , F , F }, 9.159 + /*long8*/ { F , F , F , T , F , F , F , F }, 9.160 + /*float*/ { F , F , F , F , T , F , F , F }, 9.161 + /*float1*/ { F , F , F , F , T , F , F , F }, 9.162 + /*float2*/ { F , F , F , F , T , F , F , F }, 9.163 + /*float4*/ { F , F , F , F , T , F , F , F }, 9.164 + /*double*/ { F , F , F , F , F , T , F , F }, 9.165 + /*double1*/ { F , F , F , F , F , T , F , F }, 9.166 + /*double2*/ { F , F , F , F , F , T , F , F }, 9.167 + /*double4*/ { F , F , F , F , F , T , F , F }, 9.168 + /*double8*/ { F , F , F , F , F , T , F , F }, 9.169 + /*char*/ { F , F , F , F , F , F , T , F }, 9.170 + /*char1*/ { A , A , F , F , F , F , T , F }, 9.171 + /*char2*/ { F , A , F , F , F , F , T , F }, 9.172 + /*bool*/ { F , F , F , F , F , F , F , T }}; 9.173 + 9.174 + BoxingConversionTest() { 9.175 + Type[] primitiveTypes = new Type[] { 9.176 + predef.byteType, 9.177 + predef.shortType, 9.178 + predef.intType, 9.179 + predef.longType, 9.180 + predef.floatType, 9.181 + predef.doubleType, 9.182 + predef.charType, 9.183 + predef.booleanType }; 9.184 + 9.185 + Type[] boxedTypes = new Type[primitiveTypes.length]; 9.186 + for (int i = 0 ; i < primitiveTypes.length ; i++) { 9.187 + boxedTypes[i] = box(primitiveTypes[i]); 9.188 + } 9.189 + 9.190 + types1 = join(Type.class, primitiveTypes, boxedTypes); 9.191 + 9.192 + types2 = new Type[] { 9.193 + predef.byteType, 9.194 + predef.shortType, 9.195 + fac.Constant((short)0x0001), 9.196 + fac.Constant((short)0x0100), 9.197 + predef.intType, 9.198 + fac.Constant((int)0x0000_0001), 9.199 + fac.Constant((int)0x0000_0100), 9.200 + fac.Constant((int)0x0001_0000), 9.201 + predef.longType, 9.202 + fac.Constant((long)0x0000_0000_0000_0001L), 9.203 + fac.Constant((long)0x0000_0000_0000_0100L), 9.204 + fac.Constant((long)0x0000_0000_0001_0000L), 9.205 + fac.Constant((long)0x0001_0000_0000_0000L), 9.206 + predef.floatType, 9.207 + fac.Constant((float)0x0000_0001), 9.208 + fac.Constant((float)0x0000_0100), 9.209 + fac.Constant((float)0x0001_0000), 9.210 + predef.doubleType, 9.211 + fac.Constant((double)0x0000_0000_0000_0001L), 9.212 + fac.Constant((double)0x0000_0000_0000_0100L), 9.213 + fac.Constant((double)0x0000_0000_0001_0000L), 9.214 + fac.Constant((double)0x0001_0000_0000_0000L), 9.215 + predef.charType, 9.216 + fac.Constant((char)0x0001), 9.217 + fac.Constant((char)0x0100), 9.218 + predef.booleanType 9.219 + }; 9.220 + 9.221 + types3 = boxedTypes; 9.222 + } 9.223 + 9.224 + void testConversion(ConversionKind convKind, TestKind testKind) { 9.225 + Type[] rows = testKind.getFromTypes(this); 9.226 + Type[] cols = testKind.getToTypes(this); 9.227 + for (int i = 0; i < rows.length ; i++) { 9.228 + for (int j = 0; j < cols.length ; j++) { 9.229 + convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]); 9.230 + } 9.231 + } 9.232 + } 9.233 + 9.234 + @SuppressWarnings("unchecked") 9.235 + <T> T[] join(Class<T> type, T[]... args) { 9.236 + int totalLength = 0; 9.237 + for (T[] arr : args) { 9.238 + totalLength += arr.length; 9.239 + } 9.240 + T[] new_arr = (T[])Array.newInstance(type, totalLength); 9.241 + int idx = 0; 9.242 + for (T[] arr : args) { 9.243 + System.arraycopy(arr, 0, new_arr, idx, arr.length); 9.244 + idx += arr.length; 9.245 + } 9.246 + return new_arr; 9.247 + } 9.248 + 9.249 + public static void main(String[] args) { 9.250 + BoxingConversionTest harness = new BoxingConversionTest(); 9.251 + for (ConversionKind convKind : ConversionKind.values()) { 9.252 + for (TestKind testKind : TestKind.values()) { 9.253 + harness.testConversion(convKind, testKind); 9.254 + } 9.255 + } 9.256 + } 9.257 +}
10.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 10.2 +++ b/test/tools/javac/types/CastTest.java Mon Dec 13 14:08:01 2010 -0800 10.3 @@ -0,0 +1,129 @@ 10.4 +/* 10.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 10.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 10.7 + * 10.8 + * This code is free software; you can redistribute it and/or modify it 10.9 + * under the terms of the GNU General Public License version 2 only, as 10.10 + * published by the Free Software Foundation. 10.11 + * 10.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 10.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 10.15 + * version 2 for more details (a copy is included in the LICENSE file that 10.16 + * accompanied this code). 10.17 + * 10.18 + * You should have received a copy of the GNU General Public License version 10.19 + * 2 along with this work; if not, write to the Free Software Foundation, 10.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 10.21 + * 10.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 10.23 + * or visit www.oracle.com if you need additional information or have any 10.24 + * questions. 10.25 + */ 10.26 + 10.27 +/* 10.28 + * @test 10.29 + * @bug 7006109 10.30 + * @summary Add test library to simplify the task of writing automated type-system tests 10.31 + * @author mcimadamore 10.32 + * @library . 10.33 + * @run main CastTest 10.34 + */ 10.35 + 10.36 +import com.sun.tools.javac.code.Type; 10.37 +import com.sun.tools.javac.code.Type.*; 10.38 +import com.sun.tools.javac.code.Symbol.*; 10.39 +import java.lang.reflect.Array; 10.40 + 10.41 +import static com.sun.tools.javac.code.Flags.*; 10.42 + 10.43 +/** 10.44 + * Check invariants in cast conversion involving primitive types and arrays 10.45 + */ 10.46 +public class CastTest extends TypeHarness { 10.47 + 10.48 + Type[] allTypes; 10.49 + 10.50 + static final boolean T = true; 10.51 + static final boolean F = false; 10.52 + 10.53 + boolean[][] cast_result = { 10.54 + //byte, short, int, long, float, double, char, bool, C, +C, I, T, byte[], short[], int[], long[], float[], double[], char[], bool[], C[], +C[], I[], T[] 10.55 + /*byte*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, 10.56 + /*short*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, 10.57 + /*int*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, 10.58 + /*long*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, 10.59 + /*float*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, 10.60 + /*double*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, 10.61 + /*char*/ { T , T , T , T , T , T , T , F , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, 10.62 + /*bool*/ { F , F , F , F , F , F , F , T , F, F , F, F, F , F , F , F , F , F , F , F , F , F , F , F }, 10.63 + /*C*/ { F , F , F , F , F , F , F , F , T, F , T, T, F , F , F , F , F , F , F , F , F , F , F , F }, 10.64 + /*+C*/ { F , F , F , F , F , F , F , F , F, T , F, T, F , F , F , F , F , F , F , F , F , F , F , F }, 10.65 + /*I*/ { F , F , F , F , F , F , F , F , T, F , T, T, F , F , F , F , F , F , F , F , F , F , F , F }, 10.66 + /*T*/ { F , F , F , F , F , F , F , F , T, T , T, T, T , T , T , T , T , T , T , T , T , T , T , T }, 10.67 + /*byte[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, T , F , F , F , F , F , F , F , F , F , F , F }, 10.68 + /*short[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , T , F , F , F , F , F , F , F , F , F , F }, 10.69 + /*int[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , T , F , F , F , F , F , F , F , F , F }, 10.70 + /*long[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , T , F , F , F , F , F , F , F , F }, 10.71 + /*float[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , T , F , F , F , F , F , F , F }, 10.72 + /*double[]*/{ F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , T , F , F , F , F , F , F }, 10.73 + /*char[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , T , F , F , F , F , F }, 10.74 + /*bool[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , T , F , F , F , F }, 10.75 + /*C[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , F , T , F , T , T }, 10.76 + /*+C[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , F , F , T , F , T }, 10.77 + /*I[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , F , T , F , T , T }, 10.78 + /*T[]*/ { F , F , F , F , F , F , F , F , F, F , F, T, F , F , F , F , F , F , F , F , T , T , T , T }}; 10.79 + 10.80 + CastTest() { 10.81 + Type[] primitiveTypes = { 10.82 + predef.byteType, 10.83 + predef.shortType, 10.84 + predef.intType, 10.85 + predef.longType, 10.86 + predef.floatType, 10.87 + predef.doubleType, 10.88 + predef.charType, 10.89 + predef.booleanType }; 10.90 + 10.91 + Type[] referenceTypes = { 10.92 + fac.Class(), 10.93 + fac.Class(FINAL), 10.94 + fac.Interface(), 10.95 + fac.TypeVariable() }; 10.96 + 10.97 + Type[] arrayTypes = new Type[primitiveTypes.length + referenceTypes.length]; 10.98 + int idx = 0; 10.99 + for (Type t : join(Type.class, primitiveTypes, referenceTypes)) { 10.100 + arrayTypes[idx++] = fac.Array(t); 10.101 + } 10.102 + 10.103 + allTypes = join(Type.class, primitiveTypes, referenceTypes, arrayTypes); 10.104 + } 10.105 + 10.106 + void test() { 10.107 + for (int i = 0; i < allTypes.length ; i++) { 10.108 + for (int j = 0; j < allTypes.length ; j++) { 10.109 + assertCastable(allTypes[i], allTypes[j], cast_result[i][j]); 10.110 + } 10.111 + } 10.112 + } 10.113 + 10.114 + @SuppressWarnings("unchecked") 10.115 + <T> T[] join(Class<T> type, T[]... args) { 10.116 + int totalLength = 0; 10.117 + for (T[] arr : args) { 10.118 + totalLength += arr.length; 10.119 + } 10.120 + T[] new_arr = (T[])Array.newInstance(type, totalLength); 10.121 + int idx = 0; 10.122 + for (T[] arr : args) { 10.123 + System.arraycopy(arr, 0, new_arr, idx, arr.length); 10.124 + idx += arr.length; 10.125 + } 10.126 + return new_arr; 10.127 + } 10.128 + 10.129 + public static void main(String[] args) { 10.130 + new CastTest().test(); 10.131 + } 10.132 +}
11.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 11.2 +++ b/test/tools/javac/types/PrimitiveConversionTest.java Mon Dec 13 14:08:01 2010 -0800 11.3 @@ -0,0 +1,275 @@ 11.4 +/* 11.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 11.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 11.7 + * 11.8 + * This code is free software; you can redistribute it and/or modify it 11.9 + * under the terms of the GNU General Public License version 2 only, as 11.10 + * published by the Free Software Foundation. 11.11 + * 11.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 11.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11.15 + * version 2 for more details (a copy is included in the LICENSE file that 11.16 + * accompanied this code). 11.17 + * 11.18 + * You should have received a copy of the GNU General Public License version 11.19 + * 2 along with this work; if not, write to the Free Software Foundation, 11.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 11.21 + * 11.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 11.23 + * or visit www.oracle.com if you need additional information or have any 11.24 + * questions. 11.25 + */ 11.26 + 11.27 +/* 11.28 + * @test 11.29 + * @bug 7006109 11.30 + * @summary Add test library to simplify the task of writing automated type-system tests 11.31 + * @author mcimadamore 11.32 + * @library . 11.33 + * @run main PrimitiveConversionTest 11.34 + */ 11.35 + 11.36 +import com.sun.tools.javac.code.Type; 11.37 +import com.sun.tools.javac.code.Type.*; 11.38 +import com.sun.tools.javac.code.Symbol.*; 11.39 +import java.lang.reflect.Array; 11.40 +import java.util.EnumSet; 11.41 + 11.42 +/** 11.43 + * Check invariants in assignment/method conversion involving primitive types and arrays 11.44 + */ 11.45 +public class PrimitiveConversionTest extends TypeHarness { 11.46 + 11.47 + Type[] types1; 11.48 + Type[] types2; 11.49 + Type[] types3; 11.50 + 11.51 + enum Result { 11.52 + OK_BOTH(true), 11.53 + FAIL_BOTH(false), 11.54 + OK_ASSIGN_ONLY(true); 11.55 + 11.56 + boolean value; 11.57 + 11.58 + Result(boolean value) { 11.59 + this.value = value; 11.60 + } 11.61 + } 11.62 + 11.63 + enum ConversionKind { 11.64 + ASSIGNMENT_CONVERSION(EnumSet.of(Result.OK_BOTH, Result.OK_ASSIGN_ONLY)) { 11.65 + @Override 11.66 + void check(TypeHarness harness, Type from, Type to, Result expected) { 11.67 + harness.assertAssignable(from, to, resSet.contains(expected)); 11.68 + } 11.69 + }, 11.70 + METHOD_CONVERSION(EnumSet.of(Result.OK_BOTH)) { 11.71 + @Override 11.72 + void check(TypeHarness harness, Type from, Type to, Result expected) { 11.73 + harness.assertConvertible(from, to, resSet.contains(expected)); 11.74 + } 11.75 + }; 11.76 + 11.77 + EnumSet<Result> resSet; 11.78 + 11.79 + private ConversionKind(EnumSet<Result> resSet) { 11.80 + this.resSet = resSet; 11.81 + } 11.82 + 11.83 + abstract void check(TypeHarness harness, Type from, Type to, Result expected); 11.84 + } 11.85 + 11.86 + enum TestKind { 11.87 + SIMPLE { 11.88 + @Override 11.89 + Type[] getFromTypes(PrimitiveConversionTest harness) { 11.90 + return harness.types1; 11.91 + } 11.92 + @Override 11.93 + Type[] getToTypes(PrimitiveConversionTest harness) { 11.94 + return harness.types1; 11.95 + } 11.96 + @Override 11.97 + Result[][] getResults(PrimitiveConversionTest harness) { 11.98 + return harness.results1; 11.99 + } 11.100 + }, 11.101 + CONSTANT_TYPES { 11.102 + @Override 11.103 + Type[] getFromTypes(PrimitiveConversionTest harness) { 11.104 + return harness.types2; 11.105 + } 11.106 + @Override 11.107 + Type[] getToTypes(PrimitiveConversionTest harness) { 11.108 + return harness.types3; 11.109 + } 11.110 + @Override 11.111 + Result[][] getResults(PrimitiveConversionTest harness) { 11.112 + return harness.results2; 11.113 + } 11.114 + }; 11.115 + 11.116 + abstract Type[] getFromTypes(PrimitiveConversionTest harness); 11.117 + abstract Type[] getToTypes(PrimitiveConversionTest harness); 11.118 + abstract Result[][] getResults(PrimitiveConversionTest harness); 11.119 + } 11.120 + 11.121 + static final Result T = Result.OK_BOTH; 11.122 + static final Result F = Result.FAIL_BOTH; 11.123 + static final Result A = Result.OK_ASSIGN_ONLY; 11.124 + 11.125 + Result[][] results1 = { 11.126 + //byte, short, int, long, float, double, char, bool, C1, C2, C3, T , byte[], short[], int[], long[], float[], double[], char[], bool[], C1[], C2[], C3[], T[] 11.127 + /*byte*/ { T , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.128 + /*short*/ { F , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.129 + /*int*/ { F , F , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.130 + /*long*/ { F , F , F , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.131 + /*float*/ { F , F , F , F , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.132 + /*double*/ { F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.133 + /*char*/ { F , F , T , T , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.134 + /*bool*/ { F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.135 + /*C1*/ { F , F , F , F , F , F , F , F , T , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.136 + /*C2*/ { F , F , F , F , F , F , F , F , T , T , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.137 + /*C3*/ { F , F , F , F , F , F , F , F , T , F , T , F , F , F , F , F , F , F , F , F , F , F , F , F }, 11.138 + /*T*/ { F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F , F }, 11.139 + /*byte[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F , F }, 11.140 + /*short[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F , F }, 11.141 + /*int[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F , F }, 11.142 + /*long[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F , F }, 11.143 + /*float[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F , F }, 11.144 + /*double[]*/{ F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F , F }, 11.145 + /*char[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F , F }, 11.146 + /*bool[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , F , F , F }, 11.147 + /*C1[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , T , F }, 11.148 + /*C2[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , T , T , F }, 11.149 + /*C3[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T , F , T , F }, 11.150 + /*T[]*/ { F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , F , T }}; 11.151 + 11.152 + Result[][] results2 = { 11.153 + //byte, short, int, long, float, double, char, bool 11.154 + /*byte*/ { T , T , T , T , T , T , F , F }, 11.155 + /*short*/ { F , T , T , T , T , T , F , F }, 11.156 + /*short1*/ { A , T , T , T , T , T , A , F }, 11.157 + /*short2*/ { F , T , T , T , T , T , A , F }, 11.158 + /*int*/ { F , F , T , T , T , T , F , F }, 11.159 + /*int1*/ { A , A , T , T , T , T , A , F }, 11.160 + /*int2*/ { F , A , T , T , T , T , A , F }, 11.161 + /*int4*/ { F , F , T , T , T , T , F , F }, 11.162 + /*long*/ { F , F , F , T , T , T , F , F }, 11.163 + /*long1*/ { F , F , F , T , T , T , F , F }, 11.164 + /*long2*/ { F , F , F , T , T , T , F , F }, 11.165 + /*long4*/ { F , F , F , T , T , T , F , F }, 11.166 + /*long8*/ { F , F , F , T , T , T , F , F }, 11.167 + /*float*/ { F , F , F , F , T , T , F , F }, 11.168 + /*float1*/ { F , F , F , F , T , T , F , F }, 11.169 + /*float2*/ { F , F , F , F , T , T , F , F }, 11.170 + /*float4*/ { F , F , F , F , T , T , F , F }, 11.171 + /*double*/ { F , F , F , F , F , T , F , F }, 11.172 + /*double1*/ { F , F , F , F , F , T , F , F }, 11.173 + /*double2*/ { F , F , F , F , F , T , F , F }, 11.174 + /*double4*/ { F , F , F , F , F , T , F , F }, 11.175 + /*double8*/ { F , F , F , F , F , T , F , F }, 11.176 + /*char*/ { F , F , T , T , T , T , T , F }, 11.177 + /*char1*/ { A , A , T , T , T , T , T , F }, 11.178 + /*char2*/ { F , A , T , T , T , T , T , F }, 11.179 + /*bool*/ { F , F , F , F , F , F , F , T }}; 11.180 + 11.181 + PrimitiveConversionTest() { 11.182 + Type[] primitiveTypes = new Type[] { 11.183 + predef.byteType, 11.184 + predef.shortType, 11.185 + predef.intType, 11.186 + predef.longType, 11.187 + predef.floatType, 11.188 + predef.doubleType, 11.189 + predef.charType, 11.190 + predef.booleanType }; 11.191 + 11.192 + ClassType c1 = fac.Class(fac.TypeVariable()); 11.193 + ClassType c2 = fac.Class(); 11.194 + c2.supertype_field = subst(c1, 11.195 + Mapping(c1.getTypeArguments().head, predef.stringType)); 11.196 + Type c3 = erasure(c1); 11.197 + 11.198 + Type[] referenceTypes = { 11.199 + subst(c1, 11.200 + Mapping(c1.getTypeArguments().head, predef.stringType)), 11.201 + c2, 11.202 + c3, 11.203 + fac.TypeVariable() }; 11.204 + 11.205 + Type[] arrayTypes = new Type[primitiveTypes.length + referenceTypes.length]; 11.206 + int idx = 0; 11.207 + for (Type t : join(Type.class, primitiveTypes, referenceTypes)) { 11.208 + arrayTypes[idx++] = fac.Array(t); 11.209 + } 11.210 + 11.211 + types1 = join(Type.class, primitiveTypes, referenceTypes, arrayTypes); 11.212 + 11.213 + types2 = new Type[] { 11.214 + predef.byteType, 11.215 + predef.shortType, 11.216 + fac.Constant((short)0x0001), 11.217 + fac.Constant((short)0x0100), 11.218 + predef.intType, 11.219 + fac.Constant((int)0x0000_0001), 11.220 + fac.Constant((int)0x0000_0100), 11.221 + fac.Constant((int)0x0001_0000), 11.222 + predef.longType, 11.223 + fac.Constant((long)0x0000_0000_0000_0001L), 11.224 + fac.Constant((long)0x0000_0000_0000_0100L), 11.225 + fac.Constant((long)0x0000_0000_0001_0000L), 11.226 + fac.Constant((long)0x0001_0000_0000_0000L), 11.227 + predef.floatType, 11.228 + fac.Constant((float)0x0000_0001), 11.229 + fac.Constant((float)0x0000_0100), 11.230 + fac.Constant((float)0x0001_0000), 11.231 + predef.doubleType, 11.232 + fac.Constant((double)0x0000_0000_0000_0001L), 11.233 + fac.Constant((double)0x0000_0000_0000_0100L), 11.234 + fac.Constant((double)0x0000_0000_0001_0000L), 11.235 + fac.Constant((double)0x0001_0000_0000_0000L), 11.236 + predef.charType, 11.237 + fac.Constant((char)0x0001), 11.238 + fac.Constant((char)0x0100), 11.239 + predef.booleanType 11.240 + }; 11.241 + 11.242 + types3 = primitiveTypes; 11.243 + } 11.244 + 11.245 + void testConversion(ConversionKind convKind, TestKind testKind) { 11.246 + Type[] rows = testKind.getFromTypes(this); 11.247 + Type[] cols = testKind.getToTypes(this); 11.248 + for (int i = 0; i < rows.length ; i++) { 11.249 + for (int j = 0; j < cols.length ; j++) { 11.250 + convKind.check(this, rows[i], cols[j], testKind.getResults(this)[i][j]); 11.251 + } 11.252 + } 11.253 + } 11.254 + 11.255 + @SuppressWarnings("unchecked") 11.256 + <T> T[] join(Class<T> type, T[]... args) { 11.257 + int totalLength = 0; 11.258 + for (T[] arr : args) { 11.259 + totalLength += arr.length; 11.260 + } 11.261 + T[] new_arr = (T[])Array.newInstance(type, totalLength); 11.262 + int idx = 0; 11.263 + for (T[] arr : args) { 11.264 + System.arraycopy(arr, 0, new_arr, idx, arr.length); 11.265 + idx += arr.length; 11.266 + } 11.267 + return new_arr; 11.268 + } 11.269 + 11.270 + public static void main(String[] args) { 11.271 + PrimitiveConversionTest harness = new PrimitiveConversionTest(); 11.272 + for (ConversionKind convKind : ConversionKind.values()) { 11.273 + for (TestKind testKind : TestKind.values()) { 11.274 + harness.testConversion(convKind, testKind); 11.275 + } 11.276 + } 11.277 + } 11.278 +}
12.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 12.2 +++ b/test/tools/javac/types/TypeHarness.java Mon Dec 13 14:08:01 2010 -0800 12.3 @@ -0,0 +1,315 @@ 12.4 +/* 12.5 + * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved. 12.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 12.7 + * 12.8 + * This code is free software; you can redistribute it and/or modify it 12.9 + * under the terms of the GNU General Public License version 2 only, as 12.10 + * published by the Free Software Foundation. 12.11 + * 12.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 12.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12.15 + * version 2 for more details (a copy is included in the LICENSE file that 12.16 + * accompanied this code). 12.17 + * 12.18 + * You should have received a copy of the GNU General Public License version 12.19 + * 2 along with this work; if not, write to the Free Software Foundation, 12.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 12.21 + * 12.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 12.23 + * or visit www.oracle.com if you need additional information or have any 12.24 + * questions. 12.25 + */ 12.26 + 12.27 +import com.sun.tools.javac.code.BoundKind; 12.28 +import com.sun.tools.javac.code.Flags; 12.29 +import com.sun.tools.javac.util.Context; 12.30 +import com.sun.tools.javac.code.Types; 12.31 +import com.sun.tools.javac.code.Symtab; 12.32 +import com.sun.tools.javac.code.Type; 12.33 +import com.sun.tools.javac.code.Type.*; 12.34 +import com.sun.tools.javac.code.Symbol.*; 12.35 +import com.sun.tools.javac.util.List; 12.36 +import com.sun.tools.javac.util.ListBuffer; 12.37 +import com.sun.tools.javac.util.Name; 12.38 +import com.sun.tools.javac.util.Names; 12.39 +import com.sun.tools.javac.file.JavacFileManager; 12.40 + 12.41 +/** 12.42 + * Test harness whose goal is to simplify the task of writing type-system 12.43 + * regression test. It provides functionalities to build custom types as well 12.44 + * as to access the underlying javac's symbol table in order to retrieve 12.45 + * predefined types. Among the features supported by the harness are: type 12.46 + * substitution, type containment, subtyping, cast-conversion, assigment 12.47 + * conversion. 12.48 + * 12.49 + * This class is meant to be a common super class for all concrete type test 12.50 + * classes. A subclass can access the type-factory and the test methods so as 12.51 + * to write compact tests. An example is reported below: 12.52 + * 12.53 + * <pre> 12.54 + * Type X = fac.TypeVariable(); 12.55 + * Type Y = fac.TypeVariable(); 12.56 + * Type A_X_Y = fac.Class(0, X, Y); 12.57 + * Type A_Obj_Obj = fac.Class(0, 12.58 + * predef.objectType, 12.59 + * predef.objectType); 12.60 + * checkSameType(A_Obj_Obj, subst(A_X_Y, 12.61 + * Mapping(X, predef.objectType), 12.62 + * Mapping(Y, predef.objectType))); 12.63 + * </pre> 12.64 + * 12.65 + * The above code is used to create two class types, namely {@code A<X,Y>} and 12.66 + * {@code A<Object,Object>} where both {@code X} and {@code Y} are type-variables. 12.67 + * The code then verifies that {@code [X:=Object,Y:=Object]A<X,Y> == A<Object,Object>}. 12.68 + * 12.69 + * @author mcimadamore 12.70 + */ 12.71 +public class TypeHarness { 12.72 + 12.73 + protected Types types; 12.74 + protected Symtab predef; 12.75 + protected Names names; 12.76 + protected Factory fac; 12.77 + 12.78 + protected TypeHarness() { 12.79 + Context ctx = new Context(); 12.80 + JavacFileManager.preRegister(ctx); 12.81 + types = Types.instance(ctx); 12.82 + predef = Symtab.instance(ctx); 12.83 + names = Names.instance(ctx); 12.84 + fac = new Factory(); 12.85 + } 12.86 + 12.87 + // <editor-fold defaultstate="collapsed" desc="type assertions"> 12.88 + 12.89 + /** assert that 's' is a subtype of 't' */ 12.90 + public void assertSubtype(Type s, Type t) { 12.91 + assertSubtype(s, t, true); 12.92 + } 12.93 + 12.94 + /** assert that 's' is/is not a subtype of 't' */ 12.95 + public void assertSubtype(Type s, Type t, boolean expected) { 12.96 + if (types.isSubtype(s, t) != expected) { 12.97 + String msg = expected ? 12.98 + " is not a subtype of " : 12.99 + " is a subtype of "; 12.100 + error(s + msg + t); 12.101 + } 12.102 + } 12.103 + 12.104 + /** assert that 's' is the same type as 't' */ 12.105 + public void assertSameType(Type s, Type t) { 12.106 + assertSameType(s, t, true); 12.107 + } 12.108 + 12.109 + /** assert that 's' is/is not the same type as 't' */ 12.110 + public void assertSameType(Type s, Type t, boolean expected) { 12.111 + if (types.isSameType(s, t) != expected) { 12.112 + String msg = expected ? 12.113 + " is not the same type as " : 12.114 + " is the same type as "; 12.115 + error(s + msg + t); 12.116 + } 12.117 + } 12.118 + 12.119 + /** assert that 's' is castable to 't' */ 12.120 + public void assertCastable(Type s, Type t) { 12.121 + assertCastable(s, t, true); 12.122 + } 12.123 + 12.124 + /** assert that 's' is/is not castable to 't' */ 12.125 + public void assertCastable(Type s, Type t, boolean expected) { 12.126 + if (types.isCastable(s, t) != expected) { 12.127 + String msg = expected ? 12.128 + " is not castable to " : 12.129 + " is castable to "; 12.130 + error(s + msg + t); 12.131 + } 12.132 + } 12.133 + 12.134 + /** assert that 's' is convertible (method invocation conversion) to 't' */ 12.135 + public void assertConvertible(Type s, Type t) { 12.136 + assertCastable(s, t, true); 12.137 + } 12.138 + 12.139 + /** assert that 's' is/is not convertible (method invocation conversion) to 't' */ 12.140 + public void assertConvertible(Type s, Type t, boolean expected) { 12.141 + if (types.isConvertible(s, t) != expected) { 12.142 + String msg = expected ? 12.143 + " is not convertible to " : 12.144 + " is convertible to "; 12.145 + error(s + msg + t); 12.146 + } 12.147 + } 12.148 + 12.149 + /** assert that 's' is assignable to 't' */ 12.150 + public void assertAssignable(Type s, Type t) { 12.151 + assertCastable(s, t, true); 12.152 + } 12.153 + 12.154 + /** assert that 's' is/is not assignable to 't' */ 12.155 + public void assertAssignable(Type s, Type t, boolean expected) { 12.156 + if (types.isAssignable(s, t) != expected) { 12.157 + String msg = expected ? 12.158 + " is not assignable to " : 12.159 + " is assignable to "; 12.160 + error(s + msg + t); 12.161 + } 12.162 + } 12.163 + // </editor-fold> 12.164 + 12.165 + private void error(String msg) { 12.166 + throw new AssertionError("Unexpected result: " + msg); 12.167 + } 12.168 + 12.169 + // <editor-fold defaultstate="collapsed" desc="type functions"> 12.170 + 12.171 + /** compute the erasure of a type 't' */ 12.172 + public Type erasure(Type t) { 12.173 + return types.erasure(t); 12.174 + } 12.175 + 12.176 + /** compute the capture of a type 't' */ 12.177 + public Type capture(Type t) { 12.178 + return types.capture(t); 12.179 + } 12.180 + 12.181 + /** compute the boxed type associated with 't' */ 12.182 + public Type box(Type t) { 12.183 + if (!t.isPrimitive()) { 12.184 + throw new AssertionError("Cannot box non-primitive type: " + t); 12.185 + } 12.186 + return types.boxedClass(t).type; 12.187 + } 12.188 + 12.189 + /** compute the unboxed type associated with 't' */ 12.190 + public Type unbox(Type t) { 12.191 + Type u = types.unboxedType(t); 12.192 + if (t == null) { 12.193 + throw new AssertionError("Cannot unbox reference type: " + t); 12.194 + } else { 12.195 + return u; 12.196 + } 12.197 + } 12.198 + 12.199 + /** compute a type substitution on 't' given a list of type mappings */ 12.200 + public Type subst(Type t, Mapping... maps) { 12.201 + ListBuffer<Type> from = ListBuffer.lb(); 12.202 + ListBuffer<Type> to = ListBuffer.lb(); 12.203 + for (Mapping tm : maps) { 12.204 + from.append(tm.from); 12.205 + to.append(tm.to); 12.206 + } 12.207 + return types.subst(t, from.toList(), to.toList()); 12.208 + } 12.209 + 12.210 + /** create a fresh type mapping from a type to another */ 12.211 + public Mapping Mapping(Type from, Type to) { 12.212 + return new Mapping(from, to); 12.213 + } 12.214 + 12.215 + public static class Mapping { 12.216 + Type from; 12.217 + Type to; 12.218 + private Mapping(Type from, Type to) { 12.219 + this.from = from; 12.220 + this.to = to; 12.221 + } 12.222 + } 12.223 + // </editor-fold> 12.224 + 12.225 + // <editor-fold defaultstate="collapsed" desc="type factory"> 12.226 + 12.227 + /** 12.228 + * This class is used to create Java types in a simple way. All main 12.229 + * kinds of type are supported: primitive, reference, non-denotable. The 12.230 + * factory also supports creation of constant types (used by the compiler 12.231 + * to represent the type of a literal). 12.232 + */ 12.233 + public class Factory { 12.234 + 12.235 + private int synthNameCount = 0; 12.236 + 12.237 + private Name syntheticName() { 12.238 + return names.fromString("A$" + synthNameCount++); 12.239 + } 12.240 + 12.241 + public ClassType Class(long flags, Type... typeArgs) { 12.242 + ClassSymbol csym = new ClassSymbol(flags, syntheticName(), predef.noSymbol); 12.243 + csym.type = new ClassType(Type.noType, List.from(typeArgs), csym); 12.244 + ((ClassType)csym.type).supertype_field = predef.objectType; 12.245 + return (ClassType)csym.type; 12.246 + } 12.247 + 12.248 + public ClassType Class(Type... typeArgs) { 12.249 + return Class(0, typeArgs); 12.250 + } 12.251 + 12.252 + public ClassType Interface(Type... typeArgs) { 12.253 + return Class(Flags.INTERFACE, typeArgs); 12.254 + } 12.255 + 12.256 + public ClassType Interface(long flags, Type... typeArgs) { 12.257 + return Class(Flags.INTERFACE | flags, typeArgs); 12.258 + } 12.259 + 12.260 + public Type Constant(byte b) { 12.261 + return predef.byteType.constType(b); 12.262 + } 12.263 + 12.264 + public Type Constant(short s) { 12.265 + return predef.shortType.constType(s); 12.266 + } 12.267 + 12.268 + public Type Constant(int i) { 12.269 + return predef.intType.constType(i); 12.270 + } 12.271 + 12.272 + public Type Constant(long l) { 12.273 + return predef.longType.constType(l); 12.274 + } 12.275 + 12.276 + public Type Constant(float f) { 12.277 + return predef.floatType.constType(f); 12.278 + } 12.279 + 12.280 + public Type Constant(double d) { 12.281 + return predef.doubleType.constType(d); 12.282 + } 12.283 + 12.284 + public Type Constant(char c) { 12.285 + return predef.charType.constType(c + 0); 12.286 + } 12.287 + 12.288 + public ArrayType Array(Type elemType) { 12.289 + return new ArrayType(elemType, predef.arrayClass); 12.290 + } 12.291 + 12.292 + public TypeVar TypeVariable() { 12.293 + return TypeVariable(predef.objectType); 12.294 + } 12.295 + 12.296 + public TypeVar TypeVariable(Type bound) { 12.297 + TypeSymbol tvsym = new TypeSymbol(0, syntheticName(), null, predef.noSymbol); 12.298 + tvsym.type = new TypeVar(tvsym, bound, null); 12.299 + return (TypeVar)tvsym.type; 12.300 + } 12.301 + 12.302 + public WildcardType Wildcard(BoundKind bk, Type bound) { 12.303 + return new WildcardType(bound, bk, predef.boundClass); 12.304 + } 12.305 + 12.306 + public CapturedType CapturedVariable(Type upper, Type lower) { 12.307 + return new CapturedType(syntheticName(), predef.noSymbol, upper, lower, null); 12.308 + } 12.309 + 12.310 + public ClassType Intersection(Type classBound, Type... intfBounds) { 12.311 + ClassType ct = Class(Flags.COMPOUND); 12.312 + ct.supertype_field = classBound; 12.313 + ct.interfaces_field = List.from(intfBounds); 12.314 + return ct; 12.315 + } 12.316 + } 12.317 + // </editor-fold> 12.318 +}