15
15
*/
16
16
package com .diffplug .spotless .extra .eclipse .java ;
17
17
18
- import java .util .Collections ;
19
18
import java .util .HashMap ;
20
- import java .util .Hashtable ;
21
19
import java .util .List ;
22
20
import java .util .Map ;
23
21
import java .util .Properties ;
27
25
import org .eclipse .core .resources .IFolder ;
28
26
import org .eclipse .core .resources .IProject ;
29
27
import org .eclipse .core .resources .IProjectDescription ;
28
+ import org .eclipse .core .resources .ProjectScope ;
30
29
import org .eclipse .core .resources .ResourcesPlugin ;
31
30
import org .eclipse .core .runtime .IProgressMonitor ;
31
+ import org .eclipse .core .runtime .preferences .IEclipsePreferences ;
32
32
import org .eclipse .jdt .core .IBuffer ;
33
33
import org .eclipse .jdt .core .ICompilationUnit ;
34
34
import org .eclipse .jdt .core .IJavaProject ;
35
35
import org .eclipse .jdt .core .IPackageFragment ;
36
36
import org .eclipse .jdt .core .IPackageFragmentRoot ;
37
- import org .eclipse .jdt .core .IType ;
38
37
import org .eclipse .jdt .core .JavaCore ;
39
38
import org .eclipse .jdt .core .JavaModelException ;
39
+ import org .eclipse .jdt .core .manipulation .JavaManipulation ;
40
40
import org .eclipse .jdt .internal .core .BufferManager ;
41
41
import org .eclipse .jdt .internal .core .CompilationUnit ;
42
42
import org .eclipse .jdt .internal .core .DefaultWorkingCopyOwner ;
43
- import org .eclipse .jdt .internal .core .JavaModelManager ;
43
+ import org .eclipse .jdt .internal .core .JavaCorePreferenceInitializer ;
44
44
import org .eclipse .jdt .internal .core .PackageFragment ;
45
+ import org .eclipse .jdt .internal .core .nd .indexer .Indexer ;
45
46
46
47
/**
47
48
* Helper methods to create Java compilation unit.
51
52
* (see {@code org.eclipse.core.internal.resources.LocationValidator} for details).
52
53
* </p>
53
54
*/
54
- class EclipseJdtFactory extends OS {
55
+ class EclipseJdtHelper extends OS {
55
56
56
57
private final static String ROOT_AS_SRC = "" ;
57
58
private final static String PROJECT_NAME = "spotless" ;
58
59
private final static String SOURCE_NAME = "source.java" ;
59
- private final static AtomicInteger UNIQUE_PROJECT_ID = new AtomicInteger ( 0 );
60
+ private static EclipseJdtHelper INSTANCE ;
60
61
61
- private final static Map <String , String > DEFAULT_OPTIONS ;
62
-
63
- static {
64
- Map <String , String > defaultOptions = new HashMap <>();
65
- defaultOptions .put (JavaCore .COMPILER_SOURCE , getJavaCoreVersion ());
66
- DEFAULT_OPTIONS = Collections .unmodifiableMap (defaultOptions );
62
+ static synchronized EclipseJdtHelper getInstance () {
63
+ if (null == INSTANCE ) {
64
+ INSTANCE = new EclipseJdtHelper ();
65
+ }
66
+ return INSTANCE ;
67
67
}
68
+
69
+ private final AtomicInteger uniqueProjectId = new AtomicInteger (0 );
70
+ private final Map <String , String > defaultOptions ;
71
+
72
+ private EclipseJdtHelper () {
73
+ defaultOptions = new HashMap <>();
74
+ defaultOptions .put (JavaCore .COMPILER_SOURCE , getJavaCoreVersion ());
75
+
76
+ /*
77
+ * Assure that the 'allowed keys' are initialized, otherwise
78
+ * JProject will not accept any options.
79
+ */
80
+ new JavaCorePreferenceInitializer ().initializeDefaultPreferences ();
81
+
82
+ /*
83
+ * Don't run indexer in background (does not disable thread but the job scheduling)
84
+ */
85
+ Indexer .getInstance ().enableAutomaticIndexing (false );
86
+ }
68
87
69
88
private static String getJavaCoreVersion () {
70
89
final String javaVersion = System .getProperty ("java.version" );
@@ -76,30 +95,41 @@ private static String getJavaCoreVersion() {
76
95
}
77
96
return orderedSupportedCoreVersions .get (orderedSupportedCoreVersions .size () - 1 );
78
97
}
79
-
98
+
80
99
/**
81
100
* Creates a JAVA project and applies the configuration.
82
101
* @param settings Configuration settings
83
102
* @return Configured JAVA project
84
103
* @throws Exception In case the project creation fails
85
104
*/
86
- public final static IJavaProject createProject (Properties settings ) throws Exception {
87
- String uniqueProjectName = String .format ("%s-%d" , PROJECT_NAME , UNIQUE_PROJECT_ID .incrementAndGet ());
105
+ IJavaProject createProject (Properties settings ) throws Exception {
106
+ String uniqueProjectName = String .format ("%s-%d" , PROJECT_NAME , uniqueProjectId .incrementAndGet ());
88
107
IProject project = ResourcesPlugin .getWorkspace ().getRoot ().getProject (uniqueProjectName );
89
108
// The project must be open before items (natures, folders, sources, ...) can be created
90
109
project .create (null );
91
110
project .open (0 , null );
92
- //If the project nature is not set, things like AST are not created for the Java projects
111
+
112
+ //If the project nature is not set, the AST is not created for the compilation units
93
113
IProjectDescription description = project .getDescription ();
94
114
description .setNatureIds (new String []{JavaCore .NATURE_ID });
95
115
project .setDescription (description , null );
96
116
IJavaProject jProject = JavaCore .create (project );
97
117
98
- Map <String , String > settingsMap = new HashMap <>(DEFAULT_OPTIONS );
118
+ Map <String , String > allSettings = new HashMap <>(defaultOptions );
99
119
settings .forEach ((key , value ) -> {
100
- settingsMap .put (key .toString (), value .toString ());
120
+ allSettings .put (key .toString (), value .toString ());
101
121
});
102
- jProject .setOptions (settingsMap );
122
+ //Configure JDT manipulation processor
123
+ IEclipsePreferences projectPrefs = new ProjectScope (project .getProject ()).getNode (JavaManipulation .getPreferenceNodeId ());
124
+ allSettings .forEach ((key , value ) -> {
125
+ projectPrefs .put (key .toString (), value .toString ());
126
+ });
127
+ /*
128
+ * Configure options taken directly from the Java project (without qualifier).
129
+ * Whether a setting is a Java project option or not, is filtered by the
130
+ * JavaCorePreferenceInitializer, initialized by the constructor of this class.
131
+ */
132
+ jProject .setOptions (allSettings );
103
133
104
134
// Eclipse source files require an existing source folder for creation
105
135
IPackageFragmentRoot src = jProject .getPackageFragmentRoot (jProject .getProject ());
@@ -110,27 +140,22 @@ public final static IJavaProject createProject(Properties settings) throws Excep
110
140
// Eclipse clean-up requires an existing source file
111
141
pkg .createCompilationUnit (SOURCE_NAME , "" , true , null );
112
142
113
- //
114
- disableSecondaryTypes (project );
115
-
116
143
return jProject ;
117
144
}
118
145
119
- private static void disableSecondaryTypes (IProject project ) {
120
- JavaModelManager .PerProjectInfo info = JavaModelManager .getJavaModelManager ().getPerProjectInfo (project , true );
121
- info .secondaryTypes = new Hashtable <String , Map <String , IType >>();
122
- }
123
-
124
- public static ICompilationUnit createJavaSource (String contents , IJavaProject jProject ) throws Exception {
146
+ ICompilationUnit createCompilationUnit (String contents , IJavaProject jProject ) throws Exception {
125
147
IPackageFragmentRoot src = jProject .getPackageFragmentRoot (jProject .getProject ());
126
148
IPackageFragment pkg = src .getPackageFragment (ROOT_AS_SRC );
127
149
return new RamCompilationUnit ((PackageFragment ) pkg , contents );
128
150
}
129
151
130
- /** Spotless keeps compilation units in RAM as long as they are worked on. */
152
+ /** Keep compilation units in RAM */
131
153
private static class RamCompilationUnit extends CompilationUnit {
132
154
133
- //Each RMA compilation unit has its own buffer manager. A drop is therefore prevented.
155
+ /*
156
+ * Each RAM compilation unit has its own buffer manager to
157
+ * prevent dropping of CUs when a maximum size is reached.
158
+ */
134
159
private final RamBufferManager manager ;
135
160
136
161
RamCompilationUnit (PackageFragment parent , String contents ) {
@@ -153,7 +178,7 @@ protected BufferManager getBufferManager() {
153
178
154
179
@ Override
155
180
public void save (IProgressMonitor pm , boolean force ) throws JavaModelException {
156
- //RAM CU is never saved to disk
181
+ //RAM CU is never stored on disk
157
182
}
158
183
159
184
@ Override
@@ -167,7 +192,7 @@ public boolean equals(Object obj) {
167
192
}
168
193
}
169
194
170
- /** Work- around required package privileges when adding buffer for manager singleton */
195
+ /** Work around package privileges */
171
196
private static class RamBufferManager extends BufferManager {
172
197
void add (IBuffer buffer ) {
173
198
addBuffer (buffer );
0 commit comments