1
1
/*
2
- * Copyright 2009-2018 the original author or authors.
2
+ * Copyright 2009-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
6
6
* You may obtain a copy of the License at
7
7
*
8
- * http ://www.apache.org/licenses/LICENSE-2.0
8
+ * https ://www.apache.org/licenses/LICENSE-2.0
9
9
*
10
10
* Unless required by applicable law or agreed to in writing, software
11
11
* distributed under the License is distributed on an "AS IS" BASIS,
27
27
import org .codehaus .groovy .ast .ASTNode ;
28
28
import org .codehaus .groovy .ast .expr .ArgumentListExpression ;
29
29
import org .codehaus .groovy .ast .expr .ConstantExpression ;
30
+ import org .codehaus .groovy .ast .expr .MethodCall ;
30
31
import org .codehaus .groovy .ast .expr .MethodCallExpression ;
32
+ import org .codehaus .groovy .ast .expr .StaticMethodCallExpression ;
31
33
import org .codehaus .groovy .eclipse .GroovyPlugin ;
32
34
import org .codehaus .groovy .eclipse .codebrowsing .fragments .ASTFragmentKind ;
33
35
import org .codehaus .groovy .eclipse .codebrowsing .fragments .IASTFragment ;
@@ -52,7 +54,7 @@ public class ConvertToPropertyAction extends Action {
52
54
53
55
private final GroovyEditor editor ;
54
56
55
- public ConvertToPropertyAction (GroovyEditor editor ) {
57
+ public ConvertToPropertyAction (final GroovyEditor editor ) {
56
58
this .editor = editor ;
57
59
setText ("Replace Accessor call with Property read/write" );
58
60
setActionDefinitionId ("org.codehaus.groovy.eclipse.ui.convertToProperty" );
@@ -78,49 +80,56 @@ public void run() {
78
80
}
79
81
}
80
82
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 ) {
82
84
ModuleNodeInfo info = gcu .getModuleInfo (true );
83
85
if (!info .isEmpty ()) {
86
+ MethodCall call = null ;
87
+
84
88
ASTNode node = new ASTNodeFinder (new Region (pos , len )).doVisit (info .module );
85
89
if (node instanceof ConstantExpression ) {
86
90
IASTFragment fragment = new FindSurroundingNode (new Region (node )).doVisitSurroundingNode (info .module );
87
91
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 ;
122
95
}
123
96
}
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
+ }
124
133
}
125
134
}
126
135
return null ;
0 commit comments