Skip to content

Commit fbc05e6

Browse files
committed
Support AspectJ in Gradle projects
1 parent 0428a58 commit fbc05e6

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import org.gradle.plugins.ide.eclipse.model.EclipseModel
2+
3+
gradle.projectsEvaluated { g ->
4+
if (System.getProperty("eclipse.application") == null) return
5+
def weavingPluginId = "io.freefair.aspectj.post-compile-weaving"
6+
def allprojects = g.rootProject.allprojects
7+
boolean hasWeaving = allprojects.any { it.plugins.hasPlugin(weavingPluginId) }
8+
allprojects.each { p ->
9+
if (p.plugins.hasPlugin("eclipse")) {
10+
EclipseModel eclipseModel = (EclipseModel) p.property("eclipse")
11+
if (hasWeaving) {
12+
if (GradleVersion.version(p.getGradle().getGradleVersion()) >= GradleVersion.version("5.4")) {
13+
try {
14+
try {
15+
Task task = p.getTasks().getByName("classes")
16+
eclipseModel.synchronizationTasks "classes"
17+
} catch (UnknownTaskException e) {
18+
// ignore
19+
}
20+
} catch (Exception e) {
21+
// ignore
22+
}
23+
}
24+
}
25+
if (p.plugins.hasPlugin("io.freefair.aspectj")) {
26+
String[] taskNames = [ "compileAspectj", "compileTestAspectj" ]
27+
if (GradleVersion.version(p.getGradle().getGradleVersion()) >= GradleVersion.version("5.4")) {
28+
for (final String taskName : taskNames) {
29+
try {
30+
Task task = p.getTasks().getByName(taskName)
31+
eclipseModel.synchronizationTasks taskName
32+
} catch (Exception e) {
33+
// ignore
34+
}
35+
}
36+
}
37+
eclipseModel.classpath.file.whenMerged { cp ->
38+
File projectDir = p.getProjectDir()
39+
for (final String taskName : taskNames) {
40+
Task task
41+
try {
42+
task = p.getTasks().getByName(taskName)
43+
} catch (UnknownTaskException e) {
44+
task = null;
45+
}
46+
if (task == null) {
47+
continue
48+
}
49+
if (task.hasProperty("destinationDirectory")) {
50+
def prop = task.destinationDirectory;
51+
Object outputDir = null
52+
if (prop instanceof org.gradle.api.file.DirectoryProperty) {
53+
outputDir = prop.asFile.getOrNull()
54+
} else if (prop instanceof File) {
55+
outputDir = prop
56+
}
57+
if (outputDir != null && outputDir.exists()) {
58+
cp.entries.removeAll { it instanceof org.gradle.plugins.ide.eclipse.model.Library && it.path.equals(outputDir.toString()) }
59+
Object entry = new org.gradle.plugins.ide.eclipse.model.Library(fileReference(outputDir))
60+
entry.exported = true
61+
entry.entryAttributes.put("aspectj_generated", "true")
62+
entry.entryAttributes.put("optional", "true")
63+
entry.entryAttributes.put("ignore_optional_problems", "true")
64+
cp.entries.add(entry)
65+
}
66+
}
67+
}
68+
}
69+
}
70+
}
71+
}
72+
}
73+

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/managers/GradleProjectImporter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,12 @@ private static List<String> getGradleInitScriptArgs() {
717717
addInitScriptToArgs(androidInitScript, args);
718718
}
719719

720+
// Add init script of aspectj support
721+
if (preferencesManager.getPreferences().isAspectjSupportEnabled()) {
722+
File aspectjInitScript = GradleUtils.getGradleInitScript("/gradle/aspectj/init.gradle");
723+
addInitScriptToArgs(aspectjInitScript, args);
724+
}
725+
720726
return args;
721727
}
722728

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ public class Preferences {
522522

523523
public static final String JAVA_JDT_LS_PROTOBUF_SUPPORT_ENABLED = "java.jdt.ls.protobufSupport.enabled";
524524
public static final String JAVA_JDT_LS_ANDROID_SUPPORT_ENABLED = "java.jdt.ls.androidSupport.enabled";
525+
public static final String JAVA_JDT_LS_ASPECTJ_SUPPORT_ENABLED = "java.jdt.ls.aspectjSupport.enabled";
525526
public static final String JAVA_JDT_LS_JAVAC_ENABLED = "java.jdt.ls.javac.enabled";
526527

527528
public static final String JAVA_COMPILE_NULLANALYSIS_NONNULL = "java.compile.nullAnalysis.nonnull";
@@ -709,6 +710,7 @@ public class Preferences {
709710
private ProjectEncodingMode projectEncoding;
710711
private boolean avoidVolatileChanges;
711712
private boolean protobufSupportEnabled;
713+
private boolean aspectjSupportEnabled;
712714
private boolean javacEnabled;
713715
private boolean androidSupportEnabled;
714716
private List<String> nonnullTypes;
@@ -1137,6 +1139,7 @@ public Preferences clone() {
11371139
prefs.projectEncoding = this.projectEncoding;
11381140
prefs.avoidVolatileChanges = this.avoidVolatileChanges;
11391141
prefs.protobufSupportEnabled = this.protobufSupportEnabled;
1142+
prefs.aspectjSupportEnabled = this.aspectjSupportEnabled;
11401143
prefs.javacEnabled = this.javacEnabled;
11411144
prefs.androidSupportEnabled = this.androidSupportEnabled;
11421145
prefs.nullAnalysisMode = this.nullAnalysisMode;
@@ -1753,6 +1756,11 @@ public static Preferences updateFrom(Preferences existing, Map<String, Object> c
17531756
prefs.setProtobufSupportEnabled(protobufSupported);
17541757
}
17551758

1759+
if (getValue(configuration, JAVA_JDT_LS_ASPECTJ_SUPPORT_ENABLED) != null) {
1760+
boolean aspectjSupported = getBoolean(configuration, JAVA_JDT_LS_ASPECTJ_SUPPORT_ENABLED, existing.aspectjSupportEnabled);
1761+
prefs.setAspectjSupportEnabled(aspectjSupported);
1762+
}
1763+
17561764
if (getValue(configuration, JAVA_JDT_LS_JAVAC_ENABLED) != null) {
17571765
boolean javacEnabled = getBoolean(configuration, JAVA_JDT_LS_JAVAC_ENABLED, existing.javacEnabled);
17581766
prefs.setJavacEnabled(javacEnabled);
@@ -2863,6 +2871,14 @@ public void setProtobufSupportEnabled(boolean protobufSupportEnabled) {
28632871
this.protobufSupportEnabled = protobufSupportEnabled;
28642872
}
28652873

2874+
public boolean isAspectjSupportEnabled() {
2875+
return aspectjSupportEnabled;
2876+
}
2877+
2878+
public void setAspectjSupportEnabled(boolean aspectjSupportEnabled) {
2879+
this.aspectjSupportEnabled = aspectjSupportEnabled;
2880+
}
2881+
28662882
public boolean isAndroidSupportEnabled() {
28672883
return this.androidSupportEnabled;
28682884
}

0 commit comments

Comments
 (0)