Skip to content

Commit

Permalink
Add support for OpenJDK and Java 11.
Browse files Browse the repository at this point in the history
Squash port of sampottinger:java11 on processing train with a number of sub-changes, resolving processing/processing#5750 in the processing4 train. Original message below.

Moves to OpenJDK11 as part of #5750. There's lots in here so a quick rundown of the bigger stuff...

**Primary required changes:**
Some changes directly support OpenJFX / OpenJDK 11:

 - Response to image loading changes caused by [JEP 320](https://openjdk.java.net/jeps/320)
 - Use of jmodules as necessitated by [JEP 261](https://openjdk.java.net/jeps/261)
 - Reponse to largely changed file paths caused by [JEP 220](https://openjdk.java.net/jeps/220).
 - Modifications in build system related to AdoptOpenJDK and Java 11 which have a different naming structure for downloads.
 - Allowing use of non-Oracle Java within internal Processing checks.

**Secondary required changes:**
There were some secondary required changes that impacted the usability of Processing after having moved to OpenJFX / OpenJDK 11:

 - Removal of com.apple.eawt calls related to [JEP 272](http://openjdk.java.net/jeps/272)
 - Response to HiDPI support on Windows and Linux in [JEP 263](https://openjdk.java.net/jeps/263)
- Removal of `java.ext.dirs`. Would be forced by [JEP 220](http://openjdk.java.net/jeps/220).
 - Due to bugs on Windows, updated the JNA jars.
 - Changes in downloader build tasks to support AdoptOpenJDK and OpenJFX.
 - Updated org.eclipse.* / equinox jars.
 - Some optimization around size of distribution.
 - Update of AppBundler.
 - Some changes in formulation of classpath and modifications in PreprocessingService given [JEP 261](https://openjdk.java.net/jeps/261).

**Incidental changes:**
This was (ahem) a bit of a larger PR with the above modifications. So, I wanted to introduce automated tests when possible and convenient along with a few changes for platform sustainability in order to support development:

 - Addition of cross-building capability (!) made possible by AdoptOpenJDK.
 - Addition of mockito for testing.
 - Upgrade of junit.
 - Addition of ant-contrib.
 - Standardized nomenclature around JRE / JDK in `build/build.xml`
 - Deduplication of code in `jre/build.xml`.
 - Addition of JavaDoc in a few places.
 - Some refactoring of PImage / Shape to support increased testing and readability in image manipulation code.
  • Loading branch information
sampottinger committed Oct 6, 2019
1 parent a429baa commit 428cad1
Show file tree
Hide file tree
Showing 126 changed files with 7,325 additions and 1,690 deletions.
14 changes: 14 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="library/jogl-all.jar"/>
<classpathentry kind="lib" path="library/gluegen-rt.jar"/>
<classpathentry kind="lib" path="apple.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.USER_LIBRARY/JavaFX11"/>
<classpathentry kind="output" path="bin"/>
</classpath>
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.DS_Store
.AppleDouble
*.iml
._*
*~
/build/shared/reference.zip
Expand Down
25 changes: 11 additions & 14 deletions app/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
<fail unless="core-built" message="Please build the core library first and make sure it is located at ../core/library/core.jar" />

<mkdir dir="bin" />

<!-- copy languages files -->
<copy todir="bin">
<fileset dir="src">
<include name="processing/app/languages/*.properties" />
</fileset>
</copy>

<!-- in some cases, pde.jar was not getting built
https://github.com/processing/processing/issues/1792 -->
<delete file="pde.jar" />
Expand All @@ -30,22 +30,19 @@

<javac source="1.8"
target="1.8"
destdir="bin"
excludes="**/tools/format/**"
encoding="UTF-8"
includeAntRuntime="false"
classpath="../core/library/core.jar;
../core/apple.jar;
lib/ant.jar;
lib/ant-launcher.jar;
destdir="bin"
excludes="**/tools/format/**"
encoding="UTF-8"
includeAntRuntime="false"
classpath="../core/library/core.jar;
../core/apple.jar;
lib/ant.jar;
lib/ant-launcher.jar;
lib/jna.jar;
lib/jna-platform.jar"
debug="on"
nowarn="true"
compiler="org.eclipse.jdt.core.JDTCompilerAdapter">
nowarn="true">
<src path="src" />
<compilerclasspath path="../java/mode/org.eclipse.jdt.core.jar;
../java/mode/jdtCompilerAdapter.jar" />
</javac>
</target>

Expand Down
Binary file added app/lib/jna
Binary file not shown.
Binary file modified app/lib/jna-platform.jar
Binary file not shown.
Binary file modified app/lib/jna.jar
Binary file not shown.
6 changes: 3 additions & 3 deletions app/src/processing/app/Platform.java
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,10 @@ static public File getJavaHome() {
File[] plugins = getContentFile("../PlugIns").listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return dir.isDirectory() &&
name.endsWith(".jdk") && !name.startsWith(".");
name.contains("jdk") && !name.startsWith(".");
}
});
return new File(plugins[0], "Contents/Home/jre");
return new File(plugins[0], "Contents/Home");
}
// On all other platforms, it's the 'java' folder adjacent to Processing
return getContentFile("java");
Expand Down Expand Up @@ -412,4 +412,4 @@ static public int unsetenv(String variable) {
static public int getSystemDPI() {
return inst.getSystemDPI();
}
}
}
14 changes: 0 additions & 14 deletions app/src/processing/app/platform/LinuxPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,6 @@ public class LinuxPlatform extends DefaultPlatform {
public void initBase(Base base) {
super.initBase(base);

String javaVendor = System.getProperty("java.vendor");
String javaVM = System.getProperty("java.vm.name");
if (javaVendor == null ||
(!javaVendor.contains("Sun") && !javaVendor.contains("Oracle")) ||
javaVM == null || !javaVM.contains("Java")) {
Messages.showWarning("Not fond of this Java VM",
"Processing requires Java 8 from Oracle.\n" +
"Other versions such as OpenJDK, IcedTea,\n" +
"and GCJ are strongly discouraged. Among other things, you're\n" +
"likely to run into problems with sketch window size and\n" +
"placement. For more background, please read the wiki:\n" +
"https://github.com/processing/processing/wiki/Supported-Platforms#linux", null);
}

// Set x11 WM_CLASS property which is used as the application
// name by Gnome3 and other window managers.
// https://github.com/processing/processing/issues/2534
Expand Down
111 changes: 75 additions & 36 deletions app/src/processing/app/platform/ThinkDifferent.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@

import java.awt.event.*;
import java.io.File;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;

import javax.swing.*;

import com.apple.eawt.*;
import com.apple.eawt.AppEvent.*;
import com.apple.eawt.Application;

import processing.app.*;
import processing.app.ui.About;
Expand Down Expand Up @@ -65,44 +69,40 @@ static protected void init(final Base base) {
if (adapter == null) {
adapter = new ThinkDifferent(); //base);
}

application.setAboutHandler(new AboutHandler() {
public void handleAbout(AboutEvent ae) {
new About(null);
}

setHandler(application, "setAboutHandler", (proxy, method, args) -> {
new About(null);
return null;
});

application.setPreferencesHandler(new PreferencesHandler() {
public void handlePreferences(PreferencesEvent arg0) {
base.handlePrefs();
}

setHandler(application, "setPreferencesHandler", (proxy, method, args) -> {
base.handlePrefs();
return null;
});

application.setOpenFileHandler(new OpenFilesHandler() {
public void openFiles(OpenFilesEvent event) {
for (File file : event.getFiles()) {
base.handleOpen(file.getAbsolutePath());
}
setHandler(application, "setOpenFileHandler", (proxy, method, args) -> {
Method m = args[0].getClass().getMethod("getFiles");
for (File file : (List<File>) m.invoke(args[0])) {
base.handleOpen(file.getAbsolutePath());
}
return null;
});

application.setPrintFileHandler(new PrintFilesHandler() {
public void printFiles(PrintFilesEvent event) {
// TODO not yet implemented
}

setHandler(application, "setPrintFileHandler", (proxy, method, args) -> {
// TODO not yet implemented
return null;
});

application.setQuitHandler(new QuitHandler() {
public void handleQuitRequestWith(QuitEvent event, QuitResponse response) {
if (base.handleQuit()) {
response.performQuit();
} else {
response.cancelQuit();
}

setHandler(application, "setQuitHandler", (proxy, method, args) -> {
if (base.handleQuit()) {
args[1].getClass().getMethod("performQuit").invoke(args[1]);
} else {
args[1].getClass().getMethod("cancelQuit").invoke(args[1]);
}
return null;
});

// Set the menubar to be used when nothing else is open.
// Set the menubar to be used when nothing else is open.
JMenuBar defaultMenuBar = new JMenuBar();
JMenu fileMenu = buildFileMenu(base);
defaultMenuBar.add(fileMenu);
Expand All @@ -117,12 +117,12 @@ public void handleQuitRequestWith(QuitEvent event, QuitResponse response) {
e.printStackTrace(); // oh well, never mind
}
// } else {
// // The douchebags at Oracle didn't feel that a working f*king menubar
// // on OS X was important enough to make it into the 7u40 release.
// // The douchebags at Oracle didn't feel that a working f*king menubar
// // on OS X was important enough to make it into the 7u40 release.
// //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=8007267
// // It languished in the JDK 8 source and has been backported for 7u60:
// //http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=8022667
//
//
// JFrame offscreen = new JFrame();
// offscreen.setUndecorated(true);
// offscreen.setJMenuBar(defaultMenuBar);
Expand All @@ -131,12 +131,51 @@ public void handleQuitRequestWith(QuitEvent event, QuitResponse response) {
// offscreen.setVisible(true);
// }
}


// public ThinkDifferent(Base base) {
// this.base = base;
// }

/**
* Sets a handler on an instance of {@link Application}, taking into account JVM version
* differences.
*
* @param app an instance of {@link Application}
* @param name the "set handler" method name
* @param handler the handler
*/
private static void setHandler(Application app, String name, InvocationHandler handler) {
// Determine which version of com.apple.eawt.Application to use and pass it a handler of the
// appropriate type
Method[] methods = app.getClass().getMethods();
for (Method m : methods) {
if (!name.equals(m.getName())) {
continue;
}
if (m.getParameterCount() != 1) {
continue;
}
Class paramType = m.getParameterTypes()[0];
try {
// Allow a null handler
Object proxy = null;
if (handler != null) {
proxy = Proxy.newProxyInstance(
paramType.getClassLoader(), new Class<?>[] { paramType }, handler);
}
m.invoke(app, proxy);
} catch (IllegalArgumentException ex) {
// TODO: Print error?: method doesn't take an interface, etc.
} catch (IllegalAccessException ex) {
// TODO: Print error?: Other method invocation problem
} catch (InvocationTargetException ex) {
ex.getCause().printStackTrace();
// TODO: Print ex.getCause() a different way?
}
break;
}
}

/**
* Gimpy file menu to be used on OS X when no sketches are open.
Expand All @@ -162,7 +201,7 @@ public void actionPerformed(ActionEvent e) {
fileMenu.add(item);

item = Toolkit.newJMenuItemShift(Language.text("menu.file.sketchbook"), 'K');
item.addActionListener(new ActionListener() {
item.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
base.getNextMode().showSketchbookFrame();
Expand Down
57 changes: 8 additions & 49 deletions app/src/processing/app/platform/WindowsPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,16 @@ public class WindowsPlatform extends DefaultPlatform {
"\\" + APP_NAME.toLowerCase() + ".exe \"%1\"";
static final String REG_DOC = APP_NAME + ".Document";

// Starting with Java 9, the scaling is done automatically. If DPI is
// used to scaling within the application, one ends up with 2x the
// expected scale. See JEP 263.
private static final int WINDOWS_NATIVE_DPI = 96;

public void initBase(Base base) {
super.initBase(base);

checkAssociations();

//checkQuickTime();
checkPath();

Expand Down Expand Up @@ -627,56 +633,9 @@ public int unsetenv(String variable) {
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .


// Need to extend com.sun.jna.platform.win32.User32 to access
// Win32 function GetDpiForSystem()
interface ExtUser32 extends StdCallLibrary, com.sun.jna.platform.win32.User32 {
ExtUser32 INSTANCE = (ExtUser32) Native.loadLibrary("user32", ExtUser32.class, W32APIOptions.DEFAULT_OPTIONS);

public int GetDpiForSystem();

public int SetProcessDpiAwareness(int value);

public final int DPI_AWARENESS_INVALID = -1;
public final int DPI_AWARENESS_UNAWARE = 0;
public final int DPI_AWARENESS_SYSTEM_AWARE = 1;
public final int DPI_AWARENESS_PER_MONITOR_AWARE = 2;

public Pointer SetThreadDpiAwarenessContext(Pointer dpiContext);

public final Pointer DPI_AWARENESS_CONTEXT_UNAWARE = new Pointer(-1);
public final Pointer DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = new Pointer(-2);
public final Pointer DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = new Pointer(-3);
}


static private int detected = detectSystemDPI();


public int getSystemDPI() {
if (detected == -1) {
return super.getSystemDPI();
}
return detected;
// Note that this is supported "natively" within Java - See JEP 263.
return WINDOWS_NATIVE_DPI;
}


public static int detectSystemDPI() {
try {
ExtUser32.INSTANCE.SetProcessDpiAwareness(ExtUser32.DPI_AWARENESS_SYSTEM_AWARE);
} catch (Throwable e) {
// Ignore error
}
try {
ExtUser32.INSTANCE.SetThreadDpiAwarenessContext(ExtUser32.DPI_AWARENESS_CONTEXT_SYSTEM_AWARE);
} catch (Throwable e) {
// Ignore error (call valid only on Windows 10)
}
try {
return ExtUser32.INSTANCE.GetDpiForSystem();
} catch (Throwable e) {
// DPI detection failed, fall back with default
System.out.println("DPI detection failed, fallback to 96 dpi");
return -1;
}
}
}

0 comments on commit 428cad1

Please sign in to comment.