AvoidInfiniteReattribution.java revision 2895:ec37a85dbd97
1/* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * @test 26 * @bug 8077605 27 * @summary Check that when an exception occurs during Attr.visitLambda, an attempt to attribute 28 the lambda again is avoided rather than falling into an infinite recursion. 29 */ 30 31import java.io.IOException; 32import java.net.URI; 33import java.util.Arrays; 34import java.util.List; 35import javax.tools.JavaFileObject; 36import javax.tools.SimpleJavaFileObject; 37 38import com.sun.tools.javac.api.JavacTaskImpl; 39import com.sun.tools.javac.api.JavacTool; 40import com.sun.tools.javac.comp.Attr; 41import com.sun.tools.javac.tree.JCTree.JCVariableDecl; 42import com.sun.tools.javac.util.Context; 43import com.sun.tools.javac.util.Context.Factory; 44 45public class AvoidInfiniteReattribution { 46 47 public static void main(String... args) throws Exception { 48 new AvoidInfiniteReattribution().run(); 49 } 50 51 void run() throws IOException { 52 JavacTool tool = JavacTool.create(); 53 JavaSource source = new JavaSource("class Test {" + 54 " I i = STOP -> {};" + 55 " interface I {" + 56 " public void test(int i) {}" + 57 " }" + 58 "}"); 59 Context context = new Context(); 60 CrashingAttr.preRegister(context); 61 List<JavaSource> inputs = Arrays.asList(source); 62 JavacTaskImpl task = 63 (JavacTaskImpl) tool.getTask(null, null, null, null, null, inputs, context); 64 try { 65 task.analyze(null); 66 throw new AssertionError("Expected exception not seen."); 67 } catch (StopException ex) { 68 //ok 69 } 70 } 71 72 static class CrashingAttr extends Attr { 73 74 static void preRegister(Context context) { 75 context.put(attrKey, (Factory<Attr>) c -> new CrashingAttr(c)); 76 } 77 78 CrashingAttr(Context context) { 79 super(context); 80 } 81 82 @Override public void visitVarDef(JCVariableDecl tree) { 83 if (tree.name.contentEquals("STOP")) 84 throw new StopException(); 85 super.visitVarDef(tree); 86 } 87 } 88 89 static class StopException extends NullPointerException {} 90 91 class JavaSource extends SimpleJavaFileObject { 92 93 String source; 94 95 JavaSource(String source) { 96 super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); 97 this.source = source; 98 } 99 100 @Override 101 public CharSequence getCharContent(boolean ignoreEncodingErrors) { 102 return source; 103 } 104 105 } 106 107} 108