felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dav...@apache.org
Subject svn commit: r1804522 - in /felix/trunk/converter/converter: pom.xml src/main/java/org/apache/felix/converter/impl/ConverterImpl.java src/test/java/org/apache/felix/converter/impl/ConverterTest.java
Date Wed, 09 Aug 2017 13:33:11 GMT
Author: davidb
Date: Wed Aug  9 13:33:11 2017
New Revision: 1804522

URL: http://svn.apache.org/viewvc?rev=1804522&view=rev
Log:
Reflectively add Java 8 conversions, if running under Java 8.

Modified:
    felix/trunk/converter/converter/pom.xml
    felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
    felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java

Modified: felix/trunk/converter/converter/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/pom.xml?rev=1804522&r1=1804521&r2=1804522&view=diff
==============================================================================
--- felix/trunk/converter/converter/pom.xml (original)
+++ felix/trunk/converter/converter/pom.xml Wed Aug  9 13:33:11 2017
@@ -39,7 +39,7 @@
 
     <properties>
         <felix.java.version>8</felix.java.version>
-        <felix.java.signature.artifactId>java18</felix.java.signature.artifactId>
<!-- Can we move this to java17? -->
+        <felix.java.signature.artifactId>java17</felix.java.signature.artifactId>
<!-- Can we move this to java17? -->
     </properties>
 
     <build>

Modified: felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java?rev=1804522&r1=1804521&r2=1804522&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
(original)
+++ felix/trunk/converter/converter/src/main/java/org/apache/felix/converter/impl/ConverterImpl.java
Wed Aug  9 13:33:11 2017
@@ -16,14 +16,17 @@
  */
 package org.apache.felix.converter.impl;
 
+import java.lang.reflect.Method;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.UUID;
 import java.util.regex.Pattern;
 
+import org.osgi.util.converter.ConversionException;
 import org.osgi.util.converter.ConverterBuilder;
 import org.osgi.util.converter.Functioning;
 import org.osgi.util.converter.Rule;
+import org.osgi.util.converter.TypeRule;
 import org.osgi.util.function.Function;
 
 public class ConverterImpl implements InternalConverter {
@@ -156,14 +159,6 @@ public class ConverterImpl implements In
             }
         }) {});
 
-        // TODO
-//        cb.rule(new Rule<String, LocalDateTime>(LocalDateTime::parse) {});
-//        cb.rule(new Rule<String, LocalDate>(LocalDate::parse) {});
-//        cb.rule(new Rule<String, LocalTime>(LocalTime::parse) {});
-//        cb.rule(new Rule<String, OffsetDateTime>(OffsetDateTime::parse) {});
-//        cb.rule(new Rule<String, OffsetTime>(OffsetTime::parse) {});
-//        cb.rule(new Rule<String, ZonedDateTime>(ZonedDateTime::parse) {});
-
 //        cb.rule(new Rule<String, Pattern>(Pattern::compile) {});
         cb.rule(new Rule<String, Pattern>(new Function<String, Pattern>() {
             @Override
@@ -211,6 +206,40 @@ public class ConverterImpl implements In
                 return stringToCharacterArray(s);
             }
         }) {});
+
+        // TODO
+//      cb.rule(new Rule<String, LocalDateTime>(LocalDateTime::parse) {});
+        reflectiveAddRule(cb, "java.time.LocalDateTime", "parse");
+//      cb.rule(new Rule<String, LocalDate>(LocalDate::parse) {});
+        reflectiveAddRule(cb, "java.time.LocalDate", "parse");
+//      cb.rule(new Rule<String, LocalTime>(LocalTime::parse) {});
+        reflectiveAddRule(cb, "java.time.LocalTime", "parse");
+//      cb.rule(new Rule<String, OffsetDateTime>(OffsetDateTime::parse) {});
+        reflectiveAddRule(cb, "java.time.OffsetDateTime", "parse");
+//      cb.rule(new Rule<String, OffsetTime>(OffsetTime::parse) {});
+        reflectiveAddRule(cb, "java.time.OffsetTime", "parse");
+//      cb.rule(new Rule<String, ZonedDateTime>(ZonedDateTime::parse) {});
+        reflectiveAddRule(cb, "java.time.ZonedDateTime", "parse");
+    }
+
+    private void reflectiveAddRule(ConverterBuilder cb, String toClsName, String methodName)
{
+        try {
+            final Class<?> toCls = getClass().getClassLoader().loadClass(toClsName);
+            final Method toMethod = toCls.getMethod(methodName, CharSequence.class);
+
+            cb.rule(new TypeRule<String, Object>(String.class, toCls, new Function<String,
Object>() {
+                @Override
+                public Object apply(String f) {
+                    try {
+                        return toMethod.invoke(null, f);
+                    } catch (Exception e) {
+                        throw new ConversionException("Problem converting to " + toCls, e);
+                    }
+                }
+            }));
+        } catch (Exception ex) {
+            // Class not available, do not add rule for it
+        }
     }
 
     private String charArrayToString(char[] ca) {

Modified: felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java?rev=1804522&r1=1804521&r2=1804522&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
(original)
+++ felix/trunk/converter/converter/src/test/java/org/apache/felix/converter/impl/ConverterTest.java
Wed Aug  9 13:33:11 2017
@@ -449,7 +449,6 @@ public class ConverterTest {
     }
 
     @Test
-    @Ignore("Code needs to be converted to Java 7")
     public void testLocalDateTime() {
         LocalDateTime ldt = LocalDateTime.now();
         String s = converter.convert(ldt).to(String.class);
@@ -459,7 +458,6 @@ public class ConverterTest {
     }
 
     @Test
-    @Ignore("Code needs to be converted to Java 7")
     public void testLocalDate() {
         LocalDate ld = LocalDate.now();
         String s = converter.convert(ld).to(String.class);
@@ -469,7 +467,6 @@ public class ConverterTest {
     }
 
     @Test
-    @Ignore("Code needs to be converted to Java 7")
     public void testLocalTime() {
         LocalTime lt = LocalTime.now();
         String s = converter.convert(lt).to(String.class);
@@ -479,7 +476,6 @@ public class ConverterTest {
     }
 
     @Test
-    @Ignore("Code needs to be converted to Java 7")
     public void testOffsetDateTime() {
         OffsetDateTime ot = OffsetDateTime.now();
         String s = converter.convert(ot).to(String.class);
@@ -489,7 +485,6 @@ public class ConverterTest {
     }
 
     @Test
-    @Ignore("Code needs to be converted to Java 7")
     public void testOffsetTime() {
         OffsetTime ot = OffsetTime.now();
         String s = converter.convert(ot).to(String.class);
@@ -499,7 +494,6 @@ public class ConverterTest {
     }
 
     @Test
-    @Ignore("Code needs to be converted to Java 7")
     public void testZonedDateTime() {
         ZonedDateTime zdt = ZonedDateTime.now();
         String s = converter.convert(zdt).to(String.class);



Mime
View raw message