Return-Path: X-Original-To: apmail-aries-commits-archive@www.apache.org Delivered-To: apmail-aries-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9892E188A0 for ; Wed, 9 Mar 2016 15:22:20 +0000 (UTC) Received: (qmail 39054 invoked by uid 500); 9 Mar 2016 15:22:20 -0000 Delivered-To: apmail-aries-commits-archive@aries.apache.org Received: (qmail 38991 invoked by uid 500); 9 Mar 2016 15:22:20 -0000 Mailing-List: contact commits-help@aries.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@aries.apache.org Delivered-To: mailing list commits@aries.apache.org Received: (qmail 38979 invoked by uid 99); 9 Mar 2016 15:22:20 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd4-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 09 Mar 2016 15:22:20 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd4-us-west.apache.org (ASF Mail Server at spamd4-us-west.apache.org) with ESMTP id BF1A5C0EB1 for ; Wed, 9 Mar 2016 15:22:19 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd4-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 1.471 X-Spam-Level: * X-Spam-Status: No, score=1.471 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_LAZY_DOMAIN_SECURITY=1, RP_MATCHES_RCVD=-0.329] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd4-us-west.apache.org [10.40.0.11]) (amavisd-new, port 10024) with ESMTP id nyQeCohoYhTy for ; Wed, 9 Mar 2016 15:22:17 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with ESMTP id 9A7755FAD3 for ; Wed, 9 Mar 2016 15:22:16 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id A9C84E0BBD for ; Wed, 9 Mar 2016 15:22:15 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id EF4AC3A0734 for ; Wed, 9 Mar 2016 15:22:15 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1734264 - in /aries/trunk/blueprint/blueprint-core/src: main/java/org/apache/aries/blueprint/container/ test/java/org/apache/aries/blueprint/ test/java/org/apache/aries/blueprint/container/ test/java/org/apache/aries/blueprint/pojos/ test/... Date: Wed, 09 Mar 2016 15:22:15 -0000 To: commits@aries.apache.org From: gnodet@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20160309152215.EF4AC3A0734@svn01-us-west.apache.org> Author: gnodet Date: Wed Mar 9 15:22:15 2016 New Revision: 1734264 URL: http://svn.apache.org/viewvc?rev=1734264&view=rev Log: [ARIES-1500] Fix some problems with conversions of wildcard types Added: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoGenerics2.java Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/GenericType.java aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/WiringTest.java aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java aries/trunk/blueprint/blueprint-core/src/test/resources/test-generics.xml Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java?rev=1734264&r1=1734263&r2=1734264&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java (original) +++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/AggregateConverter.java Wed Mar 9 15:22:15 2016 @@ -42,6 +42,7 @@ import java.util.concurrent.atomic.Atomi import java.util.regex.Pattern; import org.apache.aries.blueprint.container.BeanRecipe.UnwrapperedBeanHolder; +import org.apache.aries.blueprint.container.GenericType.BoundType; import org.apache.aries.blueprint.di.CollectionRecipe; import org.apache.aries.blueprint.di.MapRecipe; import org.apache.aries.blueprint.services.ExtendedBlueprintContainer; @@ -460,6 +461,22 @@ public class AggregateConverter implemen if (from.equals(to)) { return true; } + if (from.getRawClass() == to.getRawClass()) { + if (from.size() == to.size()) { + boolean ok = true; + for (int i = 0; i < from.size(); i++) { + ReifiedType tf = from.getActualTypeArgument(i); + ReifiedType tt = to.getActualTypeArgument(i); + if (!isWildcardCompatible(tf, tt)) { + ok = false; + break; + } + } + if (ok) { + return true; + } + } + } Type t = from.getRawClass().getGenericSuperclass(); if (t != null && isTypeAssignable(new GenericType(t), to)) { return true; @@ -472,6 +489,18 @@ public class AggregateConverter implemen return false; } + private static boolean isWildcardCompatible(ReifiedType from, ReifiedType to) { + BoundType fromBoundType = GenericType.boundType(from); + BoundType toBoundType = GenericType.boundType(to); + if (toBoundType == BoundType.Extends) { + return fromBoundType != BoundType.Super && isTypeAssignable(from, GenericType.bound(to)); + } else if (toBoundType == BoundType.Super) { + return fromBoundType != BoundType.Extends && isTypeAssignable(GenericType.bound(to), from); + } else { + return fromBoundType == BoundType.Exact && GenericType.bound(from).equals(GenericType.bound(to)); + } + } + private static Class unwrap(Class c) { Class u = primitives.get(c); return u != null ? u : c; Modified: aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/GenericType.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/GenericType.java?rev=1734264&r1=1734263&r2=1734264&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/GenericType.java (original) +++ aries/trunk/blueprint/blueprint-core/src/main/java/org/apache/aries/blueprint/container/GenericType.java Wed Mar 9 15:22:15 2016 @@ -56,15 +56,27 @@ public class GenericType extends Reified primitiveClasses.put("boolean", boolean.class); } + enum BoundType { + Exact, + Extends, + Super + } + private GenericType[] parameters; + private BoundType boundType; public GenericType(Type type) { - this(getConcreteClass(type), parametersOf(type)); + this(getConcreteClass(type), boundType(type), parametersOf(type)); } public GenericType(Class clazz, GenericType... parameters) { + this(clazz, BoundType.Exact, parameters); + } + + public GenericType(Class clazz, BoundType boundType, GenericType... parameters) { super(clazz); this.parameters = parameters; + this.boundType = boundType; } public static GenericType parse(String rawType, final Object loader) throws ClassNotFoundException, IllegalArgumentException { @@ -133,6 +145,12 @@ public class GenericType extends Reified @Override public String toString() { + StringBuilder sb = new StringBuilder(); + if (boundType == BoundType.Extends) { + sb.append("? extends "); + } else if (boundType == BoundType.Super) { + sb.append("? super "); + } Class cl = getRawClass(); if (cl.isArray()) { if (parameters.length > 0) { @@ -141,9 +159,8 @@ public class GenericType extends Reified return cl.getComponentType().getName() + "[]"; } } + sb.append(cl.getName()); if (parameters.length > 0) { - StringBuilder sb = new StringBuilder(); - sb.append(cl.getName()); sb.append("<"); for (int i = 0; i < parameters.length; i++) { if (i > 0) { @@ -152,9 +169,8 @@ public class GenericType extends Reified sb.append(parameters[i].toString()); } sb.append(">"); - return sb.toString(); } - return cl.getName(); + return sb.toString(); } public boolean equals(Object object) { @@ -165,6 +181,9 @@ public class GenericType extends Reified if (getRawClass() != other.getRawClass()) { return false; } + if (boundType != other.boundType) { + return false; + } if (parameters == null) { return (other.parameters == null); } else { @@ -182,7 +201,33 @@ public class GenericType extends Reified return true; } } - + + static ReifiedType bound(ReifiedType type) { + if (type instanceof GenericType + && ((GenericType) type).boundType != BoundType.Exact) { + GenericType t = (GenericType) type; + return new GenericType(t.getRawClass(), BoundType.Exact, t.parameters); + } + return type; + } + + static BoundType boundType(ReifiedType type) { + if (type instanceof GenericType) { + return ((GenericType) type).boundType; + } else { + return BoundType.Exact; + } + } + + static BoundType boundType(Type type) { + if (type instanceof WildcardType) { + WildcardType wct = (WildcardType) type; + return wct.getLowerBounds().length == 0 + ? BoundType.Extends : BoundType.Super; + } + return BoundType.Exact; + } + static GenericType[] parametersOf(Type type) { if (type instanceof Class) { Class clazz = (Class) type; 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=1734264&r1=1734263&r2=1734264&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 Wed Mar 9 15:22:15 2016 @@ -18,6 +18,7 @@ */ package org.apache.aries.blueprint; +import java.lang.reflect.Constructor; import java.math.BigInteger; import java.net.URI; import java.util.ArrayList; @@ -36,7 +37,9 @@ import java.util.TimeZone; import junit.framework.Assert; import org.apache.aries.blueprint.CallbackTracker.Callback; +import org.apache.aries.blueprint.container.AggregateConverter; import org.apache.aries.blueprint.container.BlueprintRepository; +import org.apache.aries.blueprint.container.GenericType; import org.apache.aries.blueprint.container.ServiceRecipe; import org.apache.aries.blueprint.di.CircularDependencyException; import org.apache.aries.blueprint.di.ExecutionContext; @@ -52,6 +55,10 @@ import org.apache.aries.blueprint.pojos. import org.apache.aries.blueprint.pojos.PojoA; import org.apache.aries.blueprint.pojos.PojoB; import org.apache.aries.blueprint.pojos.PojoGenerics; +import org.apache.aries.blueprint.pojos.PojoGenerics2.MyClass; +import org.apache.aries.blueprint.pojos.PojoGenerics2.MyObject; +import org.apache.aries.blueprint.pojos.PojoGenerics2.Tata; +import org.apache.aries.blueprint.pojos.PojoGenerics2.Toto; import org.apache.aries.blueprint.pojos.PojoListener; import org.apache.aries.blueprint.pojos.PojoRecursive; import org.apache.aries.blueprint.pojos.Primavera; @@ -399,6 +406,12 @@ public class WiringTest extends Abstract assertEquals(map, ((Multiple)obj).getProperties()); } + public void testGenerics2() throws Exception { + ComponentDefinitionRegistryImpl registry = parse("/test-generics.xml"); + Repository repository = new TestBlueprintContainer(registry).getRepository(); + repository.create("gen2"); + } + public void testGenerics() throws Exception { ComponentDefinitionRegistryImpl registry = parse("/test-generics.xml"); Repository repository = new TestBlueprintContainer(registry).getRepository(); Modified: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java?rev=1734264&r1=1734263&r2=1734264&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java (original) +++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/container/AggregateConverterTest.java Wed Mar 9 15:22:15 2016 @@ -19,6 +19,7 @@ package org.apache.aries.blueprint.container; import java.io.ByteArrayOutputStream; +import java.lang.reflect.Constructor; import java.math.BigInteger; import java.net.URI; import java.net.URL; @@ -33,6 +34,10 @@ import java.util.Properties; import junit.framework.TestCase; import org.apache.aries.blueprint.TestBlueprintContainer; +import org.apache.aries.blueprint.pojos.PojoGenerics2.MyClass; +import org.apache.aries.blueprint.pojos.PojoGenerics2.MyObject; +import org.apache.aries.blueprint.pojos.PojoGenerics2.Tata; +import org.apache.aries.blueprint.pojos.PojoGenerics2.Toto; import org.osgi.service.blueprint.container.ReifiedType; import org.osgi.service.blueprint.container.Converter; @@ -193,6 +198,14 @@ public class AggregateConverterTest exte assertNull(result); } + public void testGenericWilcard() throws Exception { + Constructor cns = MyClass.class.getConstructor(MyObject.class); + assertTrue(AggregateConverter.isAssignable(new Toto(), new GenericType(cns.getGenericParameterTypes()[0]))); + + cns = Tata.class.getConstructor(MyObject.class); + assertTrue(AggregateConverter.isAssignable(new Toto(), new GenericType(cns.getGenericParameterTypes()[0]))); + } + public void testGenericAssignable() throws Exception { AggregateConverter s = new AggregateConverter(new TestBlueprintContainer(null)); Added: aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoGenerics2.java URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoGenerics2.java?rev=1734264&view=auto ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoGenerics2.java (added) +++ aries/trunk/blueprint/blueprint-core/src/test/java/org/apache/aries/blueprint/pojos/PojoGenerics2.java Wed Mar 9 15:22:15 2016 @@ -0,0 +1,51 @@ +/* + * 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.pojos; + +import java.util.List; +import java.util.Map; +import java.util.Set; + +public class PojoGenerics2 { + + public static class MyClass { + + private final MyObject object; + + public MyClass(final MyObject object) + { + this.object = object; + } + } + + public static class Tata extends MyClass { + + public Tata(MyObject object) { + super(object); + } + + } + + public static class MyObject { + } + + public static class Toto extends MyObject { + } + +} Modified: aries/trunk/blueprint/blueprint-core/src/test/resources/test-generics.xml URL: http://svn.apache.org/viewvc/aries/trunk/blueprint/blueprint-core/src/test/resources/test-generics.xml?rev=1734264&r1=1734263&r2=1734264&view=diff ============================================================================== --- aries/trunk/blueprint/blueprint-core/src/test/resources/test-generics.xml (original) +++ aries/trunk/blueprint/blueprint-core/src/test/resources/test-generics.xml Wed Mar 9 15:22:15 2016 @@ -20,6 +20,12 @@ xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0" default-availability="mandatory" > + + + + + +