From d66e044db60a1c7aa870c0eefd26e1ea0e2d561f Mon Sep 17 00:00:00 2001 From: Remy Masson Date: Wed, 21 Sep 2016 18:17:59 +0200 Subject: [PATCH 1/2] FELIX-5282 patch --- .../ipojo/handlers/dependency/Dependency.java | 61 ++++++++++++----- .../handlers/dependency/NullableTest.java | 67 +++++++++++++++++++ .../dependency/TestSpecification.java | 24 +++++++ 3 files changed, 135 insertions(+), 17 deletions(-) create mode 100644 core/src/test/java/org/apache/felix/ipojo/handlers/dependency/NullableTest.java create mode 100644 core/src/test/java/org/apache/felix/ipojo/handlers/dependency/TestSpecification.java diff --git a/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java b/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java index 89dc9a4e9ec..27dba378ee4 100644 --- a/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java +++ b/core/src/main/java/org/apache/felix/ipojo/handlers/dependency/Dependency.java @@ -416,19 +416,19 @@ private RuntimeException createExceptionToThrow() { return new RuntimeException(message); } - private Object createNullableObject() { + private void createNullableObject() { // To load the proxy we use the POJO class loader. Indeed, this classloader imports iPOJO (so can access to Nullable) and has // access to the service specification. if ( ! getSpecification().isInterface()) { getHandler().getLogger().log(Log.INFO, "Cannot create the nullable object for " + getSpecification() .getName() + " - the specification is not an interface"); - return null; + return; } try { ClassLoader cl = new NullableClassLoader( getHandler().getInstanceManager().getClazz().getClassLoader(), - getSpecification().getClassLoader()); + findClassLoadersFromSpecification(getSpecification())); m_nullable = Proxy.newProxyInstance(cl, new Class[]{ @@ -441,8 +441,28 @@ private Object createNullableObject() { } catch (Throwable e) { // Catch any other exception that can occurs throw new IllegalStateException("Cannot create the Nullable object, an unexpected error occurs", e); } + } - return m_nullable; + private List findClassLoadersFromSpecification(Class clazz) { + ArrayList classLoaders = new ArrayList(); + ClassLoader specificationCL = clazz.getClassLoader(); + classLoaders.add(specificationCL); + // use Class.getMethods() to go thru the full hierarchy of classes of the specification + for (Method method : clazz.getMethods()) { + for (Class parameterType : method.getParameterTypes()) { + ClassLoader parameterCL = parameterType.getClassLoader(); + if (parameterCL != null && !classLoaders.contains(parameterCL)) { + classLoaders.add(parameterCL); + } + } + if (!Void.TYPE.equals(method.getReturnType())) { + ClassLoader returnCL = method.getReturnType().getClassLoader(); + if (returnCL != null && !classLoaders.contains(returnCL)) { + classLoaders.add(returnCL); + } + } + } + return classLoaders; } /** @@ -1038,42 +1058,49 @@ private static class NullableClassLoader extends ClassLoader { /** * Component classloader. */ - private ClassLoader m_component; + private ClassLoader m_component; /** - * Specification classloader. + * Specification classloaders. */ - private ClassLoader m_specification; + private List m_classLoadersFromSpecification; /** * Creates a NullableClassLoader. - * - * @param cmp the component class loader. - * @param spec the specification class loader. + * @param cmp the component class loader. + * @param classLoadersFromSpecification the specification class loader plus the ones referenced by parameters and return types. */ - public NullableClassLoader(ClassLoader cmp, ClassLoader spec) { + public NullableClassLoader(ClassLoader cmp, List classLoadersFromSpecification) { m_component = cmp; - m_specification = spec; + m_classLoadersFromSpecification = classLoadersFromSpecification; } /** * Loads the given class. * This method uses the classloader of the component class - * and (if not found) the specification classloader. + * and (if not found) the specification classloaders. + * Throws the last {@link ClassNotFoundException} caught while trying + * to load the class from the specifiation classloaders * * @param name the class name * @return the class object - * @throws ClassNotFoundException if the class is not found by the two classloaders. + * @throws ClassNotFoundException if the class is not found by the classloaders. * @see java.lang.ClassLoader#loadClass(java.lang.String) */ public Class loadClass(String name) throws ClassNotFoundException { try { return m_component.loadClass(name); } catch (ClassNotFoundException e) { - return m_specification.loadClass(name); + ClassNotFoundException lastCaught = null; + for (ClassLoader classLoader : m_classLoadersFromSpecification) { + try { + return classLoader.loadClass(name); + } catch (ClassNotFoundException cnfe) { + lastCaught = cnfe; + } + } + throw lastCaught; } } - - } /** diff --git a/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/NullableTest.java b/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/NullableTest.java new file mode 100644 index 00000000000..e5e9c7bff08 --- /dev/null +++ b/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/NullableTest.java @@ -0,0 +1,67 @@ +/* + * 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.felix.ipojo.handlers.dependency; + +import java.lang.reflect.Proxy; +import org.apache.felix.ipojo.InstanceManager; +import org.apache.felix.ipojo.Nullable; +import org.apache.felix.ipojo.handlers.providedservice.ComponentTestWithSuperClass; +import org.apache.felix.ipojo.test.MockBundle; +import org.mockito.Mockito; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import junit.framework.Assert; +import junit.framework.TestCase; + +public class NullableTest extends TestCase { + + public void testOnGet_whenNullableEnabled_returnsAnInstanceOfNullableAndSpecification() { + Bundle bundle = new MockBundle(Dependency.class.getClassLoader()); + BundleContext context = Mockito.mock(BundleContext.class); + InstanceManager im = Mockito.mock(InstanceManager.class); + Mockito.when(im.getClazz()).thenReturn(ComponentTestWithSuperClass.class); + DependencyHandler handler = Mockito.mock(DependencyHandler.class); + Mockito.when(handler.getInstanceManager()).thenReturn(im); + Dependency dependency = new Dependency(handler, "a_field", TestSpecification.class, null, true, false, true, + false, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null); + dependency.start(); + + Object service = dependency.onGet(null, null, null); + + Assert.assertTrue(service instanceof Nullable); + Assert.assertTrue(service instanceof TestSpecification); + } + + public void testOnGet_whenNullableEnabled_returnsAProxyWithNullableObjectAsInvocationHandler() { + Bundle bundle = new MockBundle(Dependency.class.getClassLoader()); + BundleContext context = Mockito.mock(BundleContext.class); + InstanceManager im = Mockito.mock(InstanceManager.class); + Mockito.when(im.getClazz()).thenReturn(ComponentTestWithSuperClass.class); + DependencyHandler handler = Mockito.mock(DependencyHandler.class); + Mockito.when(handler.getInstanceManager()).thenReturn(im); + Dependency dependency = new Dependency(handler, "a_field", TestSpecification.class, null, true, false, true, + false, "dep", context, Dependency.DYNAMIC_BINDING_POLICY, null, null, null); + dependency.start(); + + Object service = dependency.onGet(null, null, null); + + Assert.assertTrue(service instanceof Proxy); + Assert.assertTrue(Proxy.getInvocationHandler(service) instanceof NullableObject); + } +} diff --git a/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/TestSpecification.java b/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/TestSpecification.java new file mode 100644 index 00000000000..05ece02a480 --- /dev/null +++ b/core/src/test/java/org/apache/felix/ipojo/handlers/dependency/TestSpecification.java @@ -0,0 +1,24 @@ +/* + * 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.felix.ipojo.handlers.dependency; + +public interface TestSpecification +{ + public String doSomething(String param); +} From 0f71c430b567b8b42a134e21676d5a1821a71f29 Mon Sep 17 00:00:00 2001 From: Remy Masson Date: Wed, 21 Sep 2016 18:33:56 +0200 Subject: [PATCH 2/2] Releasing 1.12.1-ullink1 --- core/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/pom.xml b/core/pom.xml index b0c75753a46..543506e4aa8 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -27,7 +27,7 @@ bundle Apache Felix iPOJO org.apache.felix.ipojo - 1.12.1 + 1.12.1-ullink1