Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for CharSequence based fromString methods

  • Loading branch information...
commit b520fe73092c109aafbd1f35b0386acced84a2de 1 parent eb58c4a
@jodastephen jodastephen authored
View
3  src/changes/changes.xml
@@ -9,6 +9,9 @@
<!-- types are add, fix, remove, update -->
<release version="1.2" date="2011-10-27">
<action dev="scolebourne" type="add" >
+ Add support for CharSequence based fromString methods.
+ </action>
+ <action dev="scolebourne" type="add" >
Add support for JSR-310 by reflection, avoiding a dependency.
</action>
<action dev="scolebourne" type="add" >
View
7 src/main/java/org/joda/convert/MethodsStringConverter.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2010 Stephen Colebourne
+ * Copyright 2010-2011 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -48,8 +48,9 @@
if (fromString.getParameterTypes().length != 1) {
throw new IllegalStateException("FromString method must have one parameter");
}
- if (fromString.getParameterTypes()[0] != String.class) {
- throw new IllegalStateException("FromString method must take a String");
+ Class<?> param = fromString.getParameterTypes()[0];
+ if (param != String.class && param != CharSequence.class) {
+ throw new IllegalStateException("FromString method must take a String or CharSequence");
}
if (fromString.getReturnType().isAssignableFrom(cls) == false) {
throw new IllegalStateException("FromString method must return specified class or a superclass");
View
25 src/main/java/org/joda/convert/StringConvert.java
@@ -264,13 +264,18 @@ private Method findToStringMethod(Class<?> cls) {
* @return the method to call, null means use {@code toString}
*/
private <T> Constructor<T> findFromStringConstructor(Class<T> cls) {
+ Constructor<T> con;
try {
- Constructor<T> con = cls.getDeclaredConstructor(String.class);
- FromString fromString = con.getAnnotation(FromString.class);
- return fromString != null ? con : null;
+ con = cls.getDeclaredConstructor(String.class);
} catch (NoSuchMethodException ex) {
- return null;
+ try {
+ con = cls.getDeclaredConstructor(CharSequence.class);
+ } catch (NoSuchMethodException ex2) {
+ return null;
+ }
}
+ FromString fromString = con.getAnnotation(FromString.class);
+ return fromString != null ? con : null;
}
/**
@@ -438,7 +443,11 @@ private Method findFromStringMethod(Class<?> cls, String methodName) {
try {
m = cls.getMethod(methodName, String.class);
} catch (NoSuchMethodException ex) {
- throw new IllegalArgumentException(ex);
+ try {
+ m = cls.getMethod(methodName, CharSequence.class);
+ } catch (NoSuchMethodException ex2) {
+ throw new IllegalArgumentException("Method not found", ex2);
+ }
}
if (Modifier.isStatic(m.getModifiers()) == false) {
throw new IllegalArgumentException("Method must be static: " + methodName);
@@ -457,7 +466,11 @@ private Method findFromStringMethod(Class<?> cls, String methodName) {
try {
return cls.getDeclaredConstructor(String.class);
} catch (NoSuchMethodException ex) {
- return null;
+ try {
+ return cls.getDeclaredConstructor(CharSequence.class);
+ } catch (NoSuchMethodException ex2) {
+ throw new IllegalArgumentException("Constructor not found", ex2);
+ }
}
}
View
46 src/test/java/org/joda/convert/DistanceMethodConstructorCharSequence.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2010-2011 Stephen Colebourne
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.joda.convert;
+
+/**
+ * Example class with annotated constructor and method.
+ */
+public class DistanceMethodConstructorCharSequence {
+
+ /** Amount. */
+ final int amount;
+
+ public DistanceMethodConstructorCharSequence(int amount) {
+ this.amount = amount;
+ }
+
+ @FromString
+ public DistanceMethodConstructorCharSequence(CharSequence amount) {
+ String amt = amount.toString().substring(0, amount.length() - 1);
+ this.amount = Integer.parseInt(amt);
+ }
+
+ @ToString
+ public String print() {
+ return amount + "m";
+ }
+
+ @Override
+ public String toString() {
+ return "Distance[" + amount + "m]";
+ }
+
+}
View
2  src/test/java/org/joda/convert/DistanceMethodMethod.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2010 Stephen Colebourne
+ * Copyright 2010-2011 Stephen Colebourne
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
View
46 src/test/java/org/joda/convert/DistanceMethodMethodCharSequence.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2010-2011 Stephen Colebourne
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.joda.convert;
+
+/**
+ * Example class with annotated methods.
+ */
+public class DistanceMethodMethodCharSequence {
+
+ /** Amount. */
+ final int amount;
+
+ @FromString
+ public static DistanceMethodMethodCharSequence parse(CharSequence amount) {
+ String amt = amount.toString().substring(0, amount.length() - 1);
+ return new DistanceMethodMethodCharSequence(Integer.parseInt(amt));
+ }
+
+ public DistanceMethodMethodCharSequence(int amount) {
+ this.amount = amount;
+ }
+
+ @ToString
+ public String print() {
+ return amount + "m";
+ }
+
+ @Override
+ public String toString() {
+ return "Distance[" + amount + "m]";
+ }
+
+}
View
48 src/test/java/org/joda/convert/DistanceNoAnnotationsCharSequence.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2010-2011 Stephen Colebourne
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.joda.convert;
+
+/**
+ * Example class with no annotations.
+ */
+public class DistanceNoAnnotationsCharSequence {
+
+ /** Amount. */
+ final int amount;
+
+ public static DistanceNoAnnotationsCharSequence parse(CharSequence amount) {
+ return new DistanceNoAnnotationsCharSequence(amount);
+ }
+
+ public DistanceNoAnnotationsCharSequence(int amount) {
+ this.amount = amount;
+ }
+
+ public DistanceNoAnnotationsCharSequence(CharSequence amount) {
+ String amt = amount.toString().substring(0, amount.length() - 1);
+ this.amount = Integer.parseInt(amt);
+ }
+
+ public String print() {
+ return amount + "m";
+ }
+
+ @Override
+ public String toString() {
+ return "Distance[" + amount + "m]";
+ }
+
+}
View
49 src/test/java/org/joda/convert/TestStringConvert.java
@@ -168,6 +168,18 @@ public void test_convert_annotationMethodMethod() {
}
@Test
+ public void test_convert_annotationMethodMethodCharSequence() {
+ StringConvert test = new StringConvert();
+ DistanceMethodMethodCharSequence d = new DistanceMethodMethodCharSequence(25);
+ assertEquals("25m", test.convertToString(d));
+ assertEquals(d.amount, test.convertFromString(DistanceMethodMethodCharSequence.class, "25m").amount);
+ StringConverter<DistanceMethodMethodCharSequence> conv = test.findConverter(DistanceMethodMethodCharSequence.class);
+ assertEquals(true, conv instanceof MethodsStringConverter<?>);
+ assertSame(conv, test.findConverter(DistanceMethodMethodCharSequence.class));
+ assertEquals(true, conv.toString().startsWith("RefectionStringConverter"));
+ }
+
+ @Test
public void test_convert_annotationMethodConstructor() {
StringConvert test = new StringConvert();
DistanceMethodConstructor d = new DistanceMethodConstructor(25);
@@ -180,6 +192,18 @@ public void test_convert_annotationMethodConstructor() {
}
@Test
+ public void test_convert_annotationMethodConstructorCharSequence() {
+ StringConvert test = new StringConvert();
+ DistanceMethodConstructorCharSequence d = new DistanceMethodConstructorCharSequence(25);
+ assertEquals("25m", test.convertToString(d));
+ assertEquals(d.amount, test.convertFromString(DistanceMethodConstructorCharSequence.class, "25m").amount);
+ StringConverter<DistanceMethodConstructorCharSequence> conv = test.findConverter(DistanceMethodConstructorCharSequence.class);
+ assertEquals(true, conv instanceof MethodConstructorStringConverter<?>);
+ assertSame(conv, test.findConverter(DistanceMethodConstructorCharSequence.class));
+ assertEquals(true, conv.toString().startsWith("RefectionStringConverter"));
+ }
+
+ @Test
public void test_convert_annotationSubMethodMethod() {
StringConvert test = new StringConvert();
SubMethodMethod d = new SubMethodMethod(25);
@@ -383,6 +407,18 @@ public void test_registerMethods() {
assertSame(conv, test.findConverter(DistanceNoAnnotations.class));
}
+ @Test
+ public void test_registerMethodsCharSequence() {
+ StringConvert test = new StringConvert();
+ test.registerMethods(DistanceNoAnnotationsCharSequence.class, "toString", "parse");
+ DistanceNoAnnotationsCharSequence d = new DistanceNoAnnotationsCharSequence(25);
+ assertEquals("Distance[25m]", test.convertToString(d));
+ assertEquals(d.amount, test.convertFromString(DistanceNoAnnotationsCharSequence.class, "25m").amount);
+ StringConverter<DistanceNoAnnotationsCharSequence> conv = test.findConverter(DistanceNoAnnotationsCharSequence.class);
+ assertEquals(true, conv instanceof MethodsStringConverter<?>);
+ assertSame(conv, test.findConverter(DistanceNoAnnotationsCharSequence.class));
+ }
+
@Test(expected=IllegalArgumentException.class)
public void test_registerMethods_nullClass() {
StringConvert test = new StringConvert();
@@ -408,6 +444,19 @@ public void test_registerMethods_classAlreadyRegistered() {
test.registerMethods(DistanceNoAnnotations.class, "toString", "parse");
}
+ //-------------------------------------------------------------------------
+ @Test
+ public void test_registerMethodConstructorCharSequence() {
+ StringConvert test = new StringConvert();
+ test.registerMethodConstructor(DistanceNoAnnotationsCharSequence.class, "toString");
+ DistanceNoAnnotationsCharSequence d = new DistanceNoAnnotationsCharSequence(25);
+ assertEquals("Distance[25m]", test.convertToString(d));
+ assertEquals(d.amount, test.convertFromString(DistanceNoAnnotationsCharSequence.class, "25m").amount);
+ StringConverter<DistanceNoAnnotationsCharSequence> conv = test.findConverter(DistanceNoAnnotationsCharSequence.class);
+ assertEquals(true, conv instanceof MethodConstructorStringConverter<?>);
+ assertSame(conv, test.findConverter(DistanceNoAnnotationsCharSequence.class));
+ }
+
@Test
public void test_registerMethodConstructor() {
StringConvert test = new StringConvert();
Please sign in to comment.
Something went wrong with that request. Please try again.