You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2013/12/10 03:31:39 UTC

svn commit: r1549744 - /felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java

Author: cziegeler
Date: Tue Dec 10 02:31:39 2013
New Revision: 1549744

URL: http://svn.apache.org/r1549744
Log:
FELIX-4083 : [Core R5] Support for valueOf when evaluating a Filter. Apply patch from David Bosschaert

Modified:
    felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java

Modified: felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java
URL: http://svn.apache.org/viewvc/felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java?rev=1549744&r1=1549743&r2=1549744&view=diff
==============================================================================
--- felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java (original)
+++ felix/trunk/framework/src/main/java/org/apache/felix/framework/capabilityset/CapabilitySet.java Tue Dec 10 02:31:39 2013
@@ -20,6 +20,8 @@ package org.apache.felix.framework.capab
 
 import java.lang.reflect.Array;
 import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -367,6 +369,7 @@ public void dump()
     }
 
     private static final Class<?>[] STRING_CLASS = new Class[] { String.class };
+    private static final String VALUE_OF_METHOD_NAME = "valueOf";
 
     private static boolean compare(Object lhs, Object rhsUnknown, int op)
     {
@@ -374,7 +377,7 @@ public void dump()
         {
             return false;
         }
-        
+
         // If this is a PRESENT operation, then just return true immediately
         // since we wouldn't be here if the attribute wasn't present.
         if (op == SimpleFilter.PRESENT)
@@ -569,9 +572,24 @@ public void dump()
                 {
                     rhsString = rhsString.trim();
                 }
-                Constructor ctor = m_secureAction.getConstructor(lhs.getClass(), STRING_CLASS);
-                m_secureAction.setAccesssible(ctor);
-                rhs = ctor.newInstance(new Object[] { rhsString });
+
+                try {
+                    // Try to find a suitable static valueOf method
+                    Method valueOfMethod = m_secureAction.getDeclaredMethod(lhs.getClass(), VALUE_OF_METHOD_NAME, STRING_CLASS);
+                    if (valueOfMethod.getReturnType().isAssignableFrom(lhs.getClass()) &&
+                        ((valueOfMethod.getModifiers() & Modifier.STATIC) > 0)) {
+                        m_secureAction.setAccesssible(valueOfMethod);
+                        rhs = valueOfMethod.invoke(null, new Object[] { rhsString });
+                    }
+                } catch (Exception ex) {
+                    // Static valueOf fails, try the next conversion mechanism
+                }
+
+                if (rhs == null) {
+                    Constructor ctor = m_secureAction.getConstructor(lhs.getClass(), STRING_CLASS);
+                    m_secureAction.setAccesssible(ctor);
+                    rhs = ctor.newInstance(new Object[] { rhsString });
+                }
             }
         }
         catch (Exception ex)
@@ -604,4 +622,4 @@ public void dump()
         }
         return list;
     }
-}
\ No newline at end of file
+}