|
| 1 | +/* |
| 2 | + * Copyright 2003-2004 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Sun in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | + * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | + * have any questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package global; |
| 27 | + |
| 28 | +import com.sun.source.util.JavacTask; |
| 29 | +import java.io.ByteArrayOutputStream; |
| 30 | +import java.io.IOException; |
| 31 | +import java.io.OutputStream; |
| 32 | +import java.net.URI; |
| 33 | +import java.util.Arrays; |
| 34 | +import java.util.HashMap; |
| 35 | +import java.util.Iterator; |
| 36 | +import java.util.Map; |
| 37 | +import javax.tools.FileObject; |
| 38 | +import javax.tools.ForwardingJavaFileManager; |
| 39 | +import javax.tools.JavaCompiler; |
| 40 | +import javax.tools.JavaFileManager; |
| 41 | +import javax.tools.JavaFileObject; |
| 42 | +import javax.tools.SimpleJavaFileObject; |
| 43 | +import javax.tools.StandardJavaFileManager; |
| 44 | +import javax.tools.ToolProvider; |
| 45 | +import junit.framework.TestCase; |
| 46 | + |
| 47 | +public class SwitchPrimitiveTypeTest extends TestCase { |
| 48 | + |
| 49 | + public SwitchPrimitiveTypeTest(String name) { |
| 50 | + super(name); |
| 51 | + } |
| 52 | + |
| 53 | + static class MyFileObject extends SimpleJavaFileObject { |
| 54 | + private String text; |
| 55 | + public MyFileObject(String text) { |
| 56 | + super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE); |
| 57 | + this.text = text; |
| 58 | + } |
| 59 | + @Override |
| 60 | + public CharSequence getCharContent(boolean ignoreEncodingErrors) { |
| 61 | + return text; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void testSwitchOnPrimitiveType() throws IOException { |
| 66 | + final JavaCompiler tool = ToolProvider.getSystemJavaCompiler(); |
| 67 | + assert tool != null; |
| 68 | + |
| 69 | + String code = """ |
| 70 | + class X { |
| 71 | + public int checkType(int v) { |
| 72 | + return switch (v) { |
| 73 | + case Integer i -> 40 + i; |
| 74 | + default -> -42; |
| 75 | + }; |
| 76 | + } |
| 77 | +
|
| 78 | + public static void main(String... args) { |
| 79 | + X x = new X(); |
| 80 | + System.out.println(x.checkType(2)); |
| 81 | + } |
| 82 | + } |
| 83 | + """; |
| 84 | + MemoryOutputJFM fm = new MemoryOutputJFM(tool.getStandardFileManager(null, null, null)); |
| 85 | + JavacTask ct = (JavacTask)tool.getTask(null, fm, null, global.Utils.asParameters("--enable-preview", "--release", "18"), null, Arrays.asList(new MyFileObject(code))); |
| 86 | + Iterator<? extends JavaFileObject> out = ct.generate().iterator(); |
| 87 | + |
| 88 | + assertTrue("Contains at least one element", out.hasNext()); |
| 89 | + JavaFileObject xClass = out.next(); |
| 90 | + assertEquals("generated:/X", xClass.toUri().toString()); |
| 91 | + } |
| 92 | + |
| 93 | + private static class MemoryOutputJFM extends ForwardingJavaFileManager<StandardJavaFileManager> { |
| 94 | + |
| 95 | + private final Map<String, byte[]> writtenClasses = new HashMap<String, byte[]>(); |
| 96 | + |
| 97 | + public MemoryOutputJFM(StandardJavaFileManager m) { |
| 98 | + super(m); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, final String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException { |
| 103 | + if (location.isOutputLocation() && kind == JavaFileObject.Kind.CLASS) { |
| 104 | + return new SimpleJavaFileObject(URI.create("generated:/" + className), kind) { |
| 105 | + @Override |
| 106 | + public OutputStream openOutputStream() throws IOException { |
| 107 | + return new ByteArrayOutputStream() { |
| 108 | + @Override public void close() throws IOException { |
| 109 | + super.close(); |
| 110 | + writtenClasses.put(className, toByteArray()); |
| 111 | + } |
| 112 | + }; |
| 113 | + } |
| 114 | + }; |
| 115 | + } else { |
| 116 | + return super.getJavaFileForOutput(location, className, kind, sibling); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + } |
| 121 | + |
| 122 | +} |
0 commit comments