Skip to content

Commit

Permalink
Sisu specific PreConstruct/PreDestroy annotations (#76)
Browse files Browse the repository at this point in the history
Just like other annotations like Typed, Priority add
"own" annotations for Lifecycle as well (while do support
JSR250 if present).

This closes #58
  • Loading branch information
cstamas authored Feb 27, 2023
1 parent a2118f1 commit bfbca8a
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/********************************************************************************
* Copyright (c) 2023-present Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* SPDX-License-Identifier: EPL-1.0
********************************************************************************/
package org.eclipse.sisu;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* The PostConstruct annotation is used on a method that needs to be executed
* after dependency injection is done to perform any initialization. This
* method is invoked by Sisu before the class is put into service.
* <p>
* This annotation is Sisu specific annotation, that has same semantics as
* {@link javax.annotation.PostConstruct} annotation has, and may be used
* interchangeably.
* <p>
* To use annotation {@link org.eclipse.sisu.bean.LifecycleModule} needs to be
* installed.
*
* @since TBD
*/
@Target( value = { ElementType.METHOD } )
@Retention( RetentionPolicy.RUNTIME )
@Documented
public @interface PostConstruct {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/********************************************************************************
* Copyright (c) 2023-present Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* SPDX-License-Identifier: EPL-1.0
********************************************************************************/
package org.eclipse.sisu;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* The PreDestroy annotation is used on methods as a callback notification to
* signal that the instance is in the process of being removed by the
* container. The method annotated with PreDestroy is typically used to
* release resources that it has been holding.
* <p>
* This annotation is Sisu specific annotation, that has same semantics as
* {@link javax.annotation.PreDestroy} annotation has, and may be used
* interchangeably.
* <p>
* To use annotation {@link org.eclipse.sisu.bean.LifecycleModule} needs to be
* installed.
*
* @since TBD
*/

@Target( value = { ElementType.METHOD } )
@Retention( RetentionPolicy.RUNTIME )
@Documented
public @interface PreDestroy {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,37 @@
*******************************************************************************/
package org.eclipse.sisu.bean;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import org.eclipse.sisu.PostConstruct;
import org.eclipse.sisu.PreDestroy;

/**
* Builds {@link BeanLifecycle}s by searching class hierarchies for JSR250 annotations.
*/
final class LifecycleBuilder
{
static
{
boolean hasJsr250Annotations;
try
{
hasJsr250Annotations = javax.annotation.PostConstruct.class.isAnnotation()
&& javax.annotation.PreDestroy.class.isAnnotation();
}
catch ( final LinkageError e )
{
hasJsr250Annotations = false;
}
HAS_JSR250_ANNOTATIONS = hasJsr250Annotations;
}

private static final boolean HAS_JSR250_ANNOTATIONS;

// ----------------------------------------------------------------------
// Implementation fields
// ----------------------------------------------------------------------
Expand Down Expand Up @@ -83,15 +101,15 @@ private void addLifecycleMethods( final Class<?> clazz )
{
if ( isCandidateMethod( m ) )
{
if ( m.isAnnotationPresent( PostConstruct.class ) )
if ( isAnnotationPresent( m, PostConstruct.class ) )
{
foundStartMethod = true;
if ( !isOverridden( m ) )
{
startMethods.add( m );
}
}
else if ( m.isAnnotationPresent( PreDestroy.class ) )
else if ( isAnnotationPresent( m, PreDestroy.class ) )
{
foundStopMethod = true;
if ( !isOverridden( m ) )
Expand All @@ -108,6 +126,23 @@ else if ( m.isAnnotationPresent( PreDestroy.class ) )
hierarchy.add( clazz );
}

private boolean isAnnotationPresent( final Method method, final Class<? extends Annotation> annotationClass )
{
boolean result = method.isAnnotationPresent( annotationClass );
if ( !result && HAS_JSR250_ANNOTATIONS )
{
if ( PostConstruct.class.equals( annotationClass ) )
{
result = method.isAnnotationPresent( javax.annotation.PostConstruct.class );
}
else if ( PreDestroy.class.equals( annotationClass ) )
{
result = method.isAnnotationPresent( javax.annotation.PreDestroy.class );
}
}
return result;
}

/**
* Tests to see if the given method is overridden in the subclass hierarchy.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
*******************************************************************************/
package org.eclipse.sisu.bean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.google.inject.TypeLiteral;
Expand All @@ -23,8 +20,10 @@
import com.google.inject.spi.TypeListener;

/**
* Guice {@link Module} that provides JSR250 lifecycle management by following {@link PostConstruct} and
* {@link PreDestroy} annotations. The lifecycle can be controlled with the associated {@link BeanManager}.
* Guice {@link Module} that provides lifecycle management by following {@link org.eclipse.sisu.PostConstruct}
* and {@link org.eclipse.sisu.PreDestroy} annotations, or corresponding JSR250 {@link javax.annotation.PostConstruct}
* and {@link javax.annotation.PreDestroy} annotations. The lifecycle can be controlled with the associated
* {@link BeanManager}.
*/
public final class LifecycleModule
implements Module
Expand Down

0 comments on commit bfbca8a

Please sign in to comment.