Skip to content

Commit 143b396

Browse files
committed
Fix for #738: Introspector.decapitalize implements bean name rules
1 parent 68b5ef4 commit 143b396

File tree

3 files changed

+38
-86
lines changed

3 files changed

+38
-86
lines changed

ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/actions/ConvertToPropertyActionTests.groovy

+34-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -18,9 +18,12 @@ package org.codehaus.groovy.eclipse.test.actions
1818
import static org.eclipse.jdt.core.JavaCore.*
1919
import static org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants.*
2020

21+
import groovy.transform.CompileStatic
22+
2123
import org.codehaus.groovy.eclipse.test.ui.GroovyEditorTestSuite
2224
import org.junit.Test
2325

26+
@CompileStatic
2427
final class ConvertToPropertyActionTests extends GroovyEditorTestSuite {
2528

2629
private static final String ACTION_ID = 'org.codehaus.groovy.eclipse.ui.convertToProperty'
@@ -31,28 +34,49 @@ final class ConvertToPropertyActionTests extends GroovyEditorTestSuite {
3134
}
3235

3336
@Test
34-
void testGetterToProperty() {
37+
void testGetterToProperty1() {
3538
convertToProperty "new Date().get${CARET}Hours();"
36-
assertEditorContents "new Date().hours;"
39+
assertEditorContents 'new Date().hours;'
3740
}
3841

3942
@Test
4043
void testGetterToProperty2() {
41-
addGroovySource 'class Foo { def getURL() { null } }', 'Foo'
44+
addGroovySource 'class Foo { def getURL() {} }', 'Foo'
4245
convertToProperty "new Foo().get${CARET}URL()"
43-
assertEditorContents "new Foo().URL"
46+
assertEditorContents 'new Foo().URL'
4447
}
4548

4649
@Test
47-
void testIsserToProperty() {
50+
void testGetterToProperty3() {
51+
addGroovySource 'class Foo { def getURLEncoder() {} }', 'Foo'
52+
convertToProperty "new Foo().get${CARET}URLEncoder()"
53+
assertEditorContents 'new Foo().URLEncoder'
54+
}
55+
56+
@Test
57+
void testIsserToProperty1() {
4858
convertToProperty "[].is${CARET}Empty();"
49-
assertEditorContents "[].empty;"
59+
assertEditorContents '[].empty;'
5060
}
5161

5262
@Test
53-
void testSetterToProperty() {
63+
void testSetterToProperty1() {
5464
convertToProperty "new Date().set${CARET}Time(1234L);"
55-
assertEditorContents "new Date().time = 1234L;"
65+
assertEditorContents 'new Date().time = 1234L;'
66+
}
67+
68+
@Test
69+
void testSetterToProperty2() {
70+
addGroovySource 'class Foo { void setURL(url) {} }', 'Foo'
71+
convertToProperty "new Foo().set${CARET}URL(null)"
72+
assertEditorContents 'new Foo().URL = null'
73+
}
74+
75+
@Test
76+
void testSetterToProperty3() {
77+
addGroovySource 'class Foo { void setURLEncoder(encoder) {} }', 'Foo'
78+
convertToProperty "new Foo().set${CARET}URLEncoder(null)"
79+
assertEditorContents 'new Foo().URLEncoder = null'
5680
}
5781

5882
@Test
@@ -120,7 +144,7 @@ final class ConvertToPropertyActionTests extends GroovyEditorTestSuite {
120144
@Test
121145
void testStaticSetterToProperty() {
122146
convertToProperty "URL.setURL${CARET}StreamHandlerFactory(null)"
123-
assertEditorContents "URL.uRLStreamHandlerFactory = null"
147+
assertEditorContents "URL.URLStreamHandlerFactory = null"
124148
}
125149

126150
@Test

ide/org.codehaus.groovy.eclipse.refactoring/src/org/codehaus/groovy/eclipse/refactoring/core/utils/StringUtils.java

-72
This file was deleted.

ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/refactoring/actions/ConvertToPropertyAction.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 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.
@@ -15,6 +15,7 @@
1515
*/
1616
package org.codehaus.groovy.eclipse.refactoring.actions;
1717

18+
import static java.beans.Introspector.decapitalize;
1819
import static java.util.regex.Pattern.compile;
1920

2021
import static org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR;
@@ -34,7 +35,6 @@
3435
import org.codehaus.groovy.eclipse.codebrowsing.requestor.Region;
3536
import org.codehaus.groovy.eclipse.codebrowsing.selection.FindSurroundingNode;
3637
import org.codehaus.groovy.eclipse.editor.GroovyEditor;
37-
import org.codehaus.groovy.eclipse.refactoring.core.utils.StringUtils;
3838
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
3939
import org.codehaus.jdt.groovy.model.ModuleNodeMapper.ModuleNodeInfo;
4040
import org.eclipse.jdt.core.JavaCore;
@@ -96,7 +96,7 @@ public static TextEdit createEdit(GroovyCompilationUnit gcu, int pos, int len) {
9696
String propertyName = match.group(1);
9797

9898
// replace "getPropertyName()" with "propertyName"
99-
return new ReplaceEdit(offset, length, StringUtils.uncapitalize(propertyName));
99+
return new ReplaceEdit(offset, length, decapitalize(propertyName));
100100

101101
} else if (args.getExpressions().size() == 1 && (match = compile("set(\\p{javaJavaIdentifierPart}+)").matcher(call.getMethodAsString())).matches()) {
102102
int offset = node.getStart(),
@@ -107,7 +107,7 @@ public static TextEdit createEdit(GroovyCompilationUnit gcu, int pos, int len) {
107107
// with "propertyName = value_expression" (check prefs for spaces around assignment)
108108
MultiTextEdit edits = new MultiTextEdit();
109109
Map<String, String> options = gcu.getJavaProject().getOptions(true);
110-
StringBuilder replacement = new StringBuilder(StringUtils.uncapitalize(propertyName));
110+
StringBuilder replacement = new StringBuilder(decapitalize(propertyName));
111111
if (JavaCore.INSERT.equals(options.get(FORMATTER_INSERT_SPACE_BEFORE_ASSIGNMENT_OPERATOR)))
112112
replacement.append(' ');
113113
replacement.append('=');

0 commit comments

Comments
 (0)