Skip to content

Commit

Permalink
formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
cjobinabo committed Aug 9, 2023
1 parent a352015 commit ee2837c
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 177 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ public class ChangeMethodInvocation extends Recipe {
Boolean performStaticCall;

@JsonCreator
public ChangeMethodInvocation(@NonNull @JsonProperty("methodPattern") String methodPattern,
@NonNull @JsonProperty("newMethodPattern") String newMethodPattern, @Nullable @JsonProperty("performStaticCall") Boolean performStaticCall) {
public ChangeMethodInvocation(@NonNull @JsonProperty("methodPattern") String methodPattern,
@NonNull @JsonProperty("newMethodPattern") String newMethodPattern, @Nullable @JsonProperty("performStaticCall") Boolean performStaticCall) {
this.methodPattern = methodPattern;
this.newMethodPattern = newMethodPattern;
this.performStaticCall = performStaticCall;
Expand Down Expand Up @@ -95,7 +95,7 @@ private class ChangeMethodInvocationVisitor extends JavaVisitor<ExecutionContext
private final MethodMatcher methodMatcher;
private MethodMatcher methodReplacmentMatcher;
private String oldMethodType;

private String newMethodType;
private String newMethodOwnerName;
private String newMethodName;
Expand All @@ -109,19 +109,19 @@ private ChangeMethodInvocationVisitor(MethodMatcher methodMatcher, String newMet

if (newMethodPattern != null) {
Matcher m = methodPatternMatcher.matcher(newMethodPattern);
if(m.find()) {
if (m.find()) {
this.newMethodType = m.group(1);
if(this.newMethodType != null) {
if (this.newMethodType != null) {
String[] parts = this.newMethodType.split("\\.");
this.newMethodOwnerName = parts[parts.length-1];
this.newMethodOwnerName = parts[parts.length - 1];
}

this.newMethodName = m.group(2);
this.newMethodsArgsStr = m.group(3);
if(this.newMethodsArgsStr != null) {
if (this.newMethodsArgsStr != null) {
StringBuilder newArgs = new StringBuilder();
int argSize = newMethodsArgsStr.split(",").length;
for(int i=1; i <= argSize; i++) {
for (int i = 1; i <= argSize; i++) {
newArgs.append("*");
if (i < argSize) {
newArgs.append(",");
Expand All @@ -142,11 +142,11 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu

if (methodReplacmentMatcher != null && methodMatcher.matches(method) && !methodReplacmentMatcher.matches(method)) {
if (performStaticCall) {
String temp = newMethodType+"."+newMethodName+"("+newMethodsArgsStr+");";
String temp = newMethodType + "." + newMethodName + "(" + newMethodsArgsStr + ");";
JavaTemplate addArgTemplate = JavaTemplate.builder(temp).build();
J.MethodInvocation qualifiedInvocation = addArgTemplate.apply(getCursor(), m.getCoordinates().replace());

temp = newMethodOwnerName+"."+newMethodName+"("+newMethodsArgsStr+");";
temp = newMethodOwnerName + "." + newMethodName + "(" + newMethodsArgsStr + ");";
addArgTemplate = JavaTemplate.builder(temp).build();
J.MethodInvocation simpleInvocation = addArgTemplate.apply(getCursor(), m.getCoordinates().replace());

Expand All @@ -164,21 +164,21 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
String previousMethodType = declaringType.getFullyQualifiedName();
declaringType = declaringType.withFullyQualifiedName(newMethodType);
type = type.withDeclaringType(declaringType);

//add new import and remove old if no longer used
maybeAddImport(newMethodType);
maybeRemoveImport(previousMethodType);
}
m = m.withName(m.getName().withSimpleName(newMethodName))
.withMethodType(type);
if(this.newMethodsArgsStr == null) {

if (this.newMethodsArgsStr == null) {
// clear arguments
List<Expression> methodArgs = Collections.singletonList(new J.Empty(Tree.randomId(), Space.EMPTY, Markers.EMPTY));
m = m.withArguments(methodArgs);
} else {
// override arguments
JavaTemplate addArgTemplate = JavaTemplate.builder( newMethodsArgsStr).build();
JavaTemplate addArgTemplate = JavaTemplate.builder(newMethodsArgsStr).build();
m = addArgTemplate.apply(getCursor(), m.getCoordinates().replaceArguments());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new ChangeStringLiteralVisitor<> (Pattern.compile(valuePattern), newValueTemplate);
return new ChangeStringLiteralVisitor<>(Pattern.compile(valuePattern), newValueTemplate);
}

@Value
Expand All @@ -81,7 +81,7 @@ public J.Literal visitLiteral(J.Literal literal, P p) {
String literalValue = literal.getValue().toString();
Matcher m = valuePattern.matcher(literalValue);
if (m.find()) {
literalValue = m.replaceFirst(newValueTemplate);
literalValue = m.replaceFirst(newValueTemplate);
literal = literal.withValue(literalValue).withValueSource(literalValue);
}
return literal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public String getDescription() {
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new MethodInvocationVisitor();
}

private class MethodInvocationVisitor extends JavaVisitor<ExecutionContext> {
MethodMatcher methodMatcher = new MethodMatcher("java.util.Hashtable put(java.lang.Object, java.lang.Object)", false);

Expand All @@ -65,16 +65,16 @@ private <M extends MethodCall> M visitMethodCall(M methodCall) {
return methodCall;
}
// Remove the method invocation when the argumentMatcherPredicate is true for all arguments
Expression firstArg = methodCall.getArguments().get(0);
Expression firstArg = methodCall.getArguments().get(0);
if (firstArg instanceof J.Literal) {
J.Literal literalExp = (J.Literal) firstArg;
Object value = literalExp.getValue();
if(!value.equals("java.naming.factory.initial") && !value.equals("java.naming.provider.url")) {
if (!value.equals("java.naming.factory.initial") && !value.equals("java.naming.provider.url")) {
return methodCall;
}
} else {
return methodCall;
}
}

if (methodCall.getMethodType() != null) {
maybeRemoveImport(methodCall.getMethodType().getDeclaringType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/
package org.openrewrite.xml.liberty;

import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.marker.JavaProject;

Expand Down Expand Up @@ -42,7 +43,7 @@ public String getDescription() {

public File getProjectDirectory(File sourceFile, String projectName) {
File parent = sourceFile.getParentFile();
while(parent != null && !parent.getName().equals(projectName)) {
while (parent != null && !parent.getName().equals(projectName)) {
parent = parent.getParentFile();
}

Expand All @@ -52,19 +53,19 @@ public File getProjectDirectory(File sourceFile, String projectName) {
public List<File> getSrcDirectories(SourceFile sourceFile) {
List<File> srcDirs = new ArrayList<File>();
Path sourcePath = sourceFile.getSourcePath();

String projectName = sourceFile.getMarkers()
.findFirst(JavaProject.class)
.map(JavaProject::getProjectName)
.orElse("");
.findFirst(JavaProject.class)
.map(JavaProject::getProjectName)
.orElse("");

File projectDirctory = getProjectDirectory(new File(sourcePath.toAbsolutePath().toString()), projectName);
if(projectDirctory != null) {
if (projectDirctory != null) {
File[] subDirs = projectDirctory.listFiles(File::isDirectory);
if(subDirs != null) {
for (File subDir: subDirs) {
if (subDirs != null) {
for (File subDir : subDirs) {
String dirName = subDir.getName().toLowerCase();
if(dirName.endsWith("src") || dirName.equals("source")) {
if (dirName.endsWith("src") || dirName.equals("source")) {
srcDirs.add(subDir);
}
}
Expand All @@ -84,37 +85,37 @@ public Tree visit(@Nullable Tree tree, ExecutionContext executionContext) {
SourceFile sourceFile = ((SourceFile) tree);

Path sourcePath = ((SourceFile) tree).getSourcePath();
if(sourcePath.getFileName().toString().equals("persistence.xml")) {
if (sourcePath.getFileName().toString().equals("persistence.xml")) {
String projectName = sourceFile.getMarkers()
.findFirst(JavaProject.class)
.map(JavaProject::getProjectName)
.orElse("");
.findFirst(JavaProject.class)
.map(JavaProject::getProjectName)
.orElse("");

List<File> srcDirs = getSrcDirectories(sourceFile);
boolean isValidPath = false;
boolean correctFileExists = false;
Path correctPath = null;
if(!srcDirs.isEmpty()) {
for(File srcDir: srcDirs) {
if (!srcDirs.isEmpty()) {
for (File srcDir : srcDirs) {
correctPath = srcDir.toPath().resolve("META-INF").resolve("persistence.xml");
if(sourcePath.toAbsolutePath().equals(correctPath.toAbsolutePath())) {
if (sourcePath.toAbsolutePath().equals(correctPath.toAbsolutePath())) {
isValidPath = true;
break;
}

if(correctPath.toFile().exists()) {
if (correctPath.toFile().exists()) {
correctFileExists = true;
}
}
} else {
if(!sourcePath.toAbsolutePath().endsWith("src"+File.separator+"META-INF"+File.separator+"persistence.xml")) {
if (!sourcePath.toAbsolutePath().endsWith("src" + File.separator + "META-INF" + File.separator + "persistence.xml")) {
File projectFile = getProjectDirectory(new File(sourcePath.toAbsolutePath().toString()), projectName);
correctPath = projectFile.toPath().resolve("src").resolve("META-INF").resolve("persistence.xml");
return ((SourceFile) tree).withSourcePath(correctPath);
}
}

if(!isValidPath && !correctFileExists && correctPath != null) {
if (!isValidPath && !correctFileExists && correctPath != null) {
return ((SourceFile) tree).withSourcePath(correctPath);
} else {
return sourceFile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,65 +22,65 @@
import static org.openrewrite.java.Assertions.java;

public class InvalidInitialContextTest implements RewriteTest {

String initialContextClass = """
package javax.naming;
package javax.naming;
public class InitialContext {
public class InitialContext {
public InitialContext() {
}
public InitialContext() {
}
public void lookup() {
}
public void lookup() {
}
}
""";
}
""";

@Test
void replaceTimeoutTest() {
rewriteRun(
spec -> spec.recipe(new ChangeStringLiteral("^java:/comp(.*)$", "java:comp$1")),
java(initialContextClass),
java(
"""
package com.test;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TestDetectInvalidInitialContext {
public static final String BAD_ENV = "java:/comp";
public static final String GOOD_ENV = "java:comp";
private void doX(){
InitialContext ic = new InitialContext();
ic.lookup("java:/comp");
ic.lookup("java:/comp/BadEntry");
ic.lookup("java:comp/GoodEntry");
}
}
""",
"""
package com.test;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TestDetectInvalidInitialContext {
public static final String BAD_ENV = java:comp;
public static final String GOOD_ENV = "java:comp";
private void doX(){
InitialContext ic = new InitialContext();
ic.lookup(java:comp);
ic.lookup(java:comp/BadEntry);
ic.lookup("java:comp/GoodEntry");
}
}
"""
)
spec -> spec.recipe(new ChangeStringLiteral("^java:/comp(.*)$", "java:comp$1")),
java(initialContextClass),
java(
"""
package com.test;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TestDetectInvalidInitialContext {
public static final String BAD_ENV = "java:/comp";
public static final String GOOD_ENV = "java:comp";
private void doX(){
InitialContext ic = new InitialContext();
ic.lookup("java:/comp");
ic.lookup("java:/comp/BadEntry");
ic.lookup("java:comp/GoodEntry");
}
}
""",
"""
package com.test;
import javax.naming.InitialContext;
import javax.naming.NamingException;
public class TestDetectInvalidInitialContext {
public static final String BAD_ENV = java:comp;
public static final String GOOD_ENV = "java:comp";
private void doX(){
InitialContext ic = new InitialContext();
ic.lookup(java:comp);
ic.lookup(java:comp/BadEntry);
ic.lookup("java:comp/GoodEntry");
}
}
"""
)
);
}

}
Loading

0 comments on commit ee2837c

Please sign in to comment.