Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id C2163200B7B for ; Thu, 25 Aug 2016 11:49:58 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id BF2BA160ABE; Thu, 25 Aug 2016 09:49:58 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id E0179160A93 for ; Thu, 25 Aug 2016 11:49:57 +0200 (CEST) Received: (qmail 47290 invoked by uid 500); 25 Aug 2016 09:49:57 -0000 Mailing-List: contact commits-help@cayenne.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cayenne.apache.org Delivered-To: mailing list commits@cayenne.apache.org Received: (qmail 47245 invoked by uid 99); 25 Aug 2016 09:49:57 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 25 Aug 2016 09:49:57 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 0096AE0556; Thu, 25 Aug 2016 09:49:56 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: aadamchik@apache.org To: commits@cayenne.apache.org Date: Thu, 25 Aug 2016 09:49:57 -0000 Message-Id: <89918355163b4fb390138db6a1dbd86c@git.apache.org> In-Reply-To: <6676ba8c4a354adda3f10a1a3074c540@git.apache.org> References: <6676ba8c4a354adda3f10a1a3074c540@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/4] cayenne git commit: CAY-2108 cayenne-di: StackOverflow for decorator that takes Provider of the delegate archived-at: Thu, 25 Aug 2016 09:49:58 -0000 CAY-2108 cayenne-di: StackOverflow for decorator that takes Provider of the delegate Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/023d5f18 Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/023d5f18 Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/023d5f18 Branch: refs/heads/master Commit: 023d5f18f8fb97f66b23bc13accf015c6d4f11b4 Parents: 3d96be6 Author: Andrus Adamchik Authored: Thu Aug 25 12:38:06 2016 +0300 Committer: Andrus Adamchik Committed: Thu Aug 25 12:43:59 2016 +0300 ---------------------------------------------------------------------- .../ConstructorInjectingDecoratorProvider.java | 21 +++++++++-- .../di/spi/FieldInjectingDecoratorProvider.java | 23 ++++++++++-- .../di/mock/MockInterface1_Decorator4.java | 36 ++++++++++++++++++ .../di/mock/MockInterface1_Decorator5.java | 33 +++++++++++++++++ .../di/spi/DefaultInjectorDecorationTest.java | 39 ++++++++++++++++++++ 5 files changed, 146 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cayenne/blob/023d5f18/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingDecoratorProvider.java ---------------------------------------------------------------------- diff --git a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingDecoratorProvider.java b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingDecoratorProvider.java index 4ad974f..a235847 100644 --- a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingDecoratorProvider.java +++ b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/ConstructorInjectingDecoratorProvider.java @@ -18,11 +18,11 @@ ****************************************************************/ package org.apache.cayenne.di.spi; -import java.lang.reflect.Type; - import org.apache.cayenne.di.DIRuntimeException; import org.apache.cayenne.di.Provider; +import java.lang.reflect.Type; + /** * @since 4.0 */ @@ -43,7 +43,22 @@ public class ConstructorInjectingDecoratorProvider implements DecoratorProvid @Override protected Object value(Class parameter, Type genericType, String bindingName, InjectionStack stack) { - if (parameter.isAssignableFrom(implementation)) { + // delegate (possibly) injected as Provider + if (Provider.class.equals(parameter)) { + + Class objectClass = DIUtil.parameterClass(genericType); + + if (objectClass == null) { + throw new DIRuntimeException("Constructor provider parameter %s must be " + + "parameterized to be usable for injection", parameter.getName()); + } + + if(objectClass.isAssignableFrom(implementation)) { + return undecorated; + } + } + // delegate injected as value + else if (parameter.isAssignableFrom(implementation)) { return undecorated.get(); } http://git-wip-us.apache.org/repos/asf/cayenne/blob/023d5f18/cayenne-di/src/main/java/org/apache/cayenne/di/spi/FieldInjectingDecoratorProvider.java ---------------------------------------------------------------------- diff --git a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/FieldInjectingDecoratorProvider.java b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/FieldInjectingDecoratorProvider.java index e6c072b..6cbbad0 100644 --- a/cayenne-di/src/main/java/org/apache/cayenne/di/spi/FieldInjectingDecoratorProvider.java +++ b/cayenne-di/src/main/java/org/apache/cayenne/di/spi/FieldInjectingDecoratorProvider.java @@ -18,11 +18,11 @@ ****************************************************************/ package org.apache.cayenne.di.spi; -import java.lang.reflect.Field; - import org.apache.cayenne.di.DIRuntimeException; import org.apache.cayenne.di.Provider; +import java.lang.reflect.Field; + /** * @since 4.0 */ @@ -45,7 +45,24 @@ class FieldInjectingDecoratorProvider implements DecoratorProvider { @Override protected Object value(Field field, String bindingName) { - if (field.getType().isAssignableFrom(implementation)) { + Class fieldType = field.getType(); + + // delegate (possibly) injected as Provider + if (Provider.class.equals(fieldType)) { + + Class objectClass = DIUtil.parameterClass(field.getGenericType()); + + if (objectClass == null) { + throw new DIRuntimeException("Provider field %s.%s of type %s must be " + + "parameterized to be usable for injection", field.getDeclaringClass().getName(), + field.getName(), fieldType.getName()); + } + + if(objectClass.isAssignableFrom(implementation)) { + return undecorated; + } + } + else if (fieldType.isAssignableFrom(implementation)) { return undecorated.get(); } http://git-wip-us.apache.org/repos/asf/cayenne/blob/023d5f18/cayenne-di/src/test/java/org/apache/cayenne/di/mock/MockInterface1_Decorator4.java ---------------------------------------------------------------------- diff --git a/cayenne-di/src/test/java/org/apache/cayenne/di/mock/MockInterface1_Decorator4.java b/cayenne-di/src/test/java/org/apache/cayenne/di/mock/MockInterface1_Decorator4.java new file mode 100644 index 0000000..d30f358 --- /dev/null +++ b/cayenne-di/src/test/java/org/apache/cayenne/di/mock/MockInterface1_Decorator4.java @@ -0,0 +1,36 @@ +/***************************************************************** + * 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.cayenne.di.mock; + +import org.apache.cayenne.di.Inject; +import org.apache.cayenne.di.Provider; + +public class MockInterface1_Decorator4 implements MockInterface1 { + + private Provider delegate; + + public MockInterface1_Decorator4(@Inject Provider delegate) { + this.delegate = delegate; + } + + @Override + public String getName() { + return "[4" + delegate.get().getName() + "4]"; + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cayenne/blob/023d5f18/cayenne-di/src/test/java/org/apache/cayenne/di/mock/MockInterface1_Decorator5.java ---------------------------------------------------------------------- diff --git a/cayenne-di/src/test/java/org/apache/cayenne/di/mock/MockInterface1_Decorator5.java b/cayenne-di/src/test/java/org/apache/cayenne/di/mock/MockInterface1_Decorator5.java new file mode 100644 index 0000000..0b3cd35 --- /dev/null +++ b/cayenne-di/src/test/java/org/apache/cayenne/di/mock/MockInterface1_Decorator5.java @@ -0,0 +1,33 @@ +/***************************************************************** + * 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.cayenne.di.mock; + +import org.apache.cayenne.di.Inject; +import org.apache.cayenne.di.Provider; + +public class MockInterface1_Decorator5 implements MockInterface1 { + + @Inject + private Provider delegate; + + @Override + public String getName() { + return "[5" + delegate.get().getName() + "5]"; + } +} http://git-wip-us.apache.org/repos/asf/cayenne/blob/023d5f18/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorDecorationTest.java ---------------------------------------------------------------------- diff --git a/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorDecorationTest.java b/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorDecorationTest.java index 266ade2..dfe34ff 100644 --- a/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorDecorationTest.java +++ b/cayenne-di/src/test/java/org/apache/cayenne/di/spi/DefaultInjectorDecorationTest.java @@ -25,6 +25,8 @@ import org.apache.cayenne.di.mock.MockInterface1; import org.apache.cayenne.di.mock.MockInterface1_Decorator1; import org.apache.cayenne.di.mock.MockInterface1_Decorator2; import org.apache.cayenne.di.mock.MockInterface1_Decorator3; +import org.apache.cayenne.di.mock.MockInterface1_Decorator4; +import org.apache.cayenne.di.mock.MockInterface1_Decorator5; import org.junit.Test; import static org.junit.Assert.assertEquals; @@ -92,4 +94,41 @@ public class DefaultInjectorDecorationTest { assertEquals("<[{MyName}]>", service.getName()); } + @Test + public void testSingleDecorator_Provider_ConstructorInjection() { + + Module module = new Module() { + + @Override + public void configure(Binder binder) { + binder.bind(MockInterface1.class).to(MockImplementation1.class); + binder.decorate(MockInterface1.class).before(MockInterface1_Decorator4.class); + } + }; + + DefaultInjector injector = new DefaultInjector(module); + + MockInterface1 service = injector.getInstance(MockInterface1.class); + assertNotNull(service); + assertEquals("[4MyName4]", service.getName()); + } + + @Test + public void testSingleDecorator_Provider_FieldInjection() { + + Module module = new Module() { + + @Override + public void configure(Binder binder) { + binder.bind(MockInterface1.class).to(MockImplementation1.class); + binder.decorate(MockInterface1.class).before(MockInterface1_Decorator5.class); + } + }; + + DefaultInjector injector = new DefaultInjector(module); + + MockInterface1 service = injector.getInstance(MockInterface1.class); + assertNotNull(service); + assertEquals("[5MyName5]", service.getName()); + } }