Author: gnodet
Date: Fri Jun  1 16:15:45 2018
New Revision: 1832703

URL: http://svn.apache.org/viewvc?rev=1832703&view=rev
Log:
[ARIES-1793] Blueprint interceptors do not work anymore

Added:
    
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/
    
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanA.java
    
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanB.java
    
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanItf.java
    
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheInterceptor.java
    
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheProcessor.java
    
aries/trunk/blueprint/blueprint-core/src/test/resources/test-interceptors.xml
Modified:
    
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
    
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java
    
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/WiringTest.java

Modified: 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java?rev=1832703&r1=1832702&r2=1832703&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/BeanRecipe.java
 Fri Jun  1 16:15:45 2018
@@ -813,10 +813,7 @@ public class BeanRecipe extends Abstract
         final PropertyDescriptor pd = getPropertyDescriptor(clazz, 
names[names.length - 1]);
         if (pd.allowsSet()) {
             try {
-                Object val = propertyValue instanceof UnwrapperedBeanHolder
-                        ? ((UnwrapperedBeanHolder) 
propertyValue).unwrapperedBean
-                        : propertyValue;
-                pd.set(instance, val, blueprintContainer);
+                pd.set(instance, propertyValue, blueprintContainer);
             } catch (Exception e) {
                 throw new ComponentDefinitionException("Error setting 
property: " + pd, getRealCause(e));
             }

Modified: 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java?rev=1832703&r1=1832702&r2=1832703&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/utils/ReflectionUtils.java
 Fri Jun  1 16:15:45 2018
@@ -798,17 +798,28 @@ public class ReflectionUtils {
 
         private Method findMethodWithConversion(Collection<Method> 
setterMethods, Object value) throws Exception {
             ExecutionContext ctx = ExecutionContext.Holder.getContext();
-            List<Method> matchingMethods = new ArrayList<Method>();
+            Method matchingMethod = null;
             for (Method m : setterMethods) {
                 Type paramType = m.getGenericParameterTypes()[0];
-                if (ctx.canConvert(value, new GenericType(paramType))) 
matchingMethods.add(m);
+                if (ctx.canConvert(value, new GenericType(paramType))) {
+                    if (matchingMethod != null) {
+                        Class<?> p1Class = 
matchingMethod.getParameterTypes()[0];
+                        Class<?> p2Class = m.getParameterTypes()[0];
+                        if(!p2Class.equals(p1Class) && 
p1Class.isAssignableFrom(p2Class)) {
+                            // Keep method whose parameter type is the closest 
to value type
+                            matchingMethod = m;
+                        } else if(p2Class.equals(p1Class) || 
!p2Class.isAssignableFrom(p1Class)) {
+                            throw new ComponentDefinitionException(
+                                "Ambiguous setter method for property "
+                                + getName() + ". More than one method matches 
the parameter "
+                                + value + " after applying conversion.");
+                        }
+                    } else {
+                        matchingMethod = m;
+                    }
+                }
             }
-            
-            if (matchingMethods.isEmpty()) return null;
-            else if (matchingMethods.size() == 1) return 
matchingMethods.get(0);
-            else throw new ComponentDefinitionException(
-                    "Ambiguous setter method for property "+ getName() + 
-                    ". More than one method matches the parameter "+value+" 
after applying conversion.");
+            return matchingMethod;
         }
         
         public String toString() {

Modified: 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/WiringTest.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/WiringTest.java?rev=1832703&r1=1832702&r2=1832703&view=diff
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/WiringTest.java
 (original)
+++ 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/WiringTest.java
 Fri Jun  1 16:15:45 2018
@@ -43,6 +43,9 @@ import org.apache.aries.blueprint.di.Exe
 import org.apache.aries.blueprint.di.MapRecipe;
 import org.apache.aries.blueprint.di.Recipe;
 import org.apache.aries.blueprint.di.Repository;
+import org.apache.aries.blueprint.intercept.BeanA;
+import org.apache.aries.blueprint.intercept.BeanB;
+import org.apache.aries.blueprint.intercept.TheInterceptor;
 import org.apache.aries.blueprint.parser.ComponentDefinitionRegistryImpl;
 import org.apache.aries.blueprint.pojos.*;
 import org.apache.aries.blueprint.proxy.ProxyUtils;
@@ -598,6 +601,15 @@ public class WiringTest extends Abstract
         repository = createBlueprintContainer().getRepository();        
         assertNotNull(repository.create("c3"));
     }
+
+    public void testInterceptors() throws Exception {
+        ComponentDefinitionRegistryImpl registry = 
parse("/test-interceptors.xml");
+        Repository repository = new 
TestBlueprintContainer(registry).getRepository();
+        BeanB b = (BeanB) repository.create("b");
+        assertNotNull(b.getA());
+        assertEquals("Hello Guillaume !", b.getA().hello("Guillaume"));
+        assertEquals(1, TheInterceptor.calls.get());
+    }
     
     private TestBlueprintContainer createBlueprintContainer() throws Exception 
{
         ComponentDefinitionRegistryImpl registry = parse("/test-circular.xml");

Added: 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanA.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanA.java?rev=1832703&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanA.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanA.java
 Fri Jun  1 16:15:45 2018
@@ -0,0 +1,27 @@
+/*
+ * 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.apache.aries.blueprint.intercept;
+
+public class BeanA implements BeanItf {
+
+    public String hello(String msg) {
+        return "Hello " + msg + " !";
+    }
+
+}

Added: 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanB.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanB.java?rev=1832703&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanB.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanB.java
 Fri Jun  1 16:15:45 2018
@@ -0,0 +1,34 @@
+/*
+ * 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.apache.aries.blueprint.intercept;
+
+public class BeanB {
+
+    BeanItf a;
+
+    public BeanItf getA() {
+        return a;
+    }
+
+    public void setA(BeanItf a) {
+        this.a = a;
+    }
+
+
+}

Added: 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanItf.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanItf.java?rev=1832703&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanItf.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/BeanItf.java
 Fri Jun  1 16:15:45 2018
@@ -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.apache.aries.blueprint.intercept;
+
+public interface BeanItf {
+
+    String hello(String msg);
+
+}

Added: 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheInterceptor.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheInterceptor.java?rev=1832703&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheInterceptor.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheInterceptor.java
 Fri Jun  1 16:15:45 2018
@@ -0,0 +1,52 @@
+/*
+ * 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.apache.aries.blueprint.intercept;
+
+import java.lang.reflect.Method;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.aries.blueprint.Interceptor;
+import org.osgi.service.blueprint.reflect.ComponentMetadata;
+
+public class TheInterceptor implements Interceptor {
+
+    public static final AtomicInteger calls = new AtomicInteger();
+
+    @Override
+    public Object preCall(ComponentMetadata componentMetadata, Method method, 
Object... objects) throws Throwable {
+        calls.incrementAndGet();
+        return null;
+    }
+
+    @Override
+    public void postCallWithReturn(ComponentMetadata componentMetadata, Method 
method, Object o, Object o1) throws Throwable {
+
+    }
+
+    @Override
+    public void postCallWithException(ComponentMetadata componentMetadata, 
Method method, Throwable throwable, Object o) throws Throwable {
+
+    }
+
+    @Override
+    public int getRank() {
+        return 0;
+    }
+
+}

Added: 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheProcessor.java
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheProcessor.java?rev=1832703&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheProcessor.java
 (added)
+++ 
aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/intercept/TheProcessor.java
 Fri Jun  1 16:15:45 2018
@@ -0,0 +1,63 @@
+/*
+ * 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.apache.aries.blueprint.intercept;
+
+import org.apache.aries.blueprint.BeanProcessor;
+import org.apache.aries.blueprint.services.ExtendedBlueprintContainer;
+import org.osgi.service.blueprint.container.BlueprintContainer;
+import org.osgi.service.blueprint.reflect.BeanMetadata;
+
+public class TheProcessor implements BeanProcessor {
+
+    private BlueprintContainer blueprintContainer;
+
+    public BlueprintContainer getBlueprintContainer() {
+        return blueprintContainer;
+    }
+
+    public void setBlueprintContainer(BlueprintContainer blueprintContainer) {
+        this.blueprintContainer = blueprintContainer;
+    }
+
+    @Override
+    public Object beforeInit(Object bean, String beanName, BeanCreator 
beanCreator, BeanMetadata beanData) {
+        if (bean.getClass() == BeanA.class) {
+            ((ExtendedBlueprintContainer) blueprintContainer)
+                    .getComponentDefinitionRegistry()
+                    .registerInterceptorWithComponent(beanData, new 
TheInterceptor());
+        }
+        return bean;
+    }
+
+    @Override
+    public Object afterInit(Object o, String s, BeanCreator beanCreator, 
BeanMetadata beanMetadata) {
+        return o;
+    }
+
+    @Override
+    public void beforeDestroy(Object o, String s) {
+
+    }
+
+    @Override
+    public void afterDestroy(Object o, String s) {
+
+    }
+
+}

Added: 
aries/trunk/blueprint/blueprint-core/src/test/resources/test-interceptors.xml
URL: 
http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/resources/test-interceptors.xml?rev=1832703&view=auto
==============================================================================
--- 
aries/trunk/blueprint/blueprint-core/src/test/resources/test-interceptors.xml 
(added)
+++ 
aries/trunk/blueprint/blueprint-core/src/test/resources/test-interceptors.xml 
Fri Jun  1 16:15:45 2018
@@ -0,0 +1,31 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0";
+           
xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.6.0";>
+
+    <bean id="processor" ext:role="processor" 
class="org.apache.aries.blueprint.intercept.TheProcessor">
+        <property name="blueprintContainer" ref="blueprintContainer"/>
+    </bean>
+
+    <bean id="a" class="org.apache.aries.blueprint.intercept.BeanA"/>
+
+    <bean id="b" class="org.apache.aries.blueprint.intercept.BeanB">
+        <property name="a" ref="a"/>
+    </bean>
+
+</blueprint>
\ No newline at end of file


Reply via email to