mirror of
https://github.com/querydsl/querydsl.git
synced 2026-06-16 21:01:10 +08:00
#731368 : fixed bean property name capitalization
This commit is contained in:
parent
1e3b291186
commit
2649be53dd
@ -33,6 +33,7 @@ import com.mysema.query.codegen.EntityType;
|
||||
import com.mysema.query.codegen.Property;
|
||||
import com.mysema.query.codegen.QueryTypeFactory;
|
||||
import com.mysema.query.codegen.TypeMappings;
|
||||
import com.mysema.util.BeanUtils;
|
||||
|
||||
/**
|
||||
* ElementHandler is a an APT visitor for entity types
|
||||
@ -174,9 +175,9 @@ public final class ElementHandler{
|
||||
if (config.visitMethodProperties()){
|
||||
String name = method.getSimpleName().toString();
|
||||
if (name.startsWith("get") && method.getParameters().isEmpty()){
|
||||
name = StringUtils.uncapitalize(name.substring(3));
|
||||
name = BeanUtils.uncapitalize(name.substring(3));
|
||||
}else if (name.startsWith("is") && method.getParameters().isEmpty()){
|
||||
name = StringUtils.uncapitalize(name.substring(2));
|
||||
name = BeanUtils.uncapitalize(name.substring(2));
|
||||
}else{
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -8,10 +8,18 @@ package com.mysema.query.collections;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.mysema.query.support.SerializerBase;
|
||||
import com.mysema.query.types.*;
|
||||
import com.mysema.query.types.Constant;
|
||||
import com.mysema.query.types.ConstantImpl;
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.FactoryExpression;
|
||||
import com.mysema.query.types.Operator;
|
||||
import com.mysema.query.types.Ops;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.PathType;
|
||||
import com.mysema.query.types.SubQueryExpression;
|
||||
import com.mysema.query.types.Template;
|
||||
import com.mysema.util.BeanUtils;
|
||||
|
||||
/**
|
||||
* ColQuerySerializer is a Serializer implementation for the Java language
|
||||
@ -36,7 +44,7 @@ public final class ColQuerySerializer extends SerializerBase<ColQuerySerializer>
|
||||
}
|
||||
handle((Expression<?>) path.getMetadata().getParent());
|
||||
append(".").append(prefix);
|
||||
append(StringUtils.capitalize(path.getMetadata().getExpression().toString()) + "()");
|
||||
append(BeanUtils.capitalize(path.getMetadata().getExpression().toString()) + "()");
|
||||
|
||||
}else{
|
||||
List<Expression<?>> args = new ArrayList<Expression<?>>(2);
|
||||
|
||||
@ -22,13 +22,12 @@ import javax.annotation.Nullable;
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import net.sf.cglib.proxy.MethodProxy;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
import com.mysema.query.types.Expression;
|
||||
import com.mysema.query.types.ParametrizedExpression;
|
||||
import com.mysema.query.types.Path;
|
||||
import com.mysema.query.types.PathMetadata;
|
||||
import com.mysema.query.types.PathMetadataFactory;
|
||||
import com.mysema.util.BeanUtils;
|
||||
import com.mysema.util.ReflectionUtils;
|
||||
|
||||
/**
|
||||
@ -248,7 +247,7 @@ class PropertyAccessInvocationHandler implements MethodInterceptor {
|
||||
private String propertyNameForGetter(Method method) {
|
||||
String name = method.getName();
|
||||
name = name.startsWith("is") ? name.substring(2) : name.substring(3);
|
||||
return StringUtils.uncapitalize(name);
|
||||
return BeanUtils.uncapitalize(name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ import java.util.Set;
|
||||
import com.mysema.codegen.CodeWriter;
|
||||
import com.mysema.codegen.model.Parameter;
|
||||
import com.mysema.codegen.model.Types;
|
||||
import com.mysema.util.BeanUtils;
|
||||
|
||||
/**
|
||||
* BeanSerializer is a Serializer implementation which serializes EntityType instances into JavaBean classes
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
package com.mysema.query.codegen;
|
||||
package com.mysema.util;
|
||||
|
||||
import java.beans.Introspector;
|
||||
|
||||
@ -10,11 +10,11 @@ import org.apache.commons.lang.StringUtils;
|
||||
*/
|
||||
public final class BeanUtils {
|
||||
|
||||
public static String capitalize(String property){
|
||||
if (property.length() > 1 && Character.isUpperCase(property.charAt(1))){
|
||||
return property;
|
||||
public static String capitalize(String name){
|
||||
if (name.length() > 1 && Character.isUpperCase(name.charAt(1))){
|
||||
return name;
|
||||
}else{
|
||||
return StringUtils.capitalize(property);
|
||||
return StringUtils.capitalize(name);
|
||||
}
|
||||
}
|
||||
|
||||
@ -19,7 +19,6 @@ import java.util.Set;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
@ -60,7 +59,7 @@ public final class ReflectionUtils {
|
||||
|
||||
@Nullable
|
||||
private static Method getGetterOrNull(Class<?> beanClass, String name, Class<?> type){
|
||||
String methodName = (type.equals(Boolean.class) ? "is" : "get") + StringUtils.capitalize(name);
|
||||
String methodName = (type.equals(Boolean.class) ? "is" : "get") + BeanUtils.capitalize(name);
|
||||
while(beanClass != null && !beanClass.equals(Object.class)){
|
||||
try {
|
||||
return beanClass.getDeclaredMethod(methodName);
|
||||
|
||||
@ -64,6 +64,17 @@ public class BeanSerializerTest {
|
||||
assertTrue(str.contains("@QueryEntity"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Capitalization() throws IOException{
|
||||
// property
|
||||
type.addProperty(new Property(type, "cId", type));
|
||||
|
||||
BeanSerializer serializer = new BeanSerializer();
|
||||
serializer.serialize(type, SimpleSerializerConfig.DEFAULT, new JavaWriter(writer));
|
||||
assertTrue(writer.toString().contains("public DomainClass getcId() {"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Properties() throws IOException{
|
||||
// property
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
package com.mysema.query.codegen;
|
||||
package com.mysema.util;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mysema.util.BeanUtils;
|
||||
|
||||
public class BeanUtilsTest {
|
||||
|
||||
@Test
|
||||
@ -17,7 +17,6 @@ import java.util.Set;
|
||||
|
||||
import javax.xml.stream.XMLStreamException;
|
||||
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.hibernate.cfg.Configuration;
|
||||
import org.hibernate.mapping.Component;
|
||||
import org.hibernate.mapping.MappedSuperclass;
|
||||
@ -46,6 +45,7 @@ import com.mysema.query.codegen.Supertype;
|
||||
import com.mysema.query.codegen.SupertypeSerializer;
|
||||
import com.mysema.query.codegen.TypeFactory;
|
||||
import com.mysema.query.codegen.TypeMappings;
|
||||
import com.mysema.util.BeanUtils;
|
||||
|
||||
/**
|
||||
* @author tiwe
|
||||
@ -257,8 +257,8 @@ public class HibernateDomainExporter {
|
||||
Field field = cl.getDeclaredField(propertyName);
|
||||
return typeFactory.create(field.getType(), field.getGenericType());
|
||||
} catch (NoSuchFieldException e) {
|
||||
String getter = "get"+StringUtils.capitalize(propertyName);
|
||||
String bgetter = "is"+StringUtils.capitalize(propertyName);
|
||||
String getter = "get"+BeanUtils.capitalize(propertyName);
|
||||
String bgetter = "is"+BeanUtils.capitalize(propertyName);
|
||||
for (Method method : cl.getDeclaredMethods()){
|
||||
if ((method.getName().equals(getter) || method.getName().equals(bgetter)) && method.getParameterTypes().length == 0){
|
||||
return typeFactory.create(method.getReturnType(), method.getGenericReturnType());
|
||||
@ -279,8 +279,8 @@ public class HibernateDomainExporter {
|
||||
Field field = cl.getDeclaredField(propertyName);
|
||||
return getAnnotations(field.getAnnotations());
|
||||
} catch (NoSuchFieldException e) {
|
||||
String getter = "get"+StringUtils.capitalize(propertyName);
|
||||
String bgetter = "is"+StringUtils.capitalize(propertyName);
|
||||
String getter = "get"+BeanUtils.capitalize(propertyName);
|
||||
String bgetter = "is"+BeanUtils.capitalize(propertyName);
|
||||
for (Method method : cl.getDeclaredMethods()){
|
||||
if ((method.getName().equals(getter) || method.getName().equals(bgetter)) && method.getParameterTypes().length == 0){
|
||||
return getAnnotations(method.getAnnotations());
|
||||
|
||||
Loading…
Reference in New Issue
Block a user