You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by ro...@apache.org on 2019/02/13 20:30:34 UTC

svn commit: r1853533 - in /felix/trunk/converter/converter/src: main/java/org/osgi/util/converter/ test/java/org/osgi/util/converter/

Author: rotty3000
Date: Wed Feb 13 20:30:34 2019
New Revision: 1853533

URL: http://svn.apache.org/viewvc?rev=1853533&view=rev
Log:
FELIX-6057 Converter doesn't properly handle PREFIX_ in marker annotations

Added:
    felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/PrefixMarkerAnnotation.java
Modified:
    felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
    felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java
    felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterMapTest.java

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java?rev=1853533&r1=1853532&r2=1853533&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/ConvertingImpl.java Wed Feb 13 20:30:34 2019
@@ -906,8 +906,7 @@ class ConvertingImpl extends AbstractSpe
 		} else if (Annotation.class.isAssignableFrom(sourceClass)
 				&& isMarkerAnnotation(sourceClass)) {
 			// Special treatment for marker annotations
-			Class< ? > ann = Util.getAnnotationType(sourceClass, object);
-			String key = Util.toSingleElementAnnotationKey(ann.getSimpleName());
+			String key = Util.getMarkerAnnotationKey(sourceClass, object);
 			return converter
 					.convert(Collections.singletonMap(key, Boolean.TRUE))
 					.targetAs(targetAsClass)

Modified: felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java?rev=1853533&r1=1853532&r2=1853533&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java (original)
+++ felix/trunk/converter/converter/src/main/java/org/osgi/util/converter/Util.java Wed Feb 13 20:30:34 2019
@@ -191,6 +191,11 @@ class Util {
 		return keys;
 	}
 
+	static String getMarkerAnnotationKey(Class< ? > intf, Object obj) {
+		Class< ? > ann = getAnnotationType(intf, obj);
+		return getPrefix(intf) + toSingleElementAnnotationKey(ann.getSimpleName());
+	}
+
 	static String getSingleElementAnnotationKey(Class< ? > intf, Object obj) {
 		Class< ? > ann = getAnnotationType(intf, obj);
 		if (ann == null)

Modified: felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterMapTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterMapTest.java?rev=1853533&r1=1853532&r2=1853533&view=diff
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterMapTest.java (original)
+++ felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/ConverterMapTest.java Wed Feb 13 20:30:34 2019
@@ -20,8 +20,11 @@ import org.junit.After;
 import org.junit.Before;
 import org.junit.Ignore;
 import org.junit.Test;
+import org.osgi.util.converter.ConverterMapTest.TestValue;
 
 import java.lang.annotation.Annotation;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
 import java.lang.reflect.Method;
 import java.math.BigInteger;
 import java.net.URL;
@@ -590,6 +593,27 @@ public class ConverterMapTest {
         assertTrue(testValue.value());
     }
 
+    @Test
+    @TestValue(true)
+    public void testSingleElementAnnotationPrefixToMap() throws Exception {
+        final Converter converter = Converters.standardConverter();
+        Method method = getClass().getMethod("testSingleElementAnnotationPrefixToMap");
+        TestValue annotation = method.getDeclaredAnnotation(TestValue.class);
+        Map<String, Object> map = converter.convert(annotation).to(new TypeReference<Map<String, Object>>() {});
+        assertTrue((Boolean)map.get("my.prefix.test.value"));
+    }
+
+    @Test
+    @PrefixMarkerAnnotation
+    public void testMarkerAnnotationPrefixToMap() throws Exception {
+        final Converter converter = Converters.standardConverter();
+        Method method = getClass().getMethod("testMarkerAnnotationPrefixToMap");
+        PrefixMarkerAnnotation annotation = method.getDeclaredAnnotation(PrefixMarkerAnnotation.class);
+        Map<String, Object> map = converter.convert(annotation).to(new TypeReference<Map<String, Object>>() {});
+        assertTrue(map.containsKey("org.foo.bar.prefix.marker.annotation"));
+        assertTrue((Boolean)map.get("org.foo.bar.prefix.marker.annotation"));
+    }
+
     private <K,V> Map.Entry<K,V> getMapEntry(Map<K,V> map) {
         assertEquals("This method assumes a map of size 1", 1, map.size());
         return map.entrySet().iterator().next();
@@ -620,6 +644,7 @@ public class ConverterMapTest {
     	long somethingElse() default -87;
     }
 
+    @Retention(RetentionPolicy.RUNTIME)
     @interface TestValue {
         static final String PREFIX_ = "my.prefix.";
 

Added: felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/PrefixMarkerAnnotation.java
URL: http://svn.apache.org/viewvc/felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/PrefixMarkerAnnotation.java?rev=1853533&view=auto
==============================================================================
--- felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/PrefixMarkerAnnotation.java (added)
+++ felix/trunk/converter/converter/src/test/java/org/osgi/util/converter/PrefixMarkerAnnotation.java Wed Feb 13 20:30:34 2019
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.osgi.util.converter;
+
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+
+@Retention(RetentionPolicy.RUNTIME)
+public @interface PrefixMarkerAnnotation {
+    static final String PREFIX_ = "org.foo.bar.";
+}