1515 */
1616package org .openrewrite .java .migrate ;
1717
18- import org .openrewrite .Cursor ;
19- import org .openrewrite .ExecutionContext ;
20- import org .openrewrite .Recipe ;
21- import org .openrewrite .TreeVisitor ;
18+ import org .openrewrite .*;
2219import org .openrewrite .internal .ListUtils ;
2320import org .openrewrite .java .JavaIsoVisitor ;
2421import org .openrewrite .java .JavaTemplate ;
2522import org .openrewrite .java .MethodMatcher ;
23+ import org .openrewrite .java .search .UsesJavaVersion ;
2624import org .openrewrite .java .tree .J ;
2725import org .openrewrite .java .tree .Statement ;
2826
@@ -46,38 +44,39 @@ public String getDescription() {
4644
4745 @ Override
4846 public TreeVisitor <?, ExecutionContext > getVisitor () {
49- return new JavaIsoVisitor <ExecutionContext >() {
50- public J .Block visitBlock (J .Block block , ExecutionContext ctx ) {
51- MethodMatcher SETSECURITY_REF = new MethodMatcher ("java.lang.System setSecurityManager(java.lang.SecurityManager) " , true );
52- List <Statement > statements = block .getStatements ();
53- boolean propertySet = false ;
54- for (int i = 0 ; i < statements .size () ; i ++) {
55- Statement stmt = statements .get (i );
56- if (stmt instanceof J .MethodInvocation ) {
57- if (SETSECURITY_REF .matches ((J .MethodInvocation ) stmt )) {
58- for (Statement statement : statements ) {
59- if (statement instanceof J .MethodInvocation &&
60- ((J .MethodInvocation ) statement ).getSimpleName ().equals ("setProperty" ) &&
61- ((J .MethodInvocation ) statement ).getArguments ().get (0 ) instanceof J .Literal &&
62- ((J .Literal ) ((J .MethodInvocation ) statement ).getArguments ().get (0 )).getValue ().equals ("java.security.manager" )) {
63- propertySet = true ;
64- break ;
47+ return Preconditions .check (new UsesJavaVersion <>(18 ),
48+ new JavaIsoVisitor <ExecutionContext >() {
49+ public J .Block visitBlock (J .Block block , ExecutionContext ctx ) {
50+ MethodMatcher SETSECURITY_REF = new MethodMatcher ("java.lang.System setSecurityManager(java.lang.SecurityManager) " , true );
51+ List <Statement > statements = block .getStatements ();
52+ boolean propertySet = false ;
53+ for (int i = 0 ; i < statements .size (); i ++) {
54+ Statement stmt = statements .get (i );
55+ if (stmt instanceof J .MethodInvocation ) {
56+ if (SETSECURITY_REF .matches ((J .MethodInvocation ) stmt )) {
57+ for (Statement statement : statements ) {
58+ if (statement instanceof J .MethodInvocation &&
59+ ((J .MethodInvocation ) statement ).getSimpleName ().equals ("setProperty" ) &&
60+ ((J .MethodInvocation ) statement ).getArguments ().get (0 ) instanceof J .Literal &&
61+ ((J .Literal ) ((J .MethodInvocation ) statement ).getArguments ().get (0 )).getValue ().equals ("java.security.manager" )) {
62+ propertySet = true ;
63+ break ;
64+ }
65+ }
66+ String templateString = "System.setProperty(\" java.security.manager\" , \" allow\" );" ;
67+ if (!propertySet ) {
68+ stmt = JavaTemplate .builder (templateString )
69+ .contextSensitive ()
70+ .build ().apply (new Cursor (getCursor (), stmt ),
71+ stmt .getCoordinates ().replace ());
72+ statements = ListUtils .insert (statements , stmt , i );
73+ return block .withStatements (statements );
74+ }
6575 }
6676 }
67- String templateString = "System.setProperty(\" java.security.manager\" , \" allow\" );" ;
68- if (!propertySet ) {
69- stmt = JavaTemplate .builder (templateString )
70- .contextSensitive ()
71- .build ().apply (new Cursor (getCursor (), stmt ),
72- stmt .getCoordinates ().replace ());
73- statements = ListUtils .insert (statements , stmt , i );
74- return block .withStatements (statements );
75- }
7677 }
78+ return super .visitBlock (block , ctx );
7779 }
78- }
79- return super .visitBlock (block , ctx );
80- }
81- };
80+ });
8281 }
8382}
0 commit comments