Skip to content

Commit 85a3b49

Browse files
committed
Add JavaFX support
Signed-off-by: Fred Bricon <fbricon@gmail.com>
1 parent 96d95db commit 85a3b49

File tree

6 files changed

+420
-0
lines changed

6 files changed

+420
-0
lines changed

org.eclipse.jdt.ls.core/plugin.xml

+13
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,17 @@
146146
class="org.eclipse.jdt.ls.core.internal.corext.refactoring.ChangeMethodSignatureRefactoringContribution"
147147
id="org.eclipse.jdt.ls.change.method.signature"/>
148148
</extension>
149+
<extension
150+
point="org.eclipse.jdt.launching.executionEnvironments">
151+
<ruleParticipant
152+
class="org.eclipse.jdt.ls.core.internal.javafx.FXAccessRuleParticipant"
153+
id="org.eclipse.jdt.ls.core.fxrule">
154+
</ruleParticipant>
155+
</extension>
156+
<extension
157+
point="org.eclipse.jdt.launching.libraryLocationResolvers">
158+
<resolver
159+
class="org.eclipse.jdt.ls.core.internal.javafx.FXLibraryLocationResolver">
160+
</resolver>
161+
</extension>
149162
</plugin>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
package org.eclipse.jdt.ls.core.internal.javafx;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Files;
6+
import java.nio.file.Paths;
7+
import java.util.ArrayList;
8+
import java.util.Collections;
9+
import java.util.List;
10+
import java.util.stream.Collectors;
11+
12+
import org.eclipse.core.runtime.CoreException;
13+
import org.eclipse.core.runtime.IPath;
14+
import org.eclipse.core.runtime.Path;
15+
import org.eclipse.core.runtime.preferences.InstanceScope;
16+
import org.eclipse.jdt.core.IAccessRule;
17+
import org.eclipse.jdt.core.IClasspathAttribute;
18+
import org.eclipse.jdt.core.IClasspathEntry;
19+
import org.eclipse.jdt.core.IJavaProject;
20+
import org.eclipse.jdt.core.JavaCore;
21+
import org.eclipse.jdt.launching.IVMInstall;
22+
import org.eclipse.jdt.launching.JavaRuntime;
23+
import org.eclipse.jdt.launching.LibraryLocation;
24+
25+
/**
26+
*
27+
* Copied from
28+
* https://github.com/eclipse-efx/efxclipse-eclipse/blob/master/bundles/tooling/org.eclipse.fx.ide.jdt.core/src/org/eclipse/fx/ide/jdt/core/internal/BuildPathSupport.java
29+
*
30+
*/
31+
public class BuildPathSupport {
32+
public static final String WEB_JAVADOC_LOCATION = "http://docs.oracle.com/javase/8/javafx/api/";
33+
34+
public static List<IClasspathEntry> getJavaFXLibraryEntry(IJavaProject project) {
35+
FXVersion version = getFXVersion(project);
36+
if( version == FXVersion.FX2 || version == FXVersion.FX8) {
37+
IPath[] paths = getFxJarPath(project);
38+
List<IClasspathEntry> rv = new ArrayList<>();
39+
if( paths != null ) {
40+
41+
IPath jarLocationPath = paths[0];
42+
IPath javadocLocation = paths[1];
43+
IPath fxSource = paths[3];
44+
45+
IClasspathAttribute[] attributes;
46+
IAccessRule[] accessRules= { };
47+
if (javadocLocation == null || !javadocLocation.toFile().exists()) {
48+
attributes= new IClasspathAttribute[] { JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, WEB_JAVADOC_LOCATION) };
49+
} else {
50+
attributes= new IClasspathAttribute[] { JavaCore.newClasspathAttribute(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME, javadocLocation.toFile().toURI().toString()) };
51+
}
52+
53+
if( jarLocationPath.toFile().exists() ) {
54+
rv.add(JavaCore.newLibraryEntry(jarLocationPath, fxSource, null, accessRules, attributes, false));
55+
}
56+
}
57+
58+
return rv;
59+
} else if( version == FXVersion.FX11 || version == FXVersion.FX11PLUS ) {
60+
String sdkPath = InstanceScope.INSTANCE.getNode("org.eclipse.fx.ide.ui").get("javafx-sdk", null);
61+
List<IClasspathEntry> entries = new ArrayList<>();
62+
if( sdkPath != null ) {
63+
java.nio.file.Path path = Paths.get(sdkPath);
64+
if( Files.exists(path) ) {
65+
try {
66+
67+
entries.addAll(Files.list(path).filter( p -> p.getFileName().toString().endsWith(".jar")).map( p -> {
68+
IClasspathAttribute moduleAttr = JavaCore.newClasspathAttribute(IClasspathAttribute.MODULE, "true"); //$NON-NLS-1$
69+
return JavaCore.newLibraryEntry(
70+
new Path(p.toAbsolutePath().toString()),
71+
new Path(p.getParent().resolve("src.zip").toAbsolutePath().toString()),
72+
new Path("."),
73+
new IAccessRule[]{
74+
JavaCore.newAccessRule(new Path("javafx/*"),IAccessRule.K_ACCESSIBLE),
75+
JavaCore.newAccessRule(new Path("com/sun/*"),IAccessRule.K_DISCOURAGED),
76+
JavaCore.newAccessRule(new Path("netscape/javascript/*"),IAccessRule.K_DISCOURAGED)},
77+
new IClasspathAttribute[] { moduleAttr },
78+
false);
79+
}).collect(Collectors.toList()));
80+
} catch (IOException e) {
81+
throw new IllegalStateException();
82+
}
83+
}
84+
}
85+
return entries;
86+
}
87+
return Collections.emptyList();
88+
}
89+
90+
public static FXVersion getFXVersion(IJavaProject project) {
91+
try {
92+
IVMInstall i = JavaRuntime.getVMInstall(project);
93+
if( i == null ) {
94+
i = JavaRuntime.getDefaultVMInstall();
95+
}
96+
97+
return FXVersionUtil.getFxVersion(i);
98+
} catch (CoreException e) {
99+
throw new IllegalStateException(e);
100+
}
101+
}
102+
103+
public static IPath[] getFxJarPath(IJavaProject project) {
104+
IPath jarLocationPath = null;
105+
IPath javadocLocation = null;
106+
IPath antJarLocationPath = null;
107+
IPath sourceLocationPath = null;
108+
109+
try {
110+
IVMInstall i = JavaRuntime.getVMInstall(project);
111+
if( i == null ) {
112+
i = JavaRuntime.getDefaultVMInstall();
113+
}
114+
115+
if( FXVersionUtil.getFxVersion(i) != FXVersion.FX9 ) {
116+
return getFxJarPath(i);
117+
}
118+
return null;
119+
} catch (CoreException e) {
120+
// TODO Auto-generated catch block
121+
e.printStackTrace();
122+
}
123+
124+
return new IPath[] { jarLocationPath, javadocLocation, antJarLocationPath, sourceLocationPath };
125+
}
126+
127+
128+
public static IPath[] getSwtFxJarPath(IVMInstall i) {
129+
File installDir = i.getInstallLocation();
130+
IPath[] checkPaths = {
131+
// JDK 8
132+
new Path(installDir.getAbsolutePath()).append("jre").append("lib").append("jfxswt.jar"),
133+
new Path(installDir.getAbsolutePath()).append("lib").append("jfxswt.jar"), // JRE
134+
new Path(installDir.getAbsolutePath()).append("lib").append("javafx-swt.jar")
135+
};
136+
137+
IPath jarLocationPath = null;
138+
IPath javadocLocation = null;
139+
IPath sourceLocationPath = null;
140+
141+
jarLocationPath = checkPaths[0];
142+
143+
if( ! jarLocationPath.toFile().exists() ) {
144+
for( IPath p : checkPaths ) {
145+
if( p.toFile().exists() ) {
146+
jarLocationPath = p;
147+
break;
148+
}
149+
}
150+
}
151+
152+
if( jarLocationPath.toFile().exists() ) {
153+
sourceLocationPath = new Path(installDir.getAbsolutePath()).append("javafx-src.zip");
154+
155+
return new IPath[] { jarLocationPath, javadocLocation, sourceLocationPath };
156+
}
157+
158+
return null;
159+
}
160+
161+
public static IPath[] getFxJarPath(IVMInstall i) {
162+
for( LibraryLocation l : JavaRuntime.getLibraryLocations(i) ) {
163+
if( "jfxrt.jar".equals(l.getSystemLibraryPath().lastSegment()) ) {
164+
return null;
165+
}
166+
}
167+
168+
IPath jarLocationPath = null;
169+
IPath javadocLocation = null;
170+
IPath antJarLocationPath = null;
171+
IPath sourceLocationPath = null;
172+
173+
File installDir = i.getInstallLocation();
174+
175+
IPath[] checkPaths = {
176+
// Java 7
177+
new Path(installDir.getAbsolutePath()).append("jre").append("lib").append("jfxrt.jar"),
178+
new Path(installDir.getAbsolutePath()).append("lib").append("jfxrt.jar") // JRE
179+
180+
};
181+
182+
jarLocationPath = checkPaths[0];
183+
184+
if( ! jarLocationPath.toFile().exists() ) {
185+
for( IPath p : checkPaths ) {
186+
if( p.toFile().exists() ) {
187+
jarLocationPath = p;
188+
break;
189+
}
190+
}
191+
}
192+
193+
if( ! jarLocationPath.toFile().exists() ) {
194+
StringBuilder error = new StringBuilder("Unable to detect JavaFX jar for JRE ").append(i.getName());
195+
error.append(System.lineSeparator()).append(" JRE: ").append(installDir.getAbsolutePath());
196+
error.append(System.lineSeparator()).append(" Checked paths:");
197+
for( IPath p : checkPaths ) {
198+
error.append(System.lineSeparator()).append(" " + p.toFile().getAbsolutePath());
199+
}
200+
201+
return null;
202+
}
203+
204+
javadocLocation = new Path(installDir.getParentFile().getAbsolutePath()).append("docs").append("api"); //TODO Not shipped yet
205+
if( ! javadocLocation.toFile().exists() ) {
206+
IPath p = new Path(System.getProperty("user.home")).append("javafx-api-"+ i.getName()).append("docs").append("api");
207+
if( p.toFile().exists() ) {
208+
javadocLocation = p;
209+
}
210+
}
211+
212+
antJarLocationPath = new Path(installDir.getParent()).append("lib").append("ant-javafx.jar");
213+
214+
sourceLocationPath = new Path(installDir.getAbsolutePath()).append("javafx-src.zip");
215+
216+
if( ! sourceLocationPath.toFile().exists() ) {
217+
sourceLocationPath = null;
218+
}
219+
220+
return new IPath[] { jarLocationPath, javadocLocation, antJarLocationPath, sourceLocationPath };
221+
}
222+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.eclipse.jdt.ls.core.internal.javafx;
2+
import org.eclipse.core.runtime.Path;
3+
import org.eclipse.jdt.core.IAccessRule;
4+
import org.eclipse.jdt.core.IJavaProject;
5+
import org.eclipse.jdt.core.JavaCore;
6+
import org.eclipse.jdt.launching.IVMInstall;
7+
import org.eclipse.jdt.launching.LibraryLocation;
8+
import org.eclipse.jdt.launching.environments.IAccessRuleParticipant;
9+
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
10+
11+
/**
12+
*
13+
* Copied from
14+
* https://github.com/eclipse-efx/efxclipse-eclipse/blob/master/bundles/tooling/org.eclipse.fx.ide.jdt.core/src/org/eclipse/fx/ide/jdt/core/internal/FXAccessRuleParticipant.java
15+
*
16+
*/
17+
public class FXAccessRuleParticipant implements IAccessRuleParticipant {
18+
19+
@Override
20+
public IAccessRule[][] getAccessRules(IExecutionEnvironment environment, IVMInstall vm, LibraryLocation[] libraries, IJavaProject project) {
21+
boolean fxOnExtPath = false;
22+
for( LibraryLocation loc : libraries ) {
23+
if( "jfxrt.jar".equals(loc.getSystemLibraryPath().lastSegment()) ) {
24+
fxOnExtPath = true;
25+
break;
26+
}
27+
}
28+
29+
if( fxOnExtPath ) {
30+
IAccessRule[] rules = new IAccessRule[6];
31+
rules[0] = JavaCore.newAccessRule(new Path("javafx/**"), IAccessRule.K_ACCESSIBLE);
32+
rules[1] = JavaCore.newAccessRule(new Path("netscape/javascript/**"), IAccessRule.K_ACCESSIBLE);
33+
rules[2] = JavaCore.newAccessRule(new Path("com/sun/javafx/**"), IAccessRule.K_DISCOURAGED);
34+
rules[3] = JavaCore.newAccessRule(new Path("com/sun/glass/**"), IAccessRule.K_DISCOURAGED);
35+
rules[4] = JavaCore.newAccessRule(new Path("com/sun/media/jfxmedia/**"), IAccessRule.K_DISCOURAGED);
36+
rules[5] = JavaCore.newAccessRule(new Path("com/sun/prism/**"), IAccessRule.K_DISCOURAGED);
37+
38+
IAccessRule[][] rv = new IAccessRule[libraries.length][];
39+
40+
for( int i = 0; i < rv.length; i++ ) {
41+
rv[i] = rules;
42+
}
43+
44+
return rv;
45+
} else if( FXVersionUtil.getFxVersion(vm) == FXVersion.FX9 ) {
46+
IAccessRule[] rules = new IAccessRule[2];
47+
rules[0] = JavaCore.newAccessRule(new Path("javafx/**"), IAccessRule.K_ACCESSIBLE);
48+
rules[1] = JavaCore.newAccessRule(new Path("netscape/javascript/**"), IAccessRule.K_ACCESSIBLE);
49+
50+
IAccessRule[][] rv = new IAccessRule[libraries.length][];
51+
52+
for( int i = 0; i < rv.length; i++ ) {
53+
rv[i] = rules;
54+
}
55+
56+
return rv;
57+
}
58+
59+
return new IAccessRule[0][0];
60+
}
61+
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.eclipse.jdt.ls.core.internal.javafx;
2+
import static org.eclipse.jdt.ls.core.internal.JavaLanguageServerPlugin.logError;
3+
4+
import java.io.File;
5+
import java.net.URL;
6+
7+
import org.eclipse.core.runtime.IPath;
8+
import org.eclipse.core.runtime.Path;
9+
import org.eclipse.jdt.launching.ILibraryLocationResolver;
10+
11+
/**
12+
*
13+
* Copied from
14+
* https://github.com/eclipse-efx/efxclipse-eclipse/blob/master/bundles/tooling/org.eclipse.fx.ide.jdt.core/src/org/eclipse/fx/ide/jdt/core/internal/FXLibraryLocationResolver.java
15+
*
16+
*/
17+
public class FXLibraryLocationResolver implements ILibraryLocationResolver {
18+
19+
public FXLibraryLocationResolver() {
20+
}
21+
22+
@Override
23+
public IPath getPackageRoot(IPath libraryPath) {
24+
return Path.EMPTY;
25+
}
26+
27+
@Override
28+
public IPath getSourcePath(IPath libraryPath) {
29+
if( libraryPath.lastSegment().endsWith("jfxrt.jar") ) {
30+
File jarLocation = libraryPath.toFile();
31+
File jdkHome = jarLocation.getParentFile().getParentFile().getParentFile().getParentFile();
32+
IPath srcPath = new Path(jdkHome.getAbsolutePath()).append("javafx-src.zip");
33+
if( srcPath.toFile().exists() ) {
34+
return srcPath;
35+
}
36+
}
37+
return Path.EMPTY;
38+
}
39+
40+
@Override
41+
public URL getJavadocLocation(IPath libraryPath) {
42+
if( libraryPath.lastSegment().endsWith("jfxrt.jar") ) {
43+
try {
44+
File jarLocation = libraryPath.toFile();
45+
File jdkHome = jarLocation.getParentFile().getParentFile().getParentFile().getParentFile();
46+
File javaDoc = new Path(jdkHome.getAbsolutePath()).append("docs").append("api").toFile();
47+
if( javaDoc.exists() ) {
48+
return javaDoc.toURI().toURL();
49+
}
50+
51+
javaDoc = new Path(System.getProperty("user.home")).append("javafx8-api").append("docs").append("api").toFile();
52+
if( javaDoc.exists() ) {
53+
return javaDoc.toURI().toURL();
54+
}
55+
56+
return new URL(BuildPathSupport.WEB_JAVADOC_LOCATION);
57+
} catch (Exception e) {
58+
logError("Failure while trying to detect JavaFX8 JavaDoc: " + e.getMessage());
59+
}
60+
}
61+
62+
return null;
63+
}
64+
65+
@Override
66+
public URL getIndexLocation(IPath libraryPath) {
67+
return null;
68+
}
69+
70+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.eclipse.jdt.ls.core.internal.javafx;
2+
3+
/**
4+
*
5+
* Copied from
6+
* https://github.com/eclipse-efx/efxclipse-eclipse/blob/master/bundles/tooling/org.eclipse.fx.ide.jdt.core/src/org/eclipse/fx/ide/jdt/core/FXVersion.java
7+
*
8+
*/
9+
public enum FXVersion {
10+
FX2, FX8, FX9, FX11, FX11PLUS, UNKNOWN
11+
}

0 commit comments

Comments
 (0)