jodaorg / joda-convert Goto Github PK
View Code? Open in Web Editor NEWJava library to enable conversion to and from standard string formats.
Home Page: http://www.joda.org/joda-convert/
License: Apache License 2.0
Java library to enable conversion to and from standard string formats.
Home Page: http://www.joda.org/joda-convert/
License: Apache License 2.0
When sending a converted type in a serialized form, it can be useful to send the effective type of the data. This matters if you have a superclass defining the conversion and multiple private subclasses. It is not desirable to send the subclass type in the serialized form - only the effective type (the superclass) should be exposed.
Hi,
Since 1.8, at runtime, this is what I get if I don't have the guava jar in my classpath:
java.lang.ClassNotFoundException: com.google.common.reflect.Types
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.joda.convert.RenameHandler.loadType(RenameHandler.java:134)
at org.joda.convert.AbstractTypeStringConverter.<clinit>(AbstractTypeStringConverter.java:61)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at org.joda.convert.StringConvert.tryRegisterGuava(StringConvert.java:226)
at org.joda.convert.StringConvert.<init>(StringConvert.java:158)
at org.joda.convert.StringConvert.<init>(StringConvert.java:120)
at org.joda.convert.StringConvert.<clinit>(StringConvert.java:49)
Looks like org.joda.convert.StringConvert.tryRegisterGuava()
will get a RuntimeException everytime because of this code in org.joda.convert.AbstractTypeStringConverter
:
try {
Class<?> typesClass = RenameHandler.INSTANCE.loadType("com.google.common.reflect.Types");
Method newParam = typesClass.getDeclaredMethod("newParameterizedType", Class.class, Type[].class);
newParam.setAccessible(true);
NEW_PARAM_TYPE = newParam;
Method extendsType = typesClass.getDeclaredMethod("subtypeOf", Type.class);
extendsType.setAccessible(true);
EXTENDS_TYPE = extendsType;
Method superType = typesClass.getDeclaredMethod("supertypeOf", Type.class);
superType.setAccessible(true);
SUPER_TYPE = superType;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
It used to work in 1.7.
Just like Joda said:
Do or do not. There is no try.
you should either rename this method to doRegisterGuava
and modify the documentation to reflect this dependency, or really make it optional.
The dynamic code for OptionalInt
and friends extends Guava specific code, which it shouldn't.
Not strictly necessary, but Android doesn't include javax.xml.bind so if this were removed it would be easier to include this lib in Android without it complaining.
Add module-info.java
and update build to match.
Add Automatic-Module-Name
to MANIFEST.MF
. Will be org.joda.convert
.
For interfaces, or abstract classes, it is useful to be able to declare a separate factory class that contains the FromString
method.
class BarFoo implements Foo {...}
@FromStringFactory(FooFactory.class)
interface Foo {...}
class FooFactory {
@FromString
Foo createFoo(String) {...}
}
Joda-Convert previously use setAccessible()
to access package scoped methods in Guava due to Guava #1645 not being fixed. With Java 9, this is no longer tenable, thus fix #20 needs to go further - actually copying code from Guava to Joda-Convert.
As a side effect of this, the converter for Type
now works regardless of whether Guava is present or not, and it also handles subclass implementations correctly (a bug fix).
The primitive OptionalDouble
, OptionalInt
and OptionalLong
should be supported.
This is not a bug, but a general design question.
Why does the convertFromString
take a Class<? extends T>
parameter?
It's used in two places:
EnumStringConverter
-- When would the specified cls
differ from the converter's effectiveType
?MethodsStringConverter
-- When would a caller want to specify a different cls
param?Thanks, I couldn't find an explanation in the docs or in the source.
Hi There,
This project is still using an old implementation of LGTM's automated code review, which has now been disabled. To continue using automated code review, and receive checks on your Pull Requests, please install the GitHub App on this repository.
Thanks,
The LGTM Team
Adding a register method that takes the ToStringConverter
and FromStringConverter
separately allows nice code like this in JDK 1.8
sc.register(Distance.class, Distance::toString, Distance::parse)
Submitted by "nobody" to old Sourceforge issue system:
https://sourceforge.net/p/joda-convert/bugs/1/
I'd like to take advantage of most of the built-in JDK converters, but override one or two with custom logic.
That's not possible because calling register will throw an IllegalStateException "Converter already registered".
For example, if a user wants to use all built-in converters but tweak convertFromString for all enums to allow case-insensitive matches, that's not currently possible with this API.
# gradle clean assemble -S --scan
Exception in thread "main" java.lang.IllegalArgumentException: Type without superclass: module-info
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:191)
at com.google.devtools.build.android.desugar.DefaultMethodClassFixer.visit(DefaultMethodClassFixer.java:80)
at org.objectweb.asm.ClassVisitor.visit(ClassVisitor.java:113)
at com.google.devtools.build.android.desugar.InterfaceDesugaring.visit(InterfaceDesugaring.java:90)
at org.objectweb.asm.ClassReader.accept(ClassReader.java:621)
at org.objectweb.asm.ClassReader.accept(ClassReader.java:500)
at com.google.devtools.build.android.desugar.Desugar.desugarClassesInInput(Desugar.java:475)
at com.google.devtools.build.android.desugar.Desugar.desugarOneInput(Desugar.java:362)
at com.google.devtools.build.android.desugar.Desugar.desugar(Desugar.java:315)
at com.google.devtools.build.android.desugar.Desugar.main(Desugar.java:681)
Diagnosed with task :lint
> Task :lint
Error processing /home/user/.gradle/caches/modules-2/files-2.1/org.joda/joda-convert/2.0/a18cac1a17a926b60485d9f5bf0dfff000e2d079/joda-convert-2.0.jar:module-info.class: broken class file?
Error processing /home/user/.gradle/caches/modules-2/files-2.1/org.joda/joda-convert/2.0/a18cac1a17a926b60485d9f5bf0dfff000e2d079/joda-convert-2.0.jar:module-info.class: broken class file?
Error processing /home/user/.gradle/caches/modules-2/files-2.1/org.joda/joda-convert/2.0/a18cac1a17a926b60485d9f5bf0dfff000e2d079/joda-convert-2.0.jar:module-info.class: broken class file?
Error processing /home/user/.gradle/caches/modules-2/files-2.1/org.joda/joda-convert/2.0/a18cac1a17a926b60485d9f5bf0dfff000e2d079/joda-convert-2.0.jar:module-info.class: broken class file?
Error processing /home/user/.gradle/caches/modules-2/files-2.1/org.joda/joda-convert/2.0/a18cac1a17a926b60485d9f5bf0dfff000e2d079/joda-convert-2.0.jar:module-info.class: broken class file?
Error processing /home/user/.gradle/caches/modules-2/files-2.1/org.joda/joda-convert/2.0/a18cac1a17a926b60485d9f5bf0dfff000e2d079/joda-convert-2.0.jar:module-info.class: broken class file?
Removing the files does not fix the issue.
Using Gradle identification:
------------------------------------------------------------
Gradle 4.6
------------------------------------------------------------
Build time: 2018-02-28 13:36:36 UTC
Revision: 8fa6ce7945b640e6168488e4417f9bb96e4ab46c
Groovy: 2.4.12
Ant: Apache Ant(TM) version 1.9.9 compiled on February 2 2017
JVM: 1.8.0_151 (Oracle Corporation 25.151-b12)
OS: Linux 4.15.0-1-amd64 amd64
Update: I filed issue with Google Android Gradle support, see here: https://issuetracker.google.com/issues/74428304
Generic interface
public interface HasCode<C> {
C getCode();
}
And a String implementation
public final class AirportCode implements HasCode<String> {
private final String code;
@Override
@ToString
public String getCode() {
return code;
}
It fails with 'Two methods are annotated with @tostring: net.worldticket.commons.flight.AirportCode.'
It finds the getCode()
method twice one with a String return type and the other with an Object as return type.
It should be possible to configure RenameHandler
using configuration files rather than by code. The information on renames is static, and configuration is a more appropriate approach.
If a Class
or Enum
gets stored and read back later there is the chance that it has been renamed. Handle this simple case of refactoring.
RenameHandler.INSTANCE.renamedType("org.joda.OldName", NewName.class);
RenameHandler.INSTANCE.renamedEnum("CORRECT", Status.VALID);
RenameHandler.INSTANCE.renamedEnum("INCORRECT", Status.INVALID);
Guava TypeToken
is a useful value type for expressing in depth generics. Writing a parser for the string format (missing from Guava) allows TypeToken
and Type
to be serialized.
Its time to make the project require Java 8.
Observed symptom
Iit is possible for the following code:
converter.convertFromString(Foo.class, "foo-repr")
to result in an object that is not an instance of Foo
.
Cause
When StringConvert
searches for a converter, it first searches for a registered converter for Foo
. If it does not find one, it then begins searching for converters for Foo
's superclasses.
This is correct when doing a conversion from a Foo
. However, when converting to a Foo
, it is only correct to use a converter for a subclass of Foo
. A supertype converter may not convert to Foo
.
Context
Found when attempting to use joda-convert to convert to a custom implementation of CharSequence
. The default-registered CharSequence
converter just casts a string, which is not an instance of my custom CharSequence
. Since StringConvert.INSTANCE
is also immutable, I cannot register a custom converter in a static initializer block to enable seamless conversions.
Unit test failure using the Android/J2ObjC implementation of JRE:
2) test_InetAddress(org.joda.convert.TestJDKStringConverters)
01:31:12.470 [ERROR] [org.gradle.BuildExceptionReporter] org.junit.ComparisonFailure: expected:
<2001:db8:85a3:[0:0]:8a2e:370:7334> but was:<2001:db8:85a3:[]:8a2e:370:7334>
Per the official Java JDK docs, the result 2001:db8:85a3::8a2e:370:7334
is also valid, and this test should accept both answers as correct.
cc @tomball @dlew - you mentioned in #7 that some tests were failing with the J2objc JRE emul; we're working through them here: j2objc-contrib/j2objc-common-libs-e2e-test#46; this particular issue seems to be an issue with the test; the others seem like genuine differences between a conforming JRE and the J2ObjC JRE.
cc @brunobowden
There is no need for a dependency on Guava. It can all be done by reflection, which makes the dependency tree much simpler to manage.
The published POM contains many ${joda.artifactId} snippets.
http://central.maven.org/maven2/org/joda/joda-convert/2.0.1/joda-convert-2.0.1.pom
...
<artifactId>joda-convert</artifactId>
<packaging>jar</packaging>
<name>Joda-Convert</name>
<version>2.0.1</version>
<description>Library to convert Objects to and from String</description>
<url>http://www.joda.org/${joda.artifactId}/</url>
...
I have a bean field of type Class<?>
. When the field describes an Integer
it works fine, when it represents an int
it fails:
Caused by: java.lang.RuntimeException: Unable to create type: int
at org.joda.convert.JDKStringConverter$21.convertFromString(JDKStringConverter.java:274)
at org.joda.convert.StringConvert.convertFromString(StringConvert.java:309)
at org.joda.beans.ser.json.AbstractJsonReader.parseSimple(AbstractJsonReader.java:350)
at org.joda.beans.ser.json.AbstractJsonReader.parseObject(AbstractJsonReader.java:170)
at org.joda.beans.ser.json.AbstractJsonReader.parseBean(AbstractJsonReader.java:104)
... 37 more
If we have field with type Class, lets say:
private Class type;
Then if we set type
value as byte[].class
, then such value will be serialized as "[B".
When we try to deserialize such value, we get exception
Caused by: java.lang.ClassNotFoundException: [B
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
at org.joda.convert.StringConvert.loadType(StringConvert.java:857)
at org.joda.convert.RenameHandler.lookupType(RenameHandler.java:197)
at org.joda.convert.JDKStringConverter$21.convertFromString(JDKStringConverter.java:272)
Solution could be using Class.forName(fullName)
if exception occurs.
Am I the only one who can't get that test to succeed?
java version "1.7.0_75"
OpenJDK Runtime Environment (IcedTea 2.5.4) (Gentoo package icedtea-7.2.5.4)
OpenJDK 64-Bit Server VM (build 24.75-b04, mixed mode)
[junit] Running org.joda.convert.test1.Test1Class
[junit] Testsuite: org.joda.convert.test1.Test1Class
[junit] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0,147 sec
[junit] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0,147 sec
[junit]
[junit] Testcase: initializationError took 0,008 sec
[junit] Caused an ERROR
[junit] Test class should have exactly one public zero-argument constructor
[junit] java.lang.Exception: Test class should have exactly one public zero-argument constructor
[junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
[junit]
[junit] Testcase: initializationError took 0,001 sec
[junit] Caused an ERROR
[junit] No runnable methods
[junit] java.lang.Exception: No runnable methods
[junit] at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.