Mon, 21 May 2012 11:44:49 -0700
Merge
1.1 --- a/src/share/classes/com/sun/tools/javac/comp/Lower.java Thu May 17 06:20:51 2012 -0700 1.2 +++ b/src/share/classes/com/sun/tools/javac/comp/Lower.java Mon May 21 11:44:49 2012 -0700 1.3 @@ -1606,6 +1606,11 @@ 1.4 } 1.5 1.6 private JCStatement makeResourceCloseInvocation(JCExpression resource) { 1.7 + // convert to AutoCloseable if needed 1.8 + if (types.asSuper(resource.type, syms.autoCloseableType.tsym) == null) { 1.9 + resource = (JCExpression) convert(resource, syms.autoCloseableType); 1.10 + } 1.11 + 1.12 // create resource.close() method invocation 1.13 JCExpression resourceClose = makeCall(resource, 1.14 names.close,
2.1 --- a/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Thu May 17 06:20:51 2012 -0700 2.2 +++ b/src/share/classes/com/sun/tools/javac/parser/JavacParser.java Mon May 21 11:44:49 2012 -0700 2.3 @@ -2206,10 +2206,15 @@ 2.4 } else { 2.5 JCExpression t = term(EXPR | TYPE); 2.6 if ((lastmode & TYPE) != 0 && 2.7 - (token.kind == IDENTIFIER || token.kind == ASSERT || token.kind == ENUM)) 2.8 + (token.kind == IDENTIFIER || token.kind == ASSERT || 2.9 + token.kind == ENUM)) { 2.10 return variableDeclarators(modifiersOpt(), t, stats).toList(); 2.11 - else 2.12 + } else if ((lastmode & TYPE) != 0 && token.kind == COLON) { 2.13 + error(pos, "bad.initializer", "for-loop"); 2.14 + return List.of((JCStatement)F.at(pos).VarDef(null, null, t, null)); 2.15 + } else { 2.16 return moreStatementExpressions(pos, t, stats).toList(); 2.17 + } 2.18 } 2.19 } 2.20
3.1 --- a/src/share/classes/com/sun/tools/javac/processing/JavacMessager.java Thu May 17 06:20:51 2012 -0700 3.2 +++ b/src/share/classes/com/sun/tools/javac/processing/JavacMessager.java Mon May 21 11:44:49 2012 -0700 3.3 @@ -1,5 +1,5 @@ 3.4 /* 3.5 - * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 3.6 + * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved. 3.7 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3.8 * 3.9 * This code is free software; you can redistribute it and/or modify it 3.10 @@ -99,6 +99,7 @@ 3.11 if (treeTop != null) { 3.12 newSource = treeTop.snd.sourcefile; 3.13 if (newSource != null) { 3.14 + // save the old version and reinstate it later 3.15 oldSource = log.useSource(newSource); 3.16 pos = treeTop.fst.pos(); 3.17 } 3.18 @@ -131,7 +132,8 @@ 3.19 break; 3.20 } 3.21 } finally { 3.22 - if (oldSource != null) 3.23 + // reinstate the saved version, only if it was saved earlier 3.24 + if (newSource != null) 3.25 log.useSource(oldSource); 3.26 } 3.27 }
4.1 --- a/src/share/classes/com/sun/tools/javac/resources/compiler.properties Thu May 17 06:20:51 2012 -0700 4.2 +++ b/src/share/classes/com/sun/tools/javac/resources/compiler.properties Mon May 21 11:44:49 2012 -0700 4.3 @@ -137,6 +137,10 @@ 4.4 compiler.err.attribute.value.must.be.constant=\ 4.5 attribute value must be constant 4.6 4.7 +# 0: statement type 4.8 +compiler.err.bad.initializer=\ 4.9 + bad initializer for {0} 4.10 + 4.11 compiler.err.break.outside.switch.loop=\ 4.12 break outside switch or loop 4.13
5.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 5.2 +++ b/test/tools/javac/TryWithResources/T7164542.java Mon May 21 11:44:49 2012 -0700 5.3 @@ -0,0 +1,44 @@ 5.4 +/* 5.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 5.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5.7 + * 5.8 + * This code is free software; you can redistribute it and/or modify it 5.9 + * under the terms of the GNU General Public License version 2 only, as 5.10 + * published by the Free Software Foundation. 5.11 + * 5.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 5.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 5.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 5.15 + * version 2 for more details (a copy is included in the LICENSE file that 5.16 + * accompanied this code). 5.17 + * 5.18 + * You should have received a copy of the GNU General Public License version 5.19 + * 2 along with this work; if not, write to the Free Software Foundation, 5.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 5.21 + * 5.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 5.23 + * or visit www.oracle.com if you need additional information or have any 5.24 + * questions. 5.25 + */ 5.26 + 5.27 +/* 5.28 + * @test 5.29 + * @bug 7164542 5.30 + * @summary try-with-resources: problem with intersection types 5.31 + * @compile T7164542.java 5.32 + */ 5.33 + 5.34 +public class T7164542 { 5.35 + public static <S extends Readable & AutoCloseable, 5.36 + T extends Appendable & AutoCloseable> 5.37 + void copy(S s, T t, int size) throws Exception { 5.38 + /* 5.39 + * compiler used to fail here with: 5.40 + * symbol: method close() 5.41 + * location: interface Readable 5.42 + * Fatal Error: Unable to find method close 5.43 + */ 5.44 + try (S src = s; T trg = t) { 5.45 + } 5.46 + } 5.47 +}
6.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 6.2 +++ b/test/tools/javac/diags/examples/ForeachBadInitialization.java Mon May 21 11:44:49 2012 -0700 6.3 @@ -0,0 +1,31 @@ 6.4 +/* 6.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 6.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 6.7 + * 6.8 + * This code is free software; you can redistribute it and/or modify it 6.9 + * under the terms of the GNU General Public License version 2 only, as 6.10 + * published by the Free Software Foundation. 6.11 + * 6.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 6.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 6.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 6.15 + * version 2 for more details (a copy is included in the LICENSE file that 6.16 + * accompanied this code). 6.17 + * 6.18 + * You should have received a copy of the GNU General Public License version 6.19 + * 2 along with this work; if not, write to the Free Software Foundation, 6.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 6.21 + * 6.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 6.23 + * or visit www.oracle.com if you need additional information or have any 6.24 + * questions. 6.25 + */ 6.26 + 6.27 +// key: compiler.err.bad.initializer 6.28 +import java.util.List; 6.29 +class ForeachBadInitialization { 6.30 + void m() { 6.31 + List<String> s = null; 6.32 + for (a : s) {} 6.33 + } 6.34 +}
7.1 --- a/test/tools/javac/parser/JavacParserTest.java Thu May 17 06:20:51 2012 -0700 7.2 +++ b/test/tools/javac/parser/JavacParserTest.java Mon May 21 11:44:49 2012 -0700 7.3 @@ -23,7 +23,7 @@ 7.4 7.5 /* 7.6 * @test 7.7 - * @bug 7073631 7.8 + * @bug 7073631 7159445 7.9 * @summary tests error and diagnostics positions 7.10 * @author Jan Lahoda 7.11 */ 7.12 @@ -875,6 +875,7 @@ 7.13 testMissingClassError(); 7.14 testSwitchError(); 7.15 testMethodError(); 7.16 + testErrorRecoveryForEnhancedForLoop142381(); 7.17 } 7.18 7.19 public static void main(String... args) throws IOException { 7.20 @@ -892,8 +893,10 @@ 7.21 } 7.22 } 7.23 7.24 - void assertFalse(String message, boolean empty) { 7.25 - throw new UnsupportedOperationException("Not yet implemented"); 7.26 + void assertFalse(String message, boolean bvalue) { 7.27 + if (bvalue == true) { 7.28 + fail(message); 7.29 + } 7.30 } 7.31 7.32 void assertEquals(String message, int i, long l) {
8.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 8.2 +++ b/test/tools/javac/processing/messager/MessagerDiags.java Mon May 21 11:44:49 2012 -0700 8.3 @@ -0,0 +1,131 @@ 8.4 +/* 8.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. 8.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 8.7 + * 8.8 + * This code is free software; you can redistribute it and/or modify it 8.9 + * under the terms of the GNU General Public License version 2 only, as 8.10 + * published by the Free Software Foundation. Oracle designates this 8.11 + * particular file as subject to the "Classpath" exception as provided 8.12 + * by Oracle in the LICENSE file that accompanied this code. 8.13 + * 8.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 8.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 8.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 8.17 + * version 2 for more details (a copy is included in the LICENSE file that 8.18 + * accompanied this code). 8.19 + * 8.20 + * You should have received a copy of the GNU General Public License version 8.21 + * 2 along with this work; if not, write to the Free Software Foundation, 8.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 8.23 + * 8.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 8.25 + * or visit www.oracle.com if you need additional information or have any 8.26 + * questions. 8.27 + */ 8.28 + 8.29 +/* 8.30 + * @test 8.31 + * @bug 7166010 8.32 + * @summary warnings printed by annotation processors uses incorrect source 8.33 + */ 8.34 +import com.sun.source.util.JavacTask; 8.35 +import java.io.IOException; 8.36 +import java.net.URI; 8.37 +import java.util.ArrayList; 8.38 +import java.util.Arrays; 8.39 +import java.util.LinkedList; 8.40 +import java.util.List; 8.41 +import java.util.Set; 8.42 +import javax.annotation.processing.AbstractProcessor; 8.43 +import javax.annotation.processing.Messager; 8.44 +import javax.annotation.processing.RoundEnvironment; 8.45 +import javax.annotation.processing.SupportedAnnotationTypes; 8.46 +import javax.annotation.processing.SupportedSourceVersion; 8.47 +import javax.lang.model.SourceVersion; 8.48 +import javax.lang.model.element.Element; 8.49 +import javax.lang.model.element.TypeElement; 8.50 +import javax.tools.Diagnostic; 8.51 +import javax.tools.DiagnosticCollector; 8.52 +import javax.tools.JavaCompiler; 8.53 +import javax.tools.JavaFileObject; 8.54 +import javax.tools.SimpleJavaFileObject; 8.55 +import javax.tools.ToolProvider; 8.56 + 8.57 +import static javax.tools.Diagnostic.Kind.*; 8.58 +import static javax.tools.JavaFileObject.Kind.*; 8.59 + 8.60 +@SupportedSourceVersion(SourceVersion.RELEASE_6) 8.61 +@SupportedAnnotationTypes("*") 8.62 +public class MessagerDiags extends AbstractProcessor { 8.63 + static final String CNAME = "Test"; 8.64 + static final String TEST_JAVA = CNAME + ".java"; 8.65 + static final String TEST_JAVA_URI_NAME = "myfo:/" + TEST_JAVA; 8.66 + static final String WRN_NO_SOURCE = "warning without source"; 8.67 + static final String WRN_WITH_SOURCE = "warning with source"; 8.68 + static final String NONE = "<none>"; 8.69 + static final String[] EXPECTED = { NONE + ":-1--1:" + WRN_NO_SOURCE, 8.70 + TEST_JAVA + ":0-13:" + WRN_WITH_SOURCE, 8.71 + NONE + ":-1--1:" + WRN_NO_SOURCE 8.72 + }; 8.73 + 8.74 + @Override 8.75 + public boolean process(Set<? extends TypeElement> annotations, 8.76 + RoundEnvironment roundEnv) { 8.77 + Messager messager = processingEnv.getMessager(); 8.78 + for (Element e : roundEnv.getRootElements()) { 8.79 + messager.printMessage(WARNING, WRN_NO_SOURCE); 8.80 + messager.printMessage(WARNING, WRN_WITH_SOURCE, e); 8.81 + messager.printMessage(WARNING, WRN_NO_SOURCE); 8.82 + } 8.83 + return false; 8.84 + } 8.85 + 8.86 + public static void main(String... args) throws IOException { 8.87 + final String bootPath = System.getProperty("sun.boot.class.path"); 8.88 + final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); 8.89 + assert tool != null; 8.90 + 8.91 + DiagnosticCollector<JavaFileObject> dc = new DiagnosticCollector<>(); 8.92 + List<String> options = new LinkedList<>(); 8.93 + options.addAll(Arrays.asList("-bootclasspath", bootPath, 8.94 + "-source", "1.6", "-classpath", 8.95 + System.getProperty("java.class.path"))); 8.96 + options.addAll(Arrays.asList("-processor", 8.97 + MessagerDiags.class.getName())); 8.98 + JavacTask ct = (JavacTask)tool.getTask(null, null, dc, options, null, 8.99 + Arrays.asList(new MyFileObject("class " + CNAME + " {}"))); 8.100 + ct.analyze(); 8.101 + 8.102 + List<String> obtainedErrors = new ArrayList<>(); 8.103 + 8.104 + for (Diagnostic<? extends JavaFileObject> d : dc.getDiagnostics()) { 8.105 + String dSource; 8.106 + if (d.getSource() != null) { 8.107 + dSource = d.getSource().toUri().getPath(); 8.108 + dSource = dSource.substring(dSource.lastIndexOf('/') + 1); 8.109 + } else { 8.110 + dSource = NONE; 8.111 + } 8.112 + obtainedErrors.add(dSource + ":" + d.getStartPosition() + "-" + 8.113 + d.getEndPosition() + ":" + d.getMessage(null)); 8.114 + } 8.115 + List<String> expectedErrors = Arrays.asList(EXPECTED); 8.116 + if (!expectedErrors.equals(obtainedErrors)) { 8.117 + System.err.println("Expected: " + expectedErrors); 8.118 + System.err.println("Obtained: " + obtainedErrors); 8.119 + throw new AssertionError("Messages don't match"); 8.120 + } 8.121 + } 8.122 + 8.123 + static class MyFileObject extends SimpleJavaFileObject { 8.124 + private String text; 8.125 + public MyFileObject(String text) { 8.126 + super(URI.create(TEST_JAVA_URI_NAME), SOURCE); 8.127 + this.text = text; 8.128 + } 8.129 + @Override 8.130 + public CharSequence getCharContent(boolean ignoreEncodingErrors) { 8.131 + return text; 8.132 + } 8.133 + } 8.134 +}