diff --git a/config/checkstyle-checks.xml b/config/checkstyle-checks.xml index 9fec5a433..d5402dbd9 100644 --- a/config/checkstyle-checks.xml +++ b/config/checkstyle-checks.xml @@ -294,6 +294,16 @@ + + + + diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/CheckstyleFileWriter.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/CheckstyleFileWriter.java index 2239ca052..f47873c91 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/CheckstyleFileWriter.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/CheckstyleFileWriter.java @@ -37,6 +37,7 @@ public class CheckstyleFileWriter { /** An object containing all settings for the checkstyle-file. */ private final CheckstyleSetting mCheckstyleSetting; + private final String file; /** * Creates new instance of class CheckstyleFileWriter. @@ -48,7 +49,10 @@ public class CheckstyleFileWriter { */ public CheckstyleFileWriter(final CheckstyleSetting setting, final String file) { mCheckstyleSetting = setting; + this.file = file; + } + public void writeXmlFile() { try (FileOutputStream fw = new FileOutputStream(file)) { writeXMLFile(fw); } catch (final IOException ex) { diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/CheckstyleTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/CheckstyleTransformer.java index 3bed1b784..27f1172b1 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/CheckstyleTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/CheckstyleTransformer.java @@ -117,10 +117,9 @@ private void loadTransformationClasses(final List classnames) * eclipse-formatter-profile. */ // FormatterConfigWriter used via side effect on its arguments - @SuppressWarnings("unused") public void transformRules() { loadRuleConfigurations(); - new FormatterConfigWriter(mProject, mFormatterSetting); + new FormatterConfigWriter(mProject, mFormatterSetting).writeSettings(); } /** diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigWriter.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigWriter.java index 029e85122..eedbfaaf1 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigWriter.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterConfigWriter.java @@ -57,14 +57,12 @@ public class FormatterConfigWriter { public FormatterConfigWriter(IProject project, final FormatterConfiguration settings) { mConfiguration = settings; mProject = project; - - writeSettings(); } /** * Method for persisting all settings to files. */ - private void writeSettings() { + public void writeSettings() { writeCleanupSettings(mConfiguration.getCleanupSettings()); writeFormatterSettings(mConfiguration.getFormatterSettings()); } diff --git a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterTransformer.java b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterTransformer.java index 297eae78e..62eaf183b 100644 --- a/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterTransformer.java +++ b/net.sf.eclipsecs.core/src/net/sf/eclipsecs/core/transformer/FormatterTransformer.java @@ -58,14 +58,13 @@ public FormatterTransformer() { * @throws CheckstylePluginException */ // CheckstyleFileWriter acts on its own parameters - @SuppressWarnings("unused") public void transformRules(final String path, Map formatterSettings) throws CheckstylePluginException { CheckstyleSetting checkstyleSetting = new CheckstyleSetting(); loadTransformationClasses(formatterSettings).stream() .map(AbstractFTransformationClass::transformRule) .forEach(checkstyleSetting::addSetting); - new CheckstyleFileWriter(checkstyleSetting, path); + new CheckstyleFileWriter(checkstyleSetting, path).writeXmlFile(); } /** diff --git a/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/CheckstyleUIPlugin.java b/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/CheckstyleUIPlugin.java index 0f7b9e7d8..1835533ae 100644 --- a/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/CheckstyleUIPlugin.java +++ b/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/CheckstyleUIPlugin.java @@ -46,8 +46,6 @@ public class CheckstyleUIPlugin extends AbstractUIPlugin { /** The shared instance. */ private static CheckstyleUIPlugin sPlugin; - private static Boolean isEclipse3; - /** * The constructor. */ @@ -62,30 +60,6 @@ public void stop(BundleContext context) throws Exception { super.stop(context); } - /** - * @return true if we're running on an Eclipse 3 platform. - */ - public static boolean isE3() { - - if (isEclipse3 == null) { - - // previous checking on the platform product version has not been - // reliable, since there are e4 - // based - // products with a 3 as major version (e.g. Spring Tools Suite). - try { - - // instead now check for the presence of a known e4 class - Class.forName("org.eclipse.e4.ui.model.application.MApplicationElement"); - isEclipse3 = false; - } catch (ClassNotFoundException ex) { - isEclipse3 = true; - } - } - - return isEclipse3; - } - /** * Returns the shared instance. * diff --git a/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/properties/filter/CheckFileOnOpenPartListener.java b/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/properties/filter/CheckFileOnOpenPartListener.java index c8ec41ded..7c80931d6 100644 --- a/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/properties/filter/CheckFileOnOpenPartListener.java +++ b/net.sf.eclipsecs.ui/src/net/sf/eclipsecs/ui/properties/filter/CheckFileOnOpenPartListener.java @@ -20,37 +20,23 @@ package net.sf.eclipsecs.ui.properties.filter; -import java.io.StringReader; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.List; -import java.util.Map; - import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.core.runtime.SubMonitor; import org.eclipse.core.runtime.jobs.ISchedulingRule; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IEditorPart; import org.eclipse.ui.IEditorReference; -import org.eclipse.ui.IElementFactory; -import org.eclipse.ui.IMemento; import org.eclipse.ui.IPartListener2; -import org.eclipse.ui.IWorkbenchPart; import org.eclipse.ui.IWorkbenchPartReference; -import org.eclipse.ui.PlatformUI; -import org.eclipse.ui.WorkbenchException; -import org.eclipse.ui.XMLMemento; -import org.eclipse.ui.internal.IWorkbenchConstants; +import org.eclipse.ui.PartInitException; import org.eclipse.ui.part.FileEditorInput; import net.sf.eclipsecs.core.Messages; @@ -64,18 +50,13 @@ import net.sf.eclipsecs.core.projectconfig.filters.UnOpenedFilesFilter; import net.sf.eclipsecs.core.util.CheckstyleLog; import net.sf.eclipsecs.core.util.CheckstylePluginException; -import net.sf.eclipsecs.ui.CheckstyleUIPlugin; /** * PartListener implementation that listens for opening editor parts and runs Checkstyle on the * opened file if the UnOpenedFileFilter is active. * * @see feature request - * @implNote To avoid the restricted access, we would need to copy 2 constants of - * {@link IWorkbenchConstants}. However, by referencing those we can verify their - * existence more easily by simply switching the target platform to a current version. */ -@SuppressWarnings("restriction") public class CheckFileOnOpenPartListener implements IPartListener2 { /** @@ -152,129 +133,13 @@ public void partVisible(IWorkbenchPartReference partRef) { * @return the editors file or null if the workbench part is no file based editor */ private IFile getEditorFile(IWorkbenchPartReference partRef) { - - if (!(partRef instanceof IEditorReference)) { - return null; - } - - IFile file = null; - IWorkbenchPart part = partRef.getPart(false); - // fix for 3522695 - // do *NOT* restore the part here to prevent startup issues with large - // number of opened files - // instead use a different path the rip the input file reference - - IEditorInput input = null; - - if (part instanceof IEditorPart) { - - IEditorPart editor = (IEditorPart) part; - input = editor.getEditorInput(); - } else { - - // fix for 3522695 - rip input file from editor ref without initializing - // the actual part - IEditorReference editRef = (IEditorReference) partRef; - input = getRestoredInput(editRef); - } - - if (input instanceof FileEditorInput) { - file = ((FileEditorInput) input).getFile(); - } - - return file; - } - - private IEditorInput getRestoredInput(IEditorReference editorRef) { - - IMemento editorMem = null; - if (CheckstyleUIPlugin.isE3()) { - editorMem = getMementoE3(editorRef); - } else { - editorMem = getMementoE4(editorRef); - } - - if (editorMem == null) { - return null; - } - IMemento inputMem = editorMem.getChild(IWorkbenchConstants.TAG_INPUT); - String factoryID = null; - if (inputMem != null) { - factoryID = inputMem.getString(IWorkbenchConstants.TAG_FACTORY_ID); - } - if (factoryID == null) { - return null; - } - IAdaptable input = null; - - IElementFactory factory = PlatformUI.getWorkbench().getElementFactory(factoryID); - if (factory == null) { - return null; - } - - input = factory.createElement(inputMem); - if (input == null) { - return null; - } - - if (!(input instanceof IEditorInput)) { - return null; - } - return (IEditorInput) input; - } - - private IMemento getMementoE4(IEditorReference editorRef) { - try { - - // can't use this as long as were still supporting E3 - // org.eclipse.e4.ui.model.application.MApplicationElement model = - // e.getModel(); - // Map state = model.getPersistedState() - - Method getModelMethod = editorRef.getClass().getMethod("getModel", new Class[0]); - getModelMethod.setAccessible(true); - - Object model = getModelMethod.invoke(editorRef, (Object[]) null); - - Method getPersistedStateMethod = model.getClass().getMethod("getPersistedState", - new Class[0]); - getPersistedStateMethod.setAccessible(true); - - @SuppressWarnings("unchecked") - Map state = (Map) getPersistedStateMethod.invoke(model, - (Object[]) null); - - String memento = state.get("memento"); - - if (memento != null) { - - try { - return XMLMemento.createReadRoot(new StringReader(memento)); - } catch (WorkbenchException ex) { - CheckstyleLog.log(ex); - } + if (partRef instanceof IEditorReference editorRef + && editorRef.getEditorInput() instanceof FileEditorInput input) { + return input.getFile(); } - } catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException ex) { - CheckstyleLog.log(ex); - } - - return null; - - } - - private IMemento getMementoE3(IEditorReference editorRef) { - - try { - - // the direct method vanished from E4 EditorReference class - // in order to build on E4 we need to do this the dirty way via reflection - Method getMementoMethod = editorRef.getClass().getMethod("getMemento", new Class[0]); - getMementoMethod.setAccessible(true); - - return (IMemento) getMementoMethod.invoke(editorRef, (Object[]) null); - } catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException ex) { - CheckstyleLog.log(ex); + } catch (PartInitException ex) { + throw new RuntimeException(ex); } return null; }