Skip to content
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

ignore case with 3 and 4 parameters only 5 parameter case #51

Merged
merged 13 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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$
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,20 @@ public BiPredicate<? extends ASTNode, E> addClassInstanceCreation(BiPredicate<Cl
return predicatemap.put(VisitorEnum.ClassInstanceCreation, bs);
}

/**
*
* @param typeof
* @param bs
* @return old BiPredicate assigned for nodetype
*/
public BiPredicate<? extends ASTNode, E> addClassInstanceCreation(Class<?> typeof, BiPredicate<ClassInstanceCreation, E> bs) {
Map<String, Object> map = Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>(TYPEOF, typeof)
);
predicatedata.put(VisitorEnum.ClassInstanceCreation, map);
return predicatemap.put(VisitorEnum.ClassInstanceCreation, bs);
}

/**
*
* @param bs
Expand Down Expand Up @@ -3670,6 +3684,24 @@ public static <V, T> void callClassInstanceCreationVisitor(ASTNode node, Referen
hv.addClassInstanceCreation(bs);
hv.build(node);
}

/**
*
* @param <V>
* @param <T>
* @param class1
* @param node
* @param dataholder
* @param nodesprocessed
* @param bs
*/
public static <V, T> void callClassInstanceCreationVisitor(Class<?> class1, ASTNode node, ReferenceHolder<V, T> dataholder, Set<ASTNode> nodesprocessed,
BiPredicate<ClassInstanceCreation, ReferenceHolder<V, T>> bs) {

HelperVisitor<ReferenceHolder<V, T>,V,T> hv= new HelperVisitor<>(nodesprocessed, dataholder);
hv.addClassInstanceCreation(class1, bs);
hv.build(node);
}

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,15 +39,15 @@ public enum SimplifyPlatformStatusFixCore {
STATUSERROR(new StatusErrorSimplifyPlatformStatus()),
STATUSINFO(new StatusInfoSimplifyPlatformStatus());

AbstractSimplifyPlatformStatus<ASTNode> explicitencoding;
AbstractSimplifyPlatformStatus<ASTNode> platformstatus;

@SuppressWarnings("unchecked")
SimplifyPlatformStatusFixCore(AbstractSimplifyPlatformStatus<? extends ASTNode> explicitencoding) {
this.explicitencoding= (AbstractSimplifyPlatformStatus<ASTNode>) explicitencoding;
this.platformstatus= (AbstractSimplifyPlatformStatus<ASTNode>) explicitencoding;
}

public String getPreview(boolean i) {
return explicitencoding.getPreview(i);
return platformstatus.getPreview(i);
}

/**
Expand All @@ -62,10 +63,10 @@ public String getPreview(boolean i) {
public void findOperations(final CompilationUnit compilationUnit,
final Set<CompilationUnitRewriteOperation> operations, final Set<ASTNode> 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<ASTNode, Object> holder) {
return new CompilationUnitRewriteOperation() {
@Override
public void rewriteAST(final CompilationUnitRewrite cuRewrite, final LinkedProposalModelCore linkedModel)
Expand All @@ -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);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand All @@ -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);
}
Expand All @@ -71,37 +71,49 @@ protected Name addImport(String typeName, final CompilationUnitRewrite cuRewrite
public void find(SimplifyPlatformStatusFixCore fixcore, CompilationUnit compilationUnit,
Set<CompilationUnitRewriteOperation> operations, Set<ASTNode> 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<Expression> arguments= visited.arguments();
if (istatus.equals(arguments.get(0).toString())) {
operations.add(fixcore.rewrite(visited));
nodesprocessed.add(visited);
return false;
}
}

return true;
ReferenceHolder<ASTNode, Object> 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)
)) {
Comment on lines +76 to +80

Check warning

Code scanning / PMD

Useless parentheses. Warning

Useless parentheses.
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<Expression> 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();

Check warning

Code scanning / PMD

Avoid unused local variables such as 'element'. Warning

Avoid unused local variables such as 'mybinding'.

Check notice

Code scanning / Pmd (reported by Codacy)

Avoid unused local variables such as 'mybinding'. Note

Avoid unused local variables such as 'mybinding'.
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<ASTNode, Object> holder) {
ASTRewrite rewrite= cuRewrite.getASTRewrite();
AST ast= cuRewrite.getRoot().getAST();
ImportRewrite importRewrite= cuRewrite.getImportRewrite();
Expand All @@ -115,7 +127,8 @@ public void rewrite(SimplifyPlatformStatusFixCore upp, final ClassInstanceCreati
staticCall.setName(ast.newSimpleName(methodname));
List<ASTNode> arguments= visited.arguments();
List<ASTNode> 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);
Expand All @@ -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;
}
Expand Down
Loading