From 1116e99aaf2a97833d0d90281b8830f3eea64fa8 Mon Sep 17 00:00:00 2001 From: Dominik Przybysz Date: Wed, 6 Apr 2016 18:32:30 +0200 Subject: [PATCH] [ARIES-1521] Inject constructor parameters without @Inject when class has only one constructor --- .../aries/blueprint/plugin/model/Bean.java | 9 +++--- .../aries/blueprint/plugin/GeneratorTest.java | 7 ++++ .../aries/blueprint/plugin/test/MyBean5.java | 7 ++-- .../aries/blueprint/plugin/test/MyBean6.java | 32 +++++++++++++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean6.java diff --git a/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java b/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java index b382b3187f..27cc38fb74 100644 --- a/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java +++ b/blueprint/blueprint-maven-plugin/src/main/java/org/apache/aries/blueprint/plugin/model/Bean.java @@ -6,9 +6,9 @@ * 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 @@ -90,10 +90,11 @@ public void resolve(Matcher matcher) { } private void resolveConstructorArguments(Matcher matcher) { - for (Constructor constructor : clazz.getDeclaredConstructors()) { + Constructor[] declaredConstructors = clazz.getDeclaredConstructors(); + for (Constructor constructor : declaredConstructors) { Annotation inject = constructor.getAnnotation(Inject.class); Annotation autowired = constructor.getAnnotation(Autowired.class); - if (inject != null || autowired != null) { + if (inject != null || autowired != null || declaredConstructors.length == 1) { Class[] parameterTypes = constructor.getParameterTypes(); Annotation[][] parameterAnnotations = constructor.getParameterAnnotations(); for (int i = 0; i < parameterTypes.length; ++i) { diff --git a/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java b/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java index 3182e7ed25..d9891ca4ea 100644 --- a/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java +++ b/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/GeneratorTest.java @@ -171,6 +171,13 @@ public void testGenerateBeanWithConstructorInjection() throws Exception { assertEquals("serviceAImplQualified", xpath.evaluate("argument[7]/@ref", myBean5)); } + @Test + public void testGenerateBeanWithConstructorInjectionWithoutInjectAnnotation() throws Exception { + // Bean with constructor injection + Node myBean6 = getBeanById("myBean6"); + assertEquals("my2", xpath.evaluate("argument[1]/@ref", myBean6)); + } + @Test public void testGenerateReferenceWithComponentName() throws Exception { Node ser1 = getReferenceById("ser1"); diff --git a/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java b/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java index e4d3c7070d..be14c46b65 100644 --- a/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java +++ b/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean5.java @@ -6,9 +6,9 @@ * 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 @@ -59,4 +59,7 @@ public MyBean5(@Named("my2") ServiceA serviceA1, this.myReference2 = myReference2; this.serviceAAnnotated = serviceAAnnotated; } + + public MyBean5() { + } } diff --git a/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean6.java b/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean6.java new file mode 100644 index 0000000000..fdfec2e778 --- /dev/null +++ b/blueprint/blueprint-maven-plugin/src/test/java/org/apache/aries/blueprint/plugin/test/MyBean6.java @@ -0,0 +1,32 @@ +/** + * 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.plugin.test; + +import javax.inject.Named; +import javax.inject.Singleton; + +@Singleton +public class MyBean6 { + + private final ServiceA serviceA1; + + public MyBean6(@Named("my2") ServiceA serviceA1) { + this.serviceA1 = serviceA1; + } +}