Updated version 1.8.3 from 'upstream/1.8.3'
with Debian dir 352d1389b3b3897981a3fbce75e026a316124ce0
Emmanuel Bourg
6 years ago
51 | 51 | |
52 | 52 | |
53 | 53 | ### Releases |
54 | [Release 1.8.2](http://www.joda.org/joda-convert/download.html) is the current latest release. | |
54 | [Release 1.8.3](http://www.joda.org/joda-convert/download.html) is the current latest release. | |
55 | 55 | This release is considered stable and worthy of the 1.x tag. |
56 | 56 | It depends on Java SE 6 or later. |
57 | 57 | |
58 | Available in the [Maven Central repository](http://search.maven.org/#artifactdetails|org.joda|joda-convert|1.8.2|jar) | |
58 | Available in the [Maven Central repository](http://search.maven.org/#artifactdetails|org.joda|joda-convert|1.8.3|jar) | |
59 | 59 | |
60 | 60 | |
61 | 61 | ### Support |
8 | 8 | <artifactId>joda-convert</artifactId> |
9 | 9 | <packaging>jar</packaging> |
10 | 10 | <name>Joda-Convert</name> |
11 | <version>1.8.2</version> | |
11 | <version>1.8.3</version> | |
12 | 12 | <description>Library to convert Objects to and from String</description> |
13 | 13 | <url>http://www.joda.org/joda-convert/</url> |
14 | 14 | |
386 | 386 | <dependency> |
387 | 387 | <groupId>com.google.guava</groupId> |
388 | 388 | <artifactId>guava</artifactId> |
389 | <version>18.0</version> | |
389 | <version>20.0</version> | |
390 | 390 | <optional>true</optional> |
391 | 391 | </dependency> |
392 | 392 | <dependency> |
6 | 6 | |
7 | 7 | <body> |
8 | 8 | <!-- types are add, fix, remove, update --> |
9 | <release version="1.8.3" date="2017-08-21" description="Version 1.8.3"> | |
10 | <action dev="jodastephen" type="update" > | |
11 | Optional Guava dependency updated to v20.0. | |
12 | As Guava is accessed only by reflection guarded by try-catch, any version of Guava may be used by end users. | |
13 | This version has been selected for the pom.xml as it is the last version for Java 1.6. | |
14 | </action> | |
15 | <action dev="jodastephen" type="update" > | |
16 | Provide ability to lock rename handler for additional security. | |
17 | Prevent renames of java.*, javax.* and org.joda.* types. | |
18 | </action> | |
19 | </release> | |
9 | 20 | <release version="1.8.2" date="2017-06-28" description="Version 1.8.2"> |
10 | 21 | <action dev="jodastephen" type="fix" > |
11 | 22 | Fix to ensure primitive types can be deserialized. |
381 | 381 | if (str.length() != 29) { |
382 | 382 | throw new IllegalArgumentException("Unable to parse date: " + str); |
383 | 383 | } |
384 | str = str.substring(0, 26) + str.substring(27); | |
384 | String str2 = str.substring(0, 26) + str.substring(27); | |
385 | 385 | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); |
386 | 386 | try { |
387 | return f.parseObject(str); | |
387 | return f.parseObject(str2); | |
388 | 388 | } catch (ParseException ex) { |
389 | 389 | throw new RuntimeException(ex); |
390 | 390 | } |
412 | 412 | throw new IllegalArgumentException("Unable to parse date: " + str); |
413 | 413 | } |
414 | 414 | TimeZone zone = TimeZone.getTimeZone(str.substring(30, str.length() - 1)); |
415 | str = str.substring(0, 26) + str.substring(27, 29); | |
415 | String str2 = str.substring(0, 26) + str.substring(27, 29); | |
416 | 416 | SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ"); |
417 | 417 | GregorianCalendar cal = new GregorianCalendar(zone); |
418 | 418 | cal.setTimeInMillis(0); |
419 | 419 | f.setCalendar(cal); |
420 | 420 | try { |
421 | f.parseObject(str); | |
421 | f.parseObject(str2); | |
422 | 422 | return f.getCalendar(); |
423 | 423 | } catch (ParseException ex) { |
424 | 424 | throw new RuntimeException(ex); |
32 | 32 | * </pre> |
33 | 33 | * The recommended usage is to edit the static singleton before using other classes. |
34 | 34 | * Editing a static is acceptable because renames are driven by bytecode which is static. |
35 | * For additional security, an application should lock the rename handler instance | |
36 | * once any types and enums have been registered using {@link #lock()}. | |
35 | 37 | * <p> |
36 | 38 | * This class is thread-safe with concurrent caches. |
37 | 39 | * |
46 | 48 | public static final RenameHandler INSTANCE = new RenameHandler(); |
47 | 49 | |
48 | 50 | /** |
51 | * The lock flag. | |
52 | */ | |
53 | private volatile boolean locked; | |
54 | /** | |
49 | 55 | * The type renames. |
50 | 56 | */ |
51 | 57 | private final ConcurrentHashMap<String, Class<?>> typeRenames = |
78 | 84 | //----------------------------------------------------------------------- |
79 | 85 | /** |
80 | 86 | * Register the fact that a type was renamed. |
87 | * <p> | |
88 | * This handles the use case where a class is renamed. | |
81 | 89 | * |
82 | 90 | * @param oldName the old name of the type including the package name, not null |
83 | 91 | * @param currentValue the current type, not null |
89 | 97 | if (currentValue == null) { |
90 | 98 | throw new IllegalArgumentException("currentValue must not be null"); |
91 | 99 | } |
100 | if (oldName.startsWith("java.") || oldName.startsWith("javax.") || oldName.startsWith("org.joda.")) { | |
101 | throw new IllegalArgumentException("oldName must not be a java.*, javax.* or org.joda.* type"); | |
102 | } | |
103 | checkNotLocked(); | |
92 | 104 | typeRenames.put(oldName, currentValue); |
93 | 105 | } |
94 | 106 | |
164 | 176 | //----------------------------------------------------------------------- |
165 | 177 | /** |
166 | 178 | * Register the fact that an enum constant was renamed. |
179 | * <p> | |
180 | * This handles the use case where an enum constant is renamed, but the enum class remains the same. | |
167 | 181 | * |
168 | 182 | * @param oldName the old name of the enum constant, not null |
169 | 183 | * @param currentValue the current enum constant, not null |
175 | 189 | if (currentValue == null) { |
176 | 190 | throw new IllegalArgumentException("currentValue must not be null"); |
177 | 191 | } |
192 | checkNotLocked(); | |
178 | 193 | Class<?> enumType = currentValue.getDeclaringClass(); |
179 | 194 | Map<String, Enum<?>> perClass = enumRenames.get(enumType); |
180 | 195 | if (perClass == null) { |
239 | 254 | } |
240 | 255 | |
241 | 256 | //----------------------------------------------------------------------- |
257 | /** | |
258 | * Locks this instance of the rename handler. | |
259 | * <p> | |
260 | * For additional security, an application should lock the rename handler | |
261 | * once any types and enums have been registered. | |
262 | */ | |
263 | public void lock() { | |
264 | checkNotLocked(); | |
265 | locked = true; | |
266 | } | |
267 | ||
268 | // ensure not locked | |
269 | private void checkNotLocked() { | |
270 | if (locked) { | |
271 | throw new IllegalStateException("RenameHandler has been locked and it cannot now be changed"); | |
272 | } | |
273 | } | |
274 | ||
275 | //----------------------------------------------------------------------- | |
242 | 276 | @Override |
243 | 277 | public String toString() { |
244 | 278 | return "RenamedTypes" + typeRenames + ",RenamedEnumConstants" + enumRenames; |
31 | 31 | extends AbstractTypeStringConverter |
32 | 32 | implements TypedStringConverter<Type> { |
33 | 33 | |
34 | public TypeStringConverter() { | |
34 | TypeStringConverter() { | |
35 | 35 | } |
36 | 36 | |
37 | 37 | @Override |
29 | 29 | extends AbstractTypeStringConverter |
30 | 30 | implements TypedStringConverter<TypeToken<?>> { |
31 | 31 | |
32 | public TypeTokenStringConverter() { | |
32 | TypeTokenStringConverter() { | |
33 | 33 | } |
34 | 34 | |
35 | 35 | @Override |
101 | 101 | if (str.length() == 0) { |
102 | 102 | return EMPTY; |
103 | 103 | } |
104 | Character[] array = new Character[str.length()]; | |
104 | String adjusted = str; | |
105 | Character[] array = new Character[adjusted.length()]; | |
105 | 106 | int arrayPos = 0; |
106 | 107 | int pos; |
107 | while ((pos = str.indexOf('\\')) >= 0) { | |
108 | while ((pos = adjusted.indexOf('\\')) >= 0) { | |
108 | 109 | for (int i = 0; i < pos; i++) { |
109 | array[arrayPos++] = str.charAt(i); | |
110 | array[arrayPos++] = adjusted.charAt(i); | |
110 | 111 | } |
111 | if (str.charAt(pos + 1) == '\\') { | |
112 | if (adjusted.charAt(pos + 1) == '\\') { | |
112 | 113 | array[arrayPos++] = '\\'; |
113 | } else if (str.charAt(pos + 1) == '-') { | |
114 | } else if (adjusted.charAt(pos + 1) == '-') { | |
114 | 115 | array[arrayPos++] = null; |
115 | 116 | } else { |
116 | 117 | throw new IllegalArgumentException("Invalid Character[] string, incorrect escape"); |
117 | 118 | } |
118 | str = str.substring(pos + 2); | |
119 | adjusted = adjusted.substring(pos + 2); | |
119 | 120 | } |
120 | for (int i = 0; i < str.length(); i++) { | |
121 | array[arrayPos++] = str.charAt(i); | |
121 | for (int i = 0; i < adjusted.length(); i++) { | |
122 | array[arrayPos++] = adjusted.charAt(i); | |
122 | 123 | } |
123 | 124 | return Arrays.copyOf(array, arrayPos); |
124 | 125 | } |
75 | 75 | |
76 | 76 | ## <i></i> Releases |
77 | 77 | |
78 | [Release 1.8.2](download.html) is the current latest release. | |
78 | [Release 1.8.3](download.html) is the current latest release. | |
79 | 79 | This release is considered stable and worthy of the 1.x tag. |
80 | 80 | |
81 | 81 | Joda-Convert requires Java SE 6 or later and has [no dependencies](dependencies.html). |
82 | 82 | |
83 | Available in [Maven Central](http://search.maven.org/#artifactdetails%7Corg.joda%7Cjoda-convert%7C1.8.2%7Cjar). | |
83 | Available in [Maven Central](http://search.maven.org/#artifactdetails%7Corg.joda%7Cjoda-convert%7C1.8.3%7Cjar). | |
84 | 84 | |
85 | 85 | ```xml |
86 | 86 | <dependency> |
87 | 87 | <groupId>org.joda</groupId> |
88 | 88 | <artifactId>joda-convert</artifactId> |
89 | <version>1.8.2</version> | |
89 | <version>1.8.3</version> | |
90 | 90 | </dependency> |
91 | 91 | ``` |
92 | 92 |
0 | /* | |
1 | * Copyright 2010-present Stephen Colebourne | |
2 | * | |
3 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | * you may not use this file except in compliance with the License. | |
5 | * You may obtain a copy of the License at | |
6 | * | |
7 | * http://www.apache.org/licenses/LICENSE-2.0 | |
8 | * | |
9 | * Unless required by applicable law or agreed to in writing, software | |
10 | * distributed under the License is distributed on an "AS IS" BASIS, | |
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | * See the License for the specific language governing permissions and | |
13 | * limitations under the License. | |
14 | */ | |
15 | package org.joda.convert; | |
16 | ||
17 | import org.junit.Assert; | |
18 | import org.junit.Test; | |
19 | ||
20 | /** | |
21 | * Test {@link RenameHandler}. | |
22 | */ | |
23 | public class TestRenameHandler { | |
24 | ||
25 | @Test | |
26 | public void test_matchRenamedType() throws ClassNotFoundException { | |
27 | RenameHandler test = RenameHandler.create(); | |
28 | test.renamedType("com.foo.Bar", TestRenameHandler.class); | |
29 | Class<?> renamed = test.lookupType("com.foo.Bar"); | |
30 | Assert.assertEquals(TestRenameHandler.class, renamed); | |
31 | } | |
32 | ||
33 | @Test(expected = ClassNotFoundException.class) | |
34 | public void test_noMatchType() throws ClassNotFoundException { | |
35 | RenameHandler test = RenameHandler.create(); | |
36 | test.lookupType("com.foo.Foo"); | |
37 | } | |
38 | ||
39 | @Test(expected = IllegalArgumentException.class) | |
40 | public void test_renameBlockedType1() { | |
41 | RenameHandler test = RenameHandler.create(); | |
42 | test.renamedType("java.lang.String", TestRenameHandler.class); | |
43 | } | |
44 | ||
45 | @Test(expected = IllegalArgumentException.class) | |
46 | public void test_renameBlockedType2() { | |
47 | RenameHandler test = RenameHandler.create(); | |
48 | test.renamedType("javax.foo.Bar", TestRenameHandler.class); | |
49 | } | |
50 | ||
51 | @Test(expected = IllegalArgumentException.class) | |
52 | public void test_renameBlockedType3() { | |
53 | RenameHandler test = RenameHandler.create(); | |
54 | test.renamedType("org.joda.foo.Bar", TestRenameHandler.class); | |
55 | } | |
56 | ||
57 | @Test(expected = IllegalStateException.class) | |
58 | public void test_locked() { | |
59 | RenameHandler test = RenameHandler.create(); | |
60 | test.renamedType("com.foo.Bar", TestRenameHandler.class); | |
61 | test.lock(); | |
62 | test.renamedType("com.foo.Foo", TestRenameHandler.class); | |
63 | } | |
64 | ||
65 | } |