-
Notifications
You must be signed in to change notification settings - Fork 459
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Progress eclipse cleanup #1587
Draft
blacelle
wants to merge
8
commits into
diffplug:main
Choose a base branch
from
blacelle:ProgressEclipseCleanup
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,423
−1
Draft
Progress eclipse cleanup #1587
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f06c7cb
Fixed typo in eclipse-base.
fvgh 351a637
First draft for import organization (part of clean-up).
fvgh 43eea75
Some JDT versions throws exception for invalid syntax instead of retu…
fvgh 777d0b7
Finalized JDT import organizer wrapper and testing.
fvgh 2085159
First draft for JDT clean-up.
fvgh f9401fa
Merge remote-tracking branch 'origin/main' into ProgressEclipseCleanup
blacelle 1da8825
Merge remote-tracking branch 'origin/main' into ProgressEclipseCleanup
blacelle b013a55
Fix some issues with new Eclipse integration
blacelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
216 changes: 216 additions & 0 deletions
216
lib-extra/src/jdt/java/com/diffplug/spotless/extra/glue/jdt/CleanUpFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
/* | ||
* Copyright 2016 DiffPlug | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.diffplug.spotless.extra.glue.jdt; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.reflect.Constructor; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Properties; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import javax.xml.parsers.ParserConfigurationException; | ||
import javax.xml.parsers.SAXParser; | ||
import javax.xml.parsers.SAXParserFactory; | ||
import javax.xml.xpath.XPathExpressionException; | ||
|
||
import org.assertj.core.util.Sets; | ||
import org.eclipse.jdt.internal.corext.fix.CleanUpConstants; | ||
import org.eclipse.jdt.internal.corext.fix.CleanUpConstantsOptions; | ||
import org.eclipse.jdt.ui.cleanup.CleanUpOptions; | ||
import org.eclipse.jdt.ui.cleanup.ICleanUp; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.xml.sax.Attributes; | ||
import org.xml.sax.SAXException; | ||
import org.xml.sax.helpers.DefaultHandler; | ||
|
||
/** Provides configured clean-up implementations. */ | ||
final class CleanUpFactory { | ||
|
||
private final static Set<String> UNSUPPORTED_CLASSES = Collections.unmodifiableSet(Sets.newLinkedHashSet( | ||
"org.eclipse.jdt.internal.ui.fix.UnimplementedCodeCleanUp" //Would require Eclipse templates | ||
)); | ||
|
||
@SuppressWarnings("serial") | ||
private final static Map<String, FixedValue> UNSUPPORTED_CONFIG = Collections.unmodifiableMap(new HashMap<String, FixedValue>() { | ||
{ | ||
put(CleanUpConstants.REMOVE_UNUSED_CODE_IMPORTS, new FixedValue("false", "Unused import clean-up only works in case all imports can be resolved. As an alternative use: " + CleanUpConstants.ORGANIZE_IMPORTS)); | ||
} | ||
}); | ||
|
||
private final static String CLEAN_UP_CONFIG_FILE_NAME = "plugin.xml"; | ||
private final static String CLEAN_UP_CONFIG_DEPENDENCY_NAME = "org.eclipse.jdt.ui"; | ||
private static List<Constructor<? extends ICleanUp>> CLEAN_UP_SEQUENCE = null; | ||
private final CleanUpOptions options; | ||
|
||
CleanUpFactory(Properties settings) { | ||
options = new CleanUpOptions(); | ||
Logger logger = LoggerFactory.getLogger(CleanUpFactory.class); | ||
CleanUpConstantsOptions.setDefaultOptions(CleanUpConstants.DEFAULT_CLEAN_UP_OPTIONS, options); | ||
UNSUPPORTED_CONFIG.entrySet().stream().forEach(entry -> options.setOption(entry.getKey(), entry.getValue().value)); | ||
settings.forEach((key, value) -> { | ||
FixedValue fixed = UNSUPPORTED_CONFIG.get(key); | ||
if (null != fixed && fixed.value != value) { | ||
logger.warn(String.format("Using %s for %s instead of %s: %s", fixed.value, key, value, fixed.reason)); | ||
} else { | ||
options.setOption(key.toString(), value.toString()); | ||
} | ||
}); | ||
try { | ||
initializeCleanupActions(); | ||
} catch (IOException | ParserConfigurationException | XPathExpressionException e) { | ||
throw new RuntimeException("Faild to read Eclipse Clean-Up configuration.", e); | ||
} | ||
} | ||
|
||
private static synchronized void initializeCleanupActions() throws IOException, ParserConfigurationException, XPathExpressionException { | ||
if (null != CLEAN_UP_SEQUENCE) { | ||
return; | ||
} | ||
ClassLoader loader = CleanUpFactory.class.getClassLoader(); | ||
Optional<URL> configUrl = Collections.list(loader.getResources(CLEAN_UP_CONFIG_FILE_NAME)).stream().filter(url -> url.getPath().contains(CLEAN_UP_CONFIG_DEPENDENCY_NAME)).findAny(); | ||
if (!configUrl.isPresent()) { | ||
throw new RuntimeException("Could not find JAR containing " + CLEAN_UP_CONFIG_DEPENDENCY_NAME + ":" + CLEAN_UP_CONFIG_FILE_NAME); | ||
} | ||
InputStream configXmlStream = configUrl.get().openStream(); | ||
try { | ||
SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser(); | ||
CleanUpExtensionHandler handler = new CleanUpExtensionHandler(); | ||
saxParser.parse(configXmlStream, handler); | ||
CLEAN_UP_SEQUENCE = handler.getCleanUpSequence(); | ||
} catch (SAXException e) { | ||
//Add information about the XML location | ||
throw new RuntimeException("Failed to parse " + configUrl.get().toExternalForm(), e); | ||
} | ||
} | ||
|
||
public List<ICleanUp> create() { | ||
return CLEAN_UP_SEQUENCE.stream().map(constructor -> { | ||
try { | ||
ICleanUp cleanUp = constructor.newInstance(); | ||
cleanUp.setOptions(options); | ||
return cleanUp; | ||
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { | ||
throw new RuntimeException("Failed to created clean-up action for " + constructor.getName(), e); | ||
} | ||
}).collect(Collectors.toList()); | ||
} | ||
|
||
private static class FixedValue { | ||
public final String value; | ||
public final String reason; | ||
|
||
FixedValue(String value, String reason) { | ||
this.value = value; | ||
this.reason = reason; | ||
} | ||
}; | ||
|
||
private final static class CleanUpExtensionHandler extends DefaultHandler { | ||
private final static String CLEAN_UP_ELEMENT_NAME = "cleanUp"; | ||
private final static String ID_ATTRIBUTE_NAME = "id"; | ||
private final static String CLASS_ATTRIBUTE_NAME = "class"; | ||
private final static String RUN_AFTER_ATTRIBUTE_NAME = "runAfter"; | ||
private final Map<String, Constructor<? extends ICleanUp>> constructor; | ||
private final Map<String, String> runAfter; | ||
private final LinkedList<String> sorted; | ||
|
||
CleanUpExtensionHandler() { | ||
constructor = new HashMap<>(); | ||
runAfter = new LinkedHashMap<>(); //E.g. the elements are already sorted | ||
sorted = new LinkedList<>(); | ||
} | ||
|
||
@Override | ||
public void startDocument() throws SAXException { | ||
constructor.clear(); | ||
runAfter.clear(); | ||
sorted.clear(); | ||
super.startDocument(); | ||
} | ||
|
||
@Override | ||
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { | ||
if (CLEAN_UP_ELEMENT_NAME == qName) { | ||
String id = getMandatoryAttribute(attributes, ID_ATTRIBUTE_NAME); | ||
String className = getMandatoryAttribute(attributes, CLASS_ATTRIBUTE_NAME); | ||
if (!UNSUPPORTED_CLASSES.contains(className)) { | ||
try { | ||
Class<?> clazz = Class.forName(className); | ||
Class<? extends ICleanUp> clazzImplementsICleanUp = clazz.asSubclass(ICleanUp.class); | ||
constructor.put(id, clazzImplementsICleanUp.getConstructor()); | ||
} catch (ClassNotFoundException | ClassCastException | NoSuchMethodException | SecurityException e) { | ||
throw new SAXException("Failed to obtain constructor for " + CLEAN_UP_ELEMENT_NAME + " element class " + className, e); | ||
} | ||
} | ||
String runAfterId = attributes.getValue(RUN_AFTER_ATTRIBUTE_NAME); | ||
if (null == runAfterId) { | ||
sorted.push(id); | ||
} else { | ||
runAfter.put(id, runAfterId); | ||
} | ||
} | ||
super.startElement(uri, localName, qName, attributes); | ||
} | ||
|
||
private static String getMandatoryAttribute(Attributes attributes, String qName) throws SAXException { | ||
String value = attributes.getValue(qName); | ||
if (null == value) { | ||
throw new SAXException(CLEAN_UP_ELEMENT_NAME + " element without " + qName + " attribute."); | ||
} | ||
return value; | ||
} | ||
|
||
@Override | ||
public void endDocument() throws SAXException { | ||
if (runAfter.isEmpty()) { | ||
throw new SAXException(CLEAN_UP_ELEMENT_NAME + " element has not been found in XML."); | ||
} | ||
while (!runAfter.isEmpty()) { | ||
//E.g. the elements are already sorted. Hence only one iteration is expected. | ||
List<String> foundEntries = new ArrayList<>(runAfter.size()); | ||
for (Map.Entry<String, String> entry : runAfter.entrySet()) { | ||
int runAfterIndex = sorted.lastIndexOf(entry.getValue()); | ||
if (0 <= runAfterIndex) { | ||
foundEntries.add(entry.getKey()); | ||
sorted.add(runAfterIndex + 1, entry.getKey()); | ||
} | ||
} | ||
foundEntries.forEach(e -> runAfter.remove(e)); | ||
if (foundEntries.isEmpty()) { | ||
throw new SAXException(CLEAN_UP_ELEMENT_NAME + " element the following precessor IDs cannot be resolved: " + runAfter.values().stream().collect(Collectors.joining("; "))); | ||
} | ||
} | ||
super.endDocument(); | ||
} | ||
|
||
public List<Constructor<? extends ICleanUp>> getCleanUpSequence() { | ||
return sorted.stream().map(id -> constructor.get(id)).filter(clazz -> null != clazz).collect(Collectors.toList()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nedtwigg This was in the old eclipse-jdt build.gradle. I suppose it has to be converted into things like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct.