diff --git a/sandbox_cleanup_application/src/org/sandbox/jdt/core/cleanupapp/CodeCleanupApplication.java b/sandbox_cleanup_application/src/org/sandbox/jdt/core/cleanupapp/CodeCleanupApplication.java index eec0be28..b7a40a2e 100644 --- a/sandbox_cleanup_application/src/org/sandbox/jdt/core/cleanupapp/CodeCleanupApplication.java +++ b/sandbox_cleanup_application/src/org/sandbox/jdt/core/cleanupapp/CodeCleanupApplication.java @@ -8,11 +8,12 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import java.text.MessageFormat; +import java.util.HashMap; import java.util.Map; import java.util.Properties; -import java.util.HashMap; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IConfigurationElement; import org.eclipse.core.runtime.IExtension; import org.eclipse.core.runtime.IExtensionPoint; import org.eclipse.core.runtime.IStatus; @@ -30,7 +31,6 @@ import org.eclipse.jface.text.IDocument; import org.eclipse.osgi.util.NLS; import org.eclipse.text.edits.TextEdit; -import org.eclipse.core.runtime.IConfigurationElement; public class CodeCleanupApplication implements IApplication { private static final String ARG_CONFIG = "-config"; //$NON-NLS-1$ @@ -178,7 +178,7 @@ private void formatFile(File file, CodeFormatter codeFormatter) { try (BufferedWriter out = new BufferedWriter(new FileWriter(file,StandardCharsets.UTF_8));){ out.write(doc.get()); out.flush(); - } + } } catch (IOException e) { String errorMessage = Messages.bind(Messages.CaughtException, "IOException", e.getLocalizedMessage()); //$NON-NLS-1$ Util.log(e, errorMessage); @@ -388,7 +388,7 @@ private void formatDirTree(File dir, CodeFormatter codeFormatter) { } } - public static CodeFormatter createCodeFormatter(Map options, int mode) { + private static CodeFormatter createCodeFormatter(Map options, int mode) { if (options == null) options = JavaCore.getOptions(); Map currentOptions = new HashMap(options); if (mode == M_FORMAT_NEW) { @@ -403,14 +403,12 @@ public static CodeFormatter createCodeFormatter(Map options, int mode) { IExtensionPoint extension = Platform.getExtensionRegistry().getExtensionPoint(JavaCore.PLUGIN_ID, JavaCore.JAVA_FORMATTER_EXTENSION_POINT_ID); if (extension != null) { - IExtension[] extensions = extension.getExtensions(); - for (int i = 0; i < extensions.length; i++) { - IConfigurationElement[] configElements = extensions[i].getConfigurationElements(); - for (int j = 0; j < configElements.length; j++) { - String initializerID = configElements[j].getAttribute("id"); //$NON-NLS-1$ + for (IExtension extension2 : extension.getExtensions()) { + for (IConfigurationElement configElement : extension2.getConfigurationElements()) { + String initializerID = configElement.getAttribute("id"); //$NON-NLS-1$ if (initializerID != null && initializerID.equals(formatterId)) { try { - Object execExt = configElements[j].createExecutableExtension("class"); //$NON-NLS-1$ + Object execExt = configElement.createExecutableExtension("class"); //$NON-NLS-1$ if (execExt instanceof CodeFormatter) { CodeFormatter formatter = (CodeFormatter) execExt; formatter.setOptions(currentOptions); diff --git a/sandbox_common/src/org/sandbox/jdt/internal/common/HelperVisitor.java b/sandbox_common/src/org/sandbox/jdt/internal/common/HelperVisitor.java index f51cc766..99e75977 100644 --- a/sandbox_common/src/org/sandbox/jdt/internal/common/HelperVisitor.java +++ b/sandbox_common/src/org/sandbox/jdt/internal/common/HelperVisitor.java @@ -365,6 +365,20 @@ public BiPredicate addClassInstanceCreation(BiPredicate addClassInstanceCreation(Class typeof, BiPredicate bs) { + Map map = Map.ofEntries( + new AbstractMap.SimpleEntry<>(TYPEOF, typeof) + ); + predicatedata.put(VisitorEnum.ClassInstanceCreation, map); + return predicatemap.put(VisitorEnum.ClassInstanceCreation, bs); + } + /** * * @param bs @@ -3670,6 +3684,24 @@ public static void callClassInstanceCreationVisitor(ASTNode node, Referen hv.addClassInstanceCreation(bs); hv.build(node); } + + /** + * + * @param + * @param + * @param class1 + * @param node + * @param dataholder + * @param nodesprocessed + * @param bs + */ + public static void callClassInstanceCreationVisitor(Class class1, ASTNode node, ReferenceHolder dataholder, Set nodesprocessed, + BiPredicate> bs) { + + HelperVisitor,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder); + hv.addClassInstanceCreation(class1, bs); + hv.build(node); + } /** * diff --git a/sandbox_platform_helper/src/org/sandbox/jdt/internal/corext/fix/SimplifyPlatformStatusFixCore.java b/sandbox_platform_helper/src/org/sandbox/jdt/internal/corext/fix/SimplifyPlatformStatusFixCore.java index 77af6fd8..97937585 100644 --- a/sandbox_platform_helper/src/org/sandbox/jdt/internal/corext/fix/SimplifyPlatformStatusFixCore.java +++ b/sandbox_platform_helper/src/org/sandbox/jdt/internal/corext/fix/SimplifyPlatformStatusFixCore.java @@ -26,6 +26,7 @@ import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.util.Messages; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.helper.AbstractSimplifyPlatformStatus; import org.sandbox.jdt.internal.corext.fix.helper.StatusErrorSimplifyPlatformStatus; import org.sandbox.jdt.internal.corext.fix.helper.StatusInfoSimplifyPlatformStatus; @@ -38,15 +39,15 @@ public enum SimplifyPlatformStatusFixCore { STATUSERROR(new StatusErrorSimplifyPlatformStatus()), STATUSINFO(new StatusInfoSimplifyPlatformStatus()); - AbstractSimplifyPlatformStatus explicitencoding; + AbstractSimplifyPlatformStatus platformstatus; @SuppressWarnings("unchecked") SimplifyPlatformStatusFixCore(AbstractSimplifyPlatformStatus explicitencoding) { - this.explicitencoding= (AbstractSimplifyPlatformStatus) explicitencoding; + this.platformstatus= (AbstractSimplifyPlatformStatus) explicitencoding; } public String getPreview(boolean i) { - return explicitencoding.getPreview(i); + return platformstatus.getPreview(i); } /** @@ -62,10 +63,10 @@ public String getPreview(boolean i) { public void findOperations(final CompilationUnit compilationUnit, final Set operations, final Set nodesprocessed) throws CoreException { - explicitencoding.find(this, compilationUnit, operations, nodesprocessed); + platformstatus.find(this, compilationUnit, operations, nodesprocessed); } - public CompilationUnitRewriteOperation rewrite(final ClassInstanceCreation visited) { + public CompilationUnitRewriteOperation rewrite(final ClassInstanceCreation visited, ReferenceHolder holder) { return new CompilationUnitRewriteOperation() { @Override public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel) @@ -75,7 +76,7 @@ public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedPropo new Object[] { SimplifyPlatformStatusFixCore.this.toString() }), cuRewrite); cuRewrite.getASTRewrite().setTargetSourceRangeComputer(computer); - explicitencoding.rewrite(SimplifyPlatformStatusFixCore.this, visited, cuRewrite, group); + platformstatus.rewrite(SimplifyPlatformStatusFixCore.this, visited, cuRewrite, group, holder); } }; } diff --git a/sandbox_platform_helper/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractSimplifyPlatformStatus.java b/sandbox_platform_helper/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractSimplifyPlatformStatus.java index 2c09580e..905b79f8 100644 --- a/sandbox_platform_helper/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractSimplifyPlatformStatus.java +++ b/sandbox_platform_helper/src/org/sandbox/jdt/internal/corext/fix/helper/AbstractSimplifyPlatformStatus.java @@ -15,17 +15,15 @@ import java.util.List; import java.util.Set; +//import java.util.function.BiPredicate; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.Status; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTNode; -import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.ClassInstanceCreation; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.Expression; -import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.MethodInvocation; import org.eclipse.jdt.core.dom.Name; import org.eclipse.jdt.core.dom.QualifiedName; @@ -37,6 +35,8 @@ import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; import org.eclipse.jdt.internal.corext.refactoring.structure.ImportRemover; import org.eclipse.text.edits.TextEditGroup; +import org.sandbox.jdt.internal.common.HelperVisitor; +import org.sandbox.jdt.internal.common.ReferenceHolder; import org.sandbox.jdt.internal.corext.fix.SimplifyPlatformStatusFixCore; /** @@ -61,7 +61,7 @@ public AbstractSimplifyPlatformStatus(String methodname, String istatus) { * @return simple name of a class if the import was added and fully qualified * name if there was a conflict */ - protected Name addImport(String typeName, final CompilationUnitRewrite cuRewrite, AST ast) { + protected static Name addImport(String typeName, final CompilationUnitRewrite cuRewrite, AST ast) { String importedName= cuRewrite.getImportRewrite().addImport(typeName); return ast.newName(importedName); } @@ -71,37 +71,49 @@ protected Name addImport(String typeName, final CompilationUnitRewrite cuRewrite public void find(SimplifyPlatformStatusFixCore fixcore, CompilationUnit compilationUnit, Set operations, Set nodesprocessed) throws CoreException { try { - compilationUnit.accept(new ASTVisitor() { - @Override - public boolean visit(final ClassInstanceCreation visited) { - if (nodesprocessed.contains(visited) || ( - (visited.arguments().size() != 3) - && (visited.arguments().size() != 4) - && (visited.arguments().size() != 5) - )) { - return false; - } - - ITypeBinding binding= visited.resolveTypeBinding(); - if ((binding != null) && (Status.class.getSimpleName().equals(binding.getName()))) { - List arguments= visited.arguments(); - if (istatus.equals(arguments.get(0).toString())) { - operations.add(fixcore.rewrite(visited)); - nodesprocessed.add(visited); - return false; - } - } - - return true; + ReferenceHolder dataholder= new ReferenceHolder<>(); + HelperVisitor.callClassInstanceCreationVisitor(Status.class, compilationUnit, dataholder, nodesprocessed, (visited, holder) -> { + if (nodesprocessed.contains(visited) || ( +// (visited.arguments().size() != 3)&& +// (visited.arguments().size() != 4)&& + (visited.arguments().size() != 5) + )) { + return false; + } + /** + * new Status(INFO, callerClass, OK, message, null); + * new Status(WARNING, callerClass, OK, message, null); + * new Status(WARNING, callerClass, OK, message, exception); + * new Status(ERROR, callerClass, OK, message, null); + * new Status(ERROR, callerClass, OK, message, exception); + * + * + * IStatus status = new Status(IStatus.WARNING, "plugin id", IStatus.OK, "important message", e); + * IStatus status = new Status(IStatus.WARNING, "plugin id", "important message", null); + * IStatus status = new Status(IStatus.WARNING, "plugin id", "important message"); + */ + List arguments= visited.arguments(); + QualifiedName argstring3 = (QualifiedName) arguments.get(2); + if (!"IStatus.OK".equals(argstring3.toString())) { //$NON-NLS-1$ + return false; } +// QualifiedName argstring5 = (QualifiedName) arguments.get(4); + QualifiedName argstring1 = (QualifiedName) arguments.get(0); + String mybinding= argstring1.getFullyQualifiedName(); + if (istatus.equals(argstring1.toString())) { + operations.add(fixcore.rewrite(visited,holder)); + nodesprocessed.add(visited); + return false; + } + return true; }); } catch (Exception e) { - throw new CoreException(new Status(IStatus.ERROR, "sandbox_platform_helper", "Problem in find", e)); //$NON-NLS-1$ //$NON-NLS-2$ + throw new CoreException(Status.error("Problem in find", e)); //$NON-NLS-1$ } } public void rewrite(SimplifyPlatformStatusFixCore upp, final ClassInstanceCreation visited, - final CompilationUnitRewrite cuRewrite, TextEditGroup group) { + final CompilationUnitRewrite cuRewrite, TextEditGroup group, ReferenceHolder holder) { ASTRewrite rewrite= cuRewrite.getASTRewrite(); AST ast= cuRewrite.getRoot().getAST(); ImportRewrite importRewrite= cuRewrite.getImportRewrite(); @@ -115,7 +127,8 @@ public void rewrite(SimplifyPlatformStatusFixCore upp, final ClassInstanceCreati staticCall.setName(ast.newSimpleName(methodname)); List arguments= visited.arguments(); List staticCallArguments= staticCall.arguments(); - int positionmessage= arguments.size() == 5 ? 3 : 2; +// int positionmessage= arguments.size() == 5 ? 3 : 2; + int positionmessage= 3; staticCallArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(arguments.get(positionmessage)))); ASTNode node2= arguments.get(2); @@ -130,12 +143,14 @@ public void rewrite(SimplifyPlatformStatusFixCore upp, final ClassInstanceCreati } break; case 4: - ASTNode node= arguments.get(3); - if (!node.toString().equals("null")) { //$NON-NLS-1$ - staticCallArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node))); - } +// return; +// ASTNode node= arguments.get(3); +// if (!node.toString().equals("null")) { //$NON-NLS-1$ +// staticCallArguments.add(ASTNodes.createMoveTarget(rewrite, ASTNodes.getUnparenthesedExpression(node))); +// } break; case 3: +// return; default: break; } diff --git a/sandbox_platform_helper_test/src/org/sandbox/jdt/ui/tests/quickfix/Java9CleanUpTest.java b/sandbox_platform_helper_test/src/org/sandbox/jdt/ui/tests/quickfix/Java9CleanUpTest.java index c2cb7075..3fd2a514 100644 --- a/sandbox_platform_helper_test/src/org/sandbox/jdt/ui/tests/quickfix/Java9CleanUpTest.java +++ b/sandbox_platform_helper_test/src/org/sandbox/jdt/ui/tests/quickfix/Java9CleanUpTest.java @@ -16,24 +16,10 @@ public class Java9CleanUpTest { enum PlatformStatusPatterns { - STATUSWARNING3("package test1;\n" - + "import org.eclipse.core.runtime.IStatus;\n" - + "import org.eclipse.core.runtime.Status;\n" - + "public class E1 {\n" - + " IStatus status = new Status(IStatus.WARNING, \"plugin id\",\"important message\");\n" - + "}", - - "package test1;\n" - + "import org.eclipse.core.runtime.IStatus;\n" - + "import org.eclipse.core.runtime.Status;\n" - + "public class E1 {\n" - + " IStatus status = Status.warning(\"important message\");\n" - + "}"), - STATUSWARNING4("package test1;\n" + STATUSWARNING5("package test1;\n" + "import org.eclipse.core.runtime.IStatus;\n" + "import org.eclipse.core.runtime.Status;\n" + "public class E1 {\n" - + " IStatus status = new Status(IStatus.WARNING, \"plugin id\", \"important message\", null);\n" + " void bla(Throwable e) {\n" + " IStatus status = new Status(IStatus.WARNING, \"plugin id\", IStatus.OK, \"important message\", e);\n" + " }\n" @@ -43,17 +29,16 @@ enum PlatformStatusPatterns { + "import org.eclipse.core.runtime.IStatus;\n" + "import org.eclipse.core.runtime.Status;\n" + "public class E1 {\n" - + " IStatus status = Status.warning(\"important message\");\n" + " void bla(Throwable e) {\n" + " IStatus status = Status.warning(\"important message\", e);\n" + " }\n" + "}"), - STATUSWARNING5("package test1;\n" + STATUSERROR5("package test1;\n" + "import org.eclipse.core.runtime.IStatus;\n" + "import org.eclipse.core.runtime.Status;\n" + "public class E1 {\n" + " void bla(Throwable e) {\n" - + " IStatus status = new Status(IStatus.WARNING, \"plugin id\", IStatus.OK, \"important message\", e);\n" + + " IStatus status = new Status(IStatus.ERROR, \"plugin id\", IStatus.OK, \"important message\", e);\n" + " }\n" + "}", @@ -62,34 +47,25 @@ enum PlatformStatusPatterns { + "import org.eclipse.core.runtime.Status;\n" + "public class E1 {\n" + " void bla(Throwable e) {\n" - + " IStatus status = Status.warning(\"important message\", e);\n" + + " IStatus status = Status.error(\"important message\", e);\n" + " }\n" + "}"), - STATUSERROR("package test1;\n" - + "import org.eclipse.core.runtime.IStatus;\n" - + "import org.eclipse.core.runtime.Status;\n" - + "public class E1 {\n" - + " IStatus status = new Status(IStatus.ERROR, \"plugin id\", \"important message\", null);\n" - + "}", - - "package test1;\n" - + "import org.eclipse.core.runtime.IStatus;\n" - + "import org.eclipse.core.runtime.Status;\n" - + "public class E1 {\n" - + " IStatus status = Status.error(\"important message\");\n" - + "}"), - STATUSINFO("package test1;\n" + STATUSINFO5("package test1;\n" + "import org.eclipse.core.runtime.IStatus;\n" + "import org.eclipse.core.runtime.Status;\n" + "public class E1 {\n" - + " IStatus status = new Status(IStatus.INFO, \"plugin id\", \"important message\", null);\n" + + " void bla(Throwable e) {\n" + + " IStatus status = new Status(IStatus.INFO, \"plugin id\", IStatus.OK, \"important message\", e);\n" + + " }\n" + "}", "package test1;\n" + "import org.eclipse.core.runtime.IStatus;\n" + "import org.eclipse.core.runtime.Status;\n" + "public class E1 {\n" - + " IStatus status = Status.info(\"important message\");\n" + + " void bla(Throwable e) {\n" + + " IStatus status = Status.info(\"important message\", e);\n" + + " }\n" + "}"); PlatformStatusPatterns(String given, String expected) { @@ -109,7 +85,15 @@ public void testPlatformStatusParametrized(PlatformStatusPatterns test) throws C context.assertRefactoringResultAsExpected(new ICompilationUnit[] {cu}, new String[] {test.expected}, null); } - + @ParameterizedTest + @EnumSource(PlatformStatusPatternsDontTouch.class) + public void testPlatformStatus_donttouch(PlatformStatusPatternsDontTouch test) throws CoreException { + IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); + ICompilationUnit cu= pack.createCompilationUnit("E1.java", test.given, false, null); + context.enable(MYCleanUpConstants.SIMPLIFY_STATUS_CLEANUP); + context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); + } + enum PlatformStatusPatternsDontTouch { SIMPLE("package test1;\n" // @@ -120,11 +104,45 @@ enum PlatformStatusPatternsDontTouch { + "import java.io.FileInputStream;\n" + "import java.io.FileNotFoundException;\n" // + "import java.io.UnsupportedEncodingException;\n" // - + "public class E2 {\n" // + + "public class E1 {\n" // + " void method() throws UnsupportedEncodingException, IOException {\n" // + " }\n" // + "}\n") // , +// STATUSWARNING3("package test1;\n" +// + "import org.eclipse.core.runtime.IStatus;\n" +// + "import org.eclipse.core.runtime.Status;\n" +// + "public class E1 {\n" +// + " void bla(Throwable e) {\n" +// + " IStatus status = new Status(IStatus.WARNING, \"plugin id\",\"important message\");\n" +// + " }\n" +// + "}"), +// STATUSWARNING4("package test1;\n" +// + "import org.eclipse.core.runtime.IStatus;\n" +// + "import org.eclipse.core.runtime.Status;\n" +// + "public class E1 {\n" +// + " IStatus status = new Status(IStatus.WARNING, \"plugin id\", \"important message\", null);\n" +// + " void bla(Throwable e) {\n" +// + " IStatus status = new Status(IStatus.WARNING, \"plugin id\", IStatus.OK, \"important message\", e);\n" +// + " }\n" +// + "}"), +// STATUSERROR4("package test1;\n" +// + "import org.eclipse.core.runtime.IStatus;\n" +// + "import org.eclipse.core.runtime.Status;\n" +// + "public class E1 {\n" +// + " void bla(Throwable e) {\n" +// + " IStatus status = new Status(IStatus.ERROR, \"plugin id\", \"important message\", null);\n" +// + " }\n" +// + "}"), +// STATUSINFO4("package test1;\n" +// + "import org.eclipse.core.runtime.IStatus;\n" +// + "import org.eclipse.core.runtime.Status;\n" +// + "public class E1 {\n" +// + " void bla(Throwable e) {\n" +// + " IStatus status = new Status(IStatus.INFO, \"plugin id\", \"important message\", null);\n" +// + " }\n" +// + "}") +// , // WRONGSECONDPARAM("package test1;\n" // + "import org.eclipse.core.runtime.IStatus;\n" // + "import org.eclipse.core.runtime.Status;\n" @@ -143,12 +161,12 @@ enum PlatformStatusPatternsDontTouch { String given; } - @ParameterizedTest - @EnumSource(PlatformStatusPatternsDontTouch.class) - public void testPlatformStatus_donttouch(PlatformStatusPatternsDontTouch test) throws CoreException { - IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); - ICompilationUnit cu= pack.createCompilationUnit("E2.java", test.given, false, null); - context.enable(MYCleanUpConstants.SIMPLIFY_STATUS_CLEANUP); - context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); - } +// @ParameterizedTest +// @EnumSource(PlatformStatusPatternsDontTouch.class) +// public void testPlatformStatus_donttouch(PlatformStatusPatternsDontTouch test) throws CoreException { +// IPackageFragment pack= context.getfSourceFolder().createPackageFragment("test1", false, null); +// ICompilationUnit cu= pack.createCompilationUnit("E2.java", test.given, false, null); +// context.enable(MYCleanUpConstants.SIMPLIFY_STATUS_CLEANUP); +// context.assertRefactoringHasNoChange(new ICompilationUnit[] { cu }); +// } }