11/*
2- * Copyright 2009-2018 the original author or authors.
2+ * Copyright 2009-2020 the original author or authors.
33 *
44 * Licensed under the Apache License, Version 2.0 (the "License");
55 * you may not use this file except in compliance with the License.
66 * You may obtain a copy of the License at
77 *
8- * http ://www.apache.org/licenses/LICENSE-2.0
8+ * https ://www.apache.org/licenses/LICENSE-2.0
99 *
1010 * Unless required by applicable law or agreed to in writing, software
1111 * distributed under the License is distributed on an "AS IS" BASIS,
2727import org .codehaus .groovy .ast .ASTNode ;
2828import org .codehaus .groovy .ast .expr .ArgumentListExpression ;
2929import org .codehaus .groovy .ast .expr .ConstantExpression ;
30+ import org .codehaus .groovy .ast .expr .MethodCall ;
3031import org .codehaus .groovy .ast .expr .MethodCallExpression ;
32+ import org .codehaus .groovy .ast .expr .StaticMethodCallExpression ;
3133import org .codehaus .groovy .eclipse .GroovyPlugin ;
3234import org .codehaus .groovy .eclipse .codebrowsing .fragments .ASTFragmentKind ;
3335import org .codehaus .groovy .eclipse .codebrowsing .fragments .IASTFragment ;
@@ -52,7 +54,7 @@ public class ConvertToPropertyAction extends Action {
5254
5355 private final GroovyEditor editor ;
5456
55- public ConvertToPropertyAction (GroovyEditor editor ) {
57+ public ConvertToPropertyAction (final GroovyEditor editor ) {
5658 this .editor = editor ;
5759 setText ("Replace Accessor call with Property read/write" );
5860 setActionDefinitionId ("org.codehaus.groovy.eclipse.ui.convertToProperty" );
@@ -78,49 +80,56 @@ public void run() {
7880 }
7981 }
8082
81- public static TextEdit createEdit (GroovyCompilationUnit gcu , int pos , int len ) {
83+ public static TextEdit createEdit (final GroovyCompilationUnit gcu , final int pos , final int len ) {
8284 ModuleNodeInfo info = gcu .getModuleInfo (true );
8385 if (!info .isEmpty ()) {
86+ MethodCall call = null ;
87+
8488 ASTNode node = new ASTNodeFinder (new Region (pos , len )).doVisit (info .module );
8589 if (node instanceof ConstantExpression ) {
8690 IASTFragment fragment = new FindSurroundingNode (new Region (node )).doVisitSurroundingNode (info .module );
8791 if (fragment .kind () == ASTFragmentKind .METHOD_CALL ) {
88- MethodCallExpression call = (MethodCallExpression ) fragment .getAssociatedNode ();
89- if (call != null && !call .isUsingGenerics () && call .getArguments () instanceof ArgumentListExpression ) {
90- ArgumentListExpression args = (ArgumentListExpression ) call .getArguments ();
91-
92- Matcher match ; // check for accessor or mutator
93- if (args .getExpressions ().isEmpty () && (match = compile ("(?:get|is)(\\ p{javaJavaIdentifierPart}+)" ).matcher (call .getMethodAsString ())).matches ()) {
94- int offset = node .getStart (),
95- length = (args .getEnd () + 1 ) - offset ;
96- String propertyName = match .group (1 );
97-
98- // replace "getPropertyName()" with "propertyName"
99- return new ReplaceEdit (offset , length , decapitalize (propertyName ));
100-
101- } else if (args .getExpressions ().size () == 1 && (match = compile ("set(\\ p{javaJavaIdentifierPart}+)" ).matcher (call .getMethodAsString ())).matches ()) {
102- int offset = node .getStart (),
103- length = args .getStart () - offset ;
104- String propertyName = match .group (1 );
105-
106- // replace "setPropertyName(value_expression)" or "setPropertyName value_expression"
107- // with "propertyName = value_expression" (check prefs for spaces around assignment)
108- MultiTextEdit edits = new MultiTextEdit ();
109- Map <String , String > options = gcu .getJavaProject ().getOptions (true );
110- StringBuilder replacement = new StringBuilder (decapitalize (propertyName ));
111- if (JavaCore .INSERT .equals (options .get (FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR )))
112- replacement .append (' ' );
113- replacement .append ('=' );
114- if (JavaCore .INSERT .equals (options .get (FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR )))
115- replacement .append (' ' );
116-
117- edits .addChild (new ReplaceEdit (offset , length , replacement .toString ()));
118- if (gcu .getContents ()[args .getEnd ()] == ')' ) edits .addChild (new DeleteEdit (args .getEnd (), 1 ));
119-
120- return edits ;
121- }
92+ MethodCallExpression expr = (MethodCallExpression ) fragment .getAssociatedNode ();
93+ if (expr != null && !expr .isUsingGenerics ()) {
94+ call = expr ;
12295 }
12396 }
97+ } else if (node instanceof StaticMethodCallExpression ) {
98+ call = (StaticMethodCallExpression ) node ;
99+ }
100+
101+ if (call != null && call .getArguments () instanceof ArgumentListExpression ) {
102+ ArgumentListExpression args = (ArgumentListExpression ) call .getArguments ();
103+
104+ Matcher match ; // check for accessor or mutator
105+ if (args .getExpressions ().isEmpty () && (match = compile ("(?:get|is)(\\ p{javaJavaIdentifierPart}+)" ).matcher (call .getMethodAsString ())).matches ()) {
106+ int offset = node .getStart (), length = (args .getEnd () + 1 ) - offset ;
107+ String propertyName = match .group (1 );
108+
109+ // replace "getPropertyName()" with "propertyName"
110+ return new ReplaceEdit (offset , length , decapitalize (propertyName ));
111+
112+ } else if (args .getExpressions ().size () == 1 && (match = compile ("set(\\ p{javaJavaIdentifierPart}+)" ).matcher (call .getMethodAsString ())).matches ()) {
113+ int offset = node .getStart (), length = args .getStart () - offset ;
114+ String propertyName = match .group (1 );
115+
116+ // replace "setPropertyName(value_expression)" or "setPropertyName value_expression"
117+ // with "propertyName = value_expression" (check prefs for spaces around assignment)
118+ MultiTextEdit edits = new MultiTextEdit ();
119+ Map <String , String > options = gcu .getJavaProject ().getOptions (true );
120+ StringBuilder replacement = new StringBuilder (decapitalize (propertyName ));
121+ if (JavaCore .INSERT .equals (options .get (FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR ))) {
122+ replacement .append (' ' );
123+ }
124+ replacement .append ('=' );
125+ if (JavaCore .INSERT .equals (options .get (FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR ))) {
126+ replacement .append (' ' );
127+ }
128+ edits .addChild (new ReplaceEdit (offset , length , replacement .toString ()));
129+ if (gcu .getContents ()[args .getEnd ()] == ')' ) edits .addChild (new DeleteEdit (args .getEnd (), 1 ));
130+
131+ return edits ;
132+ }
124133 }
125134 }
126135 return null ;
0 commit comments