Skip to content

Commit

Permalink
Merge pull request oracle#4 from JaroslavTulach/jtulach/PrimitiveType…
Browse files Browse the repository at this point in the history
…Switch

Upgrading to 18+36 build
  • Loading branch information
jtulach authored Mar 2, 2022
2 parents a543a2e + f5e8beb commit ada3492
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

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

debug.modulepath=\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -247,3 +247,9 @@ jdk.internal.javac.NoPreview
=>
java.lang.VirtualMachineError
;;

// fallback to highest available ct.sym
com.sun.tools.javac.platform.PlatformUtils.lookupPlatformDescription($s)
=>
nbjavac.VMWrapper.lookupPlatformDescriptionWithFallback($s)
;;
71 changes: 71 additions & 0 deletions make/langtools/netbeans/nb-javac/src/nbjavac/VMWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
*/
package nbjavac;

import com.sun.source.util.Plugin;
import com.sun.tools.javac.platform.PlatformDescription;
import com.sun.tools.javac.platform.PlatformUtils;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
Expand All @@ -45,6 +48,8 @@
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import javax.annotation.processing.Processor;
import javax.tools.JavaFileManager;

public class VMWrapper {
private VMWrapper() {
Expand Down Expand Up @@ -177,4 +182,70 @@ public WatchService newWatchService() throws IOException {
}
};
}

public static PlatformDescription lookupPlatformDescriptionWithFallback(String version) {
PlatformDescription pd = PlatformUtils.lookupPlatformDescription(version);
if (pd != null) {
return pd;
}

PlatformDescription fallback = findFallbackPlatform(version);
return new DelegatingPlatformDescription(version, fallback);
}

private static PlatformDescription findFallbackPlatform(String version) throws NullPointerException, NumberFormatException {
PlatformDescription fallback = null;
for (int numVersion = Integer.valueOf(version); fallback == null; numVersion--) {
fallback = PlatformUtils.lookupPlatformDescription("" + numVersion);
}
if (fallback == null) {
throw new NullPointerException("No platform found for " + version);
}
return fallback;
}

private static class DelegatingPlatformDescription implements PlatformDescription {
private final PlatformDescription delegate;
private final String version;

DelegatingPlatformDescription(String version, PlatformDescription fallback) {
this.delegate = fallback;
this.version = version;
}

@Override
public JavaFileManager getFileManager() {
return delegate.getFileManager();
}

@Override
public String getSourceVersion() {
return version;
}

@Override
public String getTargetVersion() {
return version;
}

@Override
public List<PlatformDescription.PluginInfo<Processor>> getAnnotationProcessors() {
return delegate.getAnnotationProcessors();
}

@Override
public List<PlatformDescription.PluginInfo<Plugin>> getPlugins() {
return delegate.getPlugins();
}

@Override
public List<String> getAdditionalOptions() {
return delegate.getAdditionalOptions();
}

@Override
public void close() throws IOException {
delegate.close();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
* CA 95054 USA or visit www.sun.com if you need additional information or
* have any questions.
*/

package global;

import com.sun.source.util.JavacTask;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaCompiler;
import javax.tools.JavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.ToolProvider;
import junit.framework.TestCase;

public class SwitchPrimitiveTypeTest extends TestCase {

public SwitchPrimitiveTypeTest(String name) {
super(name);
}

static class MyFileObject extends SimpleJavaFileObject {
private String text;
public MyFileObject(String text) {
super(URI.create("myfo:/Test.java"), JavaFileObject.Kind.SOURCE);
this.text = text;
}
@Override
public CharSequence getCharContent(boolean ignoreEncodingErrors) {
return text;
}
}

public void testSwitchOnPrimitiveType() throws IOException {
final JavaCompiler tool = ToolProvider.getSystemJavaCompiler();
assert tool != null;

String code = """
class X {
public int checkType(int v) {
return switch (v) {
case Integer i -> 40 + i;
default -> -42;
};
}
public static void main(String... args) {
X x = new X();
System.out.println(x.checkType(2));
}
}
""";
MemoryOutputJFM fm = new MemoryOutputJFM(tool.getStandardFileManager(null, null, null));
JavacTask ct = (JavacTask)tool.getTask(null, fm, null, global.Utils.asParameters("--enable-preview", "--release", "18"), null, Arrays.asList(new MyFileObject(code)));
Iterator<? extends JavaFileObject> out = ct.generate().iterator();

assertTrue("Contains at least one element", out.hasNext());
JavaFileObject xClass = out.next();
assertEquals("generated:/X", xClass.toUri().toString());
}

private static class MemoryOutputJFM extends ForwardingJavaFileManager<StandardJavaFileManager> {

private final Map<String, byte[]> writtenClasses = new HashMap<String, byte[]>();

public MemoryOutputJFM(StandardJavaFileManager m) {
super(m);
}

@Override
public JavaFileObject getJavaFileForOutput(JavaFileManager.Location location, final String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
if (location.isOutputLocation() && kind == JavaFileObject.Kind.CLASS) {
return new SimpleJavaFileObject(URI.create("generated:/" + className), kind) {
@Override
public OutputStream openOutputStream() throws IOException {
return new ByteArrayOutputStream() {
@Override public void close() throws IOException {
super.close();
writtenClasses.put(className, toByteArray());
}
};
}
};
} else {
return super.getJavaFileForOutput(location, className, kind, sibling);
}
}

}

}

0 comments on commit ada3492

Please sign in to comment.