Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.tectonica.guice;
 
 
 
public class PostConstructTypeListener implements TypeListener
	private final String packagePrefix;
	public PostConstructTypeListener(String packagePrefix)
	{
		this. = packagePrefix;
	}
	public <I> void hear(final TypeLiteral<I> typeLiteralTypeEncounter<I> typeEncounter)
	{
		Class<? super I> clz = typeLiteral.getRawType();
		if ( != null && !clz.getName().startsWith())
			return;
		final Method method = getPostConstructMethod(clz);
		if (method != null)
		{
			typeEncounter.register(new InjectionListener<I>()
			{
				public void afterInjection(Object i)
				{
					try
					{
						// call the @PostConstruct annotated method after all dependencies have been injected
						method.invoke(i);
					}
					{
						throw new RuntimeException(e);
					}
				}
			});
		}
	}

checks whether the provided class, or one of its super-classes, has a method with the PostConstruct annotation. the method may be public, protected, package-private or private. if it's inaccessible by Java rules, this method will also make it accessible before returning it.

Returns:
the method that meets all requirements, or null if none found
	{
		for (Method method : clz.getDeclaredMethods())
		{
			if (method.getAnnotation(PostConstruct.class) != null && isPostConstructEligible(method))
			{
				method.setAccessible(true);
				return method;
			}
		}
		Class<?> superClz = clz.getSuperclass();
		return (superClz == Object.class || superClz == null) ? null : getPostConstructMethod(superClz);
	}

apply restrictions as defined in the JavaEE specifications
	private boolean isPostConstructEligible(final Method method)
	{
		return (method.getReturnType() == void.class) && (method.getParameterTypes().length == 0)
				&& (method.getExceptionTypes().length == 0);
	}
New to GrepCode? Check out our FAQ X