Skip to content

Commit

Permalink
Fix for #738: Introspector.decapitalize implements bean name rules
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Oct 1, 2018
1 parent 68b5ef4 commit 143b396
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,9 +18,12 @@ package org.codehaus.groovy.eclipse.test.actions
import static org.eclipse.jdt.core.JavaCore.*
import static org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants.*

import groovy.transform.CompileStatic

import org.codehaus.groovy.eclipse.test.ui.GroovyEditorTestSuite
import org.junit.Test

@CompileStatic
final class ConvertToPropertyActionTests extends GroovyEditorTestSuite {

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

@Test
void testGetterToProperty() {
void testGetterToProperty1() {
convertToProperty "new Date().get${CARET}Hours();"
assertEditorContents "new Date().hours;"
assertEditorContents 'new Date().hours;'
}

@Test
void testGetterToProperty2() {
addGroovySource 'class Foo { def getURL() { null } }', 'Foo'
addGroovySource 'class Foo { def getURL() {} }', 'Foo'
convertToProperty "new Foo().get${CARET}URL()"
assertEditorContents "new Foo().URL"
assertEditorContents 'new Foo().URL'
}

@Test
void testIsserToProperty() {
void testGetterToProperty3() {
addGroovySource 'class Foo { def getURLEncoder() {} }', 'Foo'
convertToProperty "new Foo().get${CARET}URLEncoder()"
assertEditorContents 'new Foo().URLEncoder'
}

@Test
void testIsserToProperty1() {
convertToProperty "[].is${CARET}Empty();"
assertEditorContents "[].empty;"
assertEditorContents '[].empty;'
}

@Test
void testSetterToProperty() {
void testSetterToProperty1() {
convertToProperty "new Date().set${CARET}Time(1234L);"
assertEditorContents "new Date().time = 1234L;"
assertEditorContents 'new Date().time = 1234L;'
}

@Test
void testSetterToProperty2() {
addGroovySource 'class Foo { void setURL(url) {} }', 'Foo'
convertToProperty "new Foo().set${CARET}URL(null)"
assertEditorContents 'new Foo().URL = null'
}

@Test
void testSetterToProperty3() {
addGroovySource 'class Foo { void setURLEncoder(encoder) {} }', 'Foo'
convertToProperty "new Foo().set${CARET}URLEncoder(null)"
assertEditorContents 'new Foo().URLEncoder = null'
}

@Test
Expand Down Expand Up @@ -120,7 +144,7 @@ final class ConvertToPropertyActionTests extends GroovyEditorTestSuite {
@Test
void testStaticSetterToProperty() {
convertToProperty "URL.setURL${CARET}StreamHandlerFactory(null)"
assertEditorContents "URL.uRLStreamHandlerFactory = null"
assertEditorContents "URL.URLStreamHandlerFactory = null"
}

@Test
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*/
package org.codehaus.groovy.eclipse.refactoring.actions;

import static java.beans.Introspector.decapitalize;
import static java.util.regex.Pattern.compile;

import static org.eclipse.jdt.core.formatter.DefaultCodeFormatterConstants.FORMATTER_INSERT_SPACE_AFTER_ASSIGNMENT_OPERATOR;
Expand All @@ -34,7 +35,6 @@
import org.codehaus.groovy.eclipse.codebrowsing.requestor.Region;
import org.codehaus.groovy.eclipse.codebrowsing.selection.FindSurroundingNode;
import org.codehaus.groovy.eclipse.editor.GroovyEditor;
import org.codehaus.groovy.eclipse.refactoring.core.utils.StringUtils;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.codehaus.jdt.groovy.model.ModuleNodeMapper.ModuleNodeInfo;
import org.eclipse.jdt.core.JavaCore;
Expand Down Expand Up @@ -96,7 +96,7 @@ public static TextEdit createEdit(GroovyCompilationUnit gcu, int pos, int len) {
String propertyName = match.group(1);

// replace "getPropertyName()" with "propertyName"
return new ReplaceEdit(offset, length, StringUtils.uncapitalize(propertyName));
return new ReplaceEdit(offset, length, decapitalize(propertyName));

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

0 comments on commit 143b396

Please sign in to comment.