generated from moderneinc/rewrite-recipe-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5951d2f
commit f9811c9
Showing
5 changed files
with
216 additions
and
1 deletion.
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
72 changes: 72 additions & 0 deletions
72
src/main/java/org/openrewrite/java/struts/search/FindStrutsActions.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,72 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 org.openrewrite.java.struts.search; | ||
|
||
import org.openrewrite.*; | ||
import org.openrewrite.java.struts.table.StrutsActions; | ||
import org.openrewrite.marker.SearchResult; | ||
import org.openrewrite.xml.XPathMatcher; | ||
import org.openrewrite.xml.XmlIsoVisitor; | ||
import org.openrewrite.xml.tree.Xml; | ||
|
||
import java.util.stream.Collectors; | ||
|
||
public class FindStrutsActions extends Recipe { | ||
private final transient StrutsActions actions = new StrutsActions(this); | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Find Struts actions"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Find actions and their associated definitions."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
XPathMatcher actionMatcher = new XPathMatcher("//action"); | ||
return Preconditions.check(new FindSourceFiles("**/struts.xml"), new XmlIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public Xml.Tag visitTag(Xml.Tag tag, ExecutionContext ctx) { | ||
if (actionMatcher.matches(getCursor())) { | ||
String pkg = getCursor().getPathAsStream(Xml.Tag.class::isInstance) | ||
.map(Xml.Tag.class::cast) | ||
.filter(t -> t.getName().equals("package")) | ||
.map(t -> getAttribute(t, "name", "")) | ||
.collect(Collectors.joining(".")); | ||
actions.insertRow(ctx, new StrutsActions.Row( | ||
getCursor().firstEnclosingOrThrow(SourceFile.class).getSourcePath().toString(), | ||
pkg, | ||
getAttribute(tag, "name", "unknown"), | ||
getAttribute(tag, "class", "unknown"), | ||
getAttribute(tag, "method", "unknown"))); | ||
return SearchResult.found(tag); | ||
} | ||
return super.visitTag(tag, ctx); | ||
} | ||
}); | ||
} | ||
|
||
private String getAttribute(Xml.Tag tag, String name, String defaultValue) { | ||
return tag.getAttributes().stream() | ||
.filter(a -> a.getKey().getName().equals(name)) | ||
.findFirst() | ||
.map(a -> a.getValue().getValue()) | ||
.orElse(defaultValue); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/org/openrewrite/java/struts/search/package-info.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,21 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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. | ||
*/ | ||
@NonNullApi | ||
@NonNullFields | ||
package org.openrewrite.java.struts.search; | ||
|
||
import org.openrewrite.internal.lang.NonNullApi; | ||
import org.openrewrite.internal.lang.NonNullFields; |
55 changes: 55 additions & 0 deletions
55
src/main/java/org/openrewrite/java/struts/table/StrutsActions.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,55 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 org.openrewrite.java.struts.table; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreType; | ||
import lombok.Value; | ||
import org.openrewrite.Column; | ||
import org.openrewrite.DataTable; | ||
import org.openrewrite.Recipe; | ||
|
||
@JsonIgnoreType | ||
public class StrutsActions extends DataTable<StrutsActions.Row> { | ||
|
||
public StrutsActions(Recipe recipe) { | ||
super(recipe, | ||
"Struts actions", | ||
"Definition of struts action."); | ||
} | ||
|
||
@Value | ||
public static class Row { | ||
@Column(displayName = "Source file", | ||
description = "The source file that the action is defined in.") | ||
String sourceFile; | ||
|
||
@Column(displayName = "Package", | ||
description = "The package of the action.") | ||
String pkg; | ||
|
||
@Column(displayName = "Action name", | ||
description = "The name of the action.") | ||
String name; | ||
|
||
@Column(displayName = "Class", | ||
description = "The action class.") | ||
String className; | ||
|
||
@Column(displayName = "Method name", | ||
description = "The method name of the action method.") | ||
String methodName; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/org/openrewrite/java/struts/search/FindStrutsActionTest.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,68 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* 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 | ||
* <p> | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* <p> | ||
* 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 org.openrewrite.java.struts.search; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.openrewrite.java.Assertions.srcMainResources; | ||
import static org.openrewrite.xml.Assertions.xml; | ||
|
||
public class FindStrutsActionTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new FindStrutsActions()); | ||
} | ||
|
||
@Test | ||
void findActions() { | ||
rewriteRun( | ||
srcMainResources( | ||
xml( | ||
//language=xml | ||
""" | ||
<struts> | ||
<package name="basicstruts2" extends="struts-default"> | ||
<action name="index"> | ||
<result>/index.jsp</result> | ||
</action> | ||
<action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute"> | ||
<result name="success">/HelloWorld.jsp</result> | ||
</action> | ||
</package> | ||
</struts> | ||
""", | ||
//language=xml | ||
""" | ||
<struts> | ||
<package name="basicstruts2" extends="struts-default"> | ||
<!--~~>--><action name="index"> | ||
<result>/index.jsp</result> | ||
</action> | ||
<!--~~>--><action name="hello" class="org.apache.struts.helloworld.action.HelloWorldAction" method="execute"> | ||
<result name="success">/HelloWorld.jsp</result> | ||
</action> | ||
</package> | ||
</struts> | ||
""", | ||
spec -> spec.path("struts.xml") | ||
) | ||
) | ||
); | ||
} | ||
} |