Skip to content

Commit ada3492

Browse files
authored
Merge pull request oracle#4 from JaroslavTulach/jtulach/PrimitiveTypeSwitch
Upgrading to 18+36 build
2 parents a543a2e + f5e8beb commit ada3492

File tree

4 files changed

+200
-1
lines changed

4 files changed

+200
-1
lines changed

make/langtools/netbeans/nb-javac/nbproject/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
jdk.git.url=https://github.com/openjdk/jdk
3-
jdk.git.commit=jdk-18+25
3+
jdk.git.commit=jdk-18+36
44
nb-javac-ver=${jdk.git.commit}
55

66
debug.modulepath=\

make/langtools/netbeans/nb-javac/src/META-INF/upgrade/nbjavac.hint

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,9 @@ jdk.internal.javac.NoPreview
247247
=>
248248
java.lang.VirtualMachineError
249249
;;
250+
251+
// fallback to highest available ct.sym
252+
com.sun.tools.javac.platform.PlatformUtils.lookupPlatformDescription($s)
253+
=>
254+
nbjavac.VMWrapper.lookupPlatformDescriptionWithFallback($s)
255+
;;

make/langtools/netbeans/nb-javac/src/nbjavac/VMWrapper.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
*/
2525
package nbjavac;
2626

27+
import com.sun.source.util.Plugin;
28+
import com.sun.tools.javac.platform.PlatformDescription;
29+
import com.sun.tools.javac.platform.PlatformUtils;
2730
import java.io.IOException;
2831
import java.net.JarURLConnection;
2932
import java.net.URISyntaxException;
@@ -45,6 +48,8 @@
4548
import java.util.Iterator;
4649
import java.util.List;
4750
import java.util.Set;
51+
import javax.annotation.processing.Processor;
52+
import javax.tools.JavaFileManager;
4853

4954
public class VMWrapper {
5055
private VMWrapper() {
@@ -177,4 +182,70 @@ public WatchService newWatchService() throws IOException {
177182
}
178183
};
179184
}
185+
186+
public static PlatformDescription lookupPlatformDescriptionWithFallback(String version) {
187+
PlatformDescription pd = PlatformUtils.lookupPlatformDescription(version);
188+
if (pd != null) {
189+
return pd;
190+
}
191+
192+
PlatformDescription fallback = findFallbackPlatform(version);
193+
return new DelegatingPlatformDescription(version, fallback);
194+
}
195+
196+
private static PlatformDescription findFallbackPlatform(String version) throws NullPointerException, NumberFormatException {
197+
PlatformDescription fallback = null;
198+
for (int numVersion = Integer.valueOf(version); fallback == null; numVersion--) {
199+
fallback = PlatformUtils.lookupPlatformDescription("" + numVersion);
200+
}
201+
if (fallback == null) {
202+
throw new NullPointerException("No platform found for " + version);
203+
}
204+
return fallback;
205+
}
206+
207+
private static class DelegatingPlatformDescription implements PlatformDescription {
208+
private final PlatformDescription delegate;
209+
private final String version;
210+
211+
DelegatingPlatformDescription(String version, PlatformDescription fallback) {
212+
this.delegate = fallback;
213+
this.version = version;
214+
}
215+
216+
@Override
217+
public JavaFileManager getFileManager() {
218+
return delegate.getFileManager();
219+
}
220+
221+
@Override
222+
public String getSourceVersion() {
223+
return version;
224+
}
225+
226+
@Override
227+
public String getTargetVersion() {
228+
return version;
229+
}
230+
231+
@Override
232+
public List<PlatformDescription.PluginInfo<Processor>> getAnnotationProcessors() {
233+
return delegate.getAnnotationProcessors();
234+
}
235+
236+
@Override
237+
public List<PlatformDescription.PluginInfo<Plugin>> getPlugins() {
238+
return delegate.getPlugins();
239+
}
240+
241+
@Override
242+
public List<String> getAdditionalOptions() {
243+
return delegate.getAdditionalOptions();
244+
}
245+
246+
@Override
247+
public void close() throws IOException {
248+
delegate.close();
249+
}
250+
}
180251
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)