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

Sisu specific PreConstruct/PreDestroy annotations #76

Merged
merged 4 commits into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*******************************************************************************
* Copyright (c) 2010-present Sonatype, Inc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sonatype copyright?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

* 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
*
* Contributors:
* Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn’t that be you @cstamas?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

*******************************************************************************/
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,39 @@
/*******************************************************************************
* Copyright (c) 2010-present Sonatype, Inc.
* 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
*
* Contributors:
* Stuart McCulloch (Sonatype, Inc.) - initial API and implementation
*******************************************************************************/
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 hasJsr250Lifecycle;
cstamas marked this conversation as resolved.
Show resolved Hide resolved
try
{
hasJsr250Lifecycle = javax.annotation.PostConstruct.class.isAnnotation()
&& javax.annotation.PreDestroy.class.isAnnotation();
}
catch ( final LinkageError e )
{
hasJsr250Lifecycle = false;
}
HAS_JSR250_LIFECYCLE = hasJsr250Lifecycle;
}

private static final boolean HAS_JSR250_LIFECYCLE;

// ----------------------------------------------------------------------
// 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_LIFECYCLE )
{
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,8 @@
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 JSR250 lifecycle management by following {@link javax.annotation.PostConstruct} and
* {@link javax.annotation.PreDestroy} annotations. The lifecycle can be controlled with the associated {@link BeanManager}.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or sisu lifecycle annotations!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed, please reread

*/
public final class LifecycleModule
implements Module
Expand Down