Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ARIES-1474] Fix init/destroy method inheritence. #33

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -34,6 +34,9 @@

import org.springframework.stereotype.Component;

import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;

public class Bean extends BeanRef {
public String initMethod;
public String destroyMethod;
Expand All @@ -45,15 +48,16 @@ public class Bean extends BeanRef {
public Bean(Class<?> clazz) {
super(clazz, BeanRef.getBeanName(clazz));

for (Method method : clazz.getDeclaredMethods()) {
PostConstruct postConstruct = getEffectiveAnnotation(method, PostConstruct.class);
if (postConstruct != null) {
this.initMethod = method.getName();
}
PreDestroy preDestroy = getEffectiveAnnotation(method, PreDestroy.class);
if (preDestroy != null) {
this.destroyMethod = method.getName();
}
// Init method
Method initMethod = getMethodWithAnnotation(clazz, PostConstruct.class);
if (initMethod != null) {
this.initMethod = initMethod.getName();
}

// Destroy method
Method destroyMethod = getMethodWithAnnotation(clazz, PreDestroy.class);
if (destroyMethod != null) {
this.destroyMethod = destroyMethod.getName();
}
this.isPrototype = isPrototype(clazz);
this.persistenceFields = getPersistenceFields();
Expand Down Expand Up @@ -81,15 +85,15 @@ private Field[] getPersistenceFields() {
}
return persistenceFields.toArray(new Field[]{});
}

public void resolve(Matcher matcher) {
Class<?> curClass = this.clazz;
while (curClass != null && curClass != Object.class) {
resolveProperties(matcher, curClass);
curClass = curClass.getSuperclass();
}
}

private void resolveProperties(Matcher matcher, Class<?> curClass) {
for (Field field : curClass.getDeclaredFields()) {
Property prop = Property.create(matcher, field);
Expand All @@ -99,46 +103,27 @@ private void resolveProperties(Matcher matcher, Class<?> curClass) {
}
}

private static <T extends Annotation> T getEffectiveAnnotation(Method method, Class<T> annotationClass) {
final Class<?> methodClass = method.getDeclaringClass();
final String name = method.getName();
final Class<?>[] params = method.getParameterTypes();

// 1. Current class
final T rootAnnotation = method.getAnnotation(annotationClass);
if (rootAnnotation != null) {
return rootAnnotation;
}

// 2. Superclass
final Class<?> superclass = methodClass.getSuperclass();
if (superclass != null) {
final T annotation = getMethodAnnotation(superclass, name, params, annotationClass);
if (annotation != null)
return annotation;
}

// 3. Interfaces
for (final Class<?> intfs : methodClass.getInterfaces()) {
final T annotation = getMethodAnnotation(intfs, name, params, annotationClass);
if (annotation != null)
return annotation;
}

return null;
private static <T extends Annotation> Method getMethodWithAnnotation(Class<?> classToSearch,
Class<T> annotationClass) {
List<Method> methods = getMethodsWithAnnotation(classToSearch, annotationClass);
Preconditions.checkArgument(methods.size() <= 1,
"Found %d methods annotated with %s in class %s, but only 1 allowed",
methods.size(), annotationClass.getName(), classToSearch.getName());
return Iterables.getOnlyElement(methods, null);
}

private static <T extends Annotation> T getMethodAnnotation(Class<?> searchClass, String name, Class<?>[] params,
Class<T> annotationClass) {
try {
Method method = searchClass.getMethod(name, params);
return getEffectiveAnnotation(method, annotationClass);
} catch (NoSuchMethodException e) {
return null;
private static <T extends Annotation> List<Method> getMethodsWithAnnotation(Class<?> classToSearch,
Class<T> annotationClass) {
List<Method> methods = new ArrayList<>();
for (Method method : classToSearch.getMethods()) {
T annotation = method.getAnnotation(annotationClass);
if (annotation != null) {
methods.add(method);
}
}
return methods;
}


@Override
public int hashCode() {
final int prime = 31;
Expand All @@ -158,5 +143,5 @@ public void writeProperties(PropertyWriter writer) {
writer.writeProperty(property);
}
}

}
@@ -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.aries.blueprint.plugin.bad;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;

import org.apache.aries.blueprint.plugin.test.ParentBean;

@Singleton
public class BadBean1 extends ParentBean
{
@PostConstruct
public void secondInit() {

}
}
@@ -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.aries.blueprint.plugin.bad;

import javax.annotation.PreDestroy;
import javax.inject.Singleton;

import org.apache.aries.blueprint.plugin.test.ParentBean;

@Singleton
public class BadBean2 extends ParentBean
{
@PreDestroy
public void secondDestroy() {

}
}
Expand Up @@ -25,6 +25,8 @@

import javax.inject.Named;

import org.apache.aries.blueprint.plugin.bad.BadBean1;
import org.apache.aries.blueprint.plugin.bad.BadBean2;
import org.apache.aries.blueprint.plugin.test.MyBean1;
import org.apache.aries.blueprint.plugin.test.MyBean3;
import org.apache.aries.blueprint.plugin.test.MyBean4;
Expand All @@ -34,7 +36,7 @@


public class BeanTest {

@Test
public void testParseMyBean1() {
Bean bean = new Bean(MyBean1.class);
Expand All @@ -54,7 +56,7 @@ public void testParseMyBean1() {
assertEquals("bean2", prop.name);
assertEquals("serviceA", prop.ref);
}

@Test
public void testParseMyBean3() {
Bean bean = new Bean(MyBean3.class);
Expand All @@ -69,7 +71,7 @@ public void testParseMyBean3() {
assertEquals(5, bean.properties.size());
assertTrue(bean.isPrototype);
}

@Test
public void testParseNamedBean() {
Bean bean = new Bean(ServiceAImpl1.class);
Expand All @@ -84,7 +86,7 @@ public void testParseNamedBean() {
assertEquals("There should be no properties", 0, bean.properties.size());
assertTrue(bean.isPrototype);
}

@Test
public void testBlueprintBundleContext() {
Bean bean = new Bean(MyBean4.class);
Expand All @@ -95,4 +97,13 @@ public void testBlueprintBundleContext() {
assertFalse(bean.isPrototype);
}

@Test(expected = IllegalArgumentException.class)
public void testMultipleInitMethods() {
new Bean(BadBean1.class);
}

@Test(expected = IllegalArgumentException.class)
public void testMultipleDestroyMethods() {
new Bean(BadBean2.class);
}
}
Expand Up @@ -18,6 +18,7 @@
*/
package org.apache.aries.blueprint.plugin.test;

import javax.annotation.PostConstruct;
import javax.inject.Singleton;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
Expand All @@ -33,20 +34,23 @@ public class MyBean1 extends ParentBean {

@Autowired
ServiceA bean2;

@PersistenceContext(unitName="person")
EntityManager em;

@PersistenceUnit(unitName="person")
EntityManager emf;

public void init() {

public void overridenInit() {
// By overriding the method and removing the annotation, this method has lost its
// @PostConstruct method because it isn't @Inherited
}

public void destroy() {

@PostConstruct
public void init() {
}

public void saveData() {

}
}
Expand Up @@ -24,9 +24,9 @@
public class ParentBean {

@PostConstruct
public void init() {
public void overridenInit() {
}

@PreDestroy
public void destroy() {
}
Expand Down