417561 Refactor annotation related code to remove some deadwood

This commit is contained in:
Jan Bartel 2013-09-19 15:42:42 +10:00
parent aa5705fd78
commit d4c035a06f
17 changed files with 277 additions and 180 deletions

View File

@ -18,24 +18,22 @@
package org.eclipse.jetty.annotations; package org.eclipse.jetty.annotations;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.eclipse.jetty.annotations.AnnotationParser.AbstractHandler; import org.eclipse.jetty.annotations.AnnotationParser.AbstractHandler;
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.webapp.DiscoveredAnnotation; import org.eclipse.jetty.webapp.DiscoveredAnnotation;
import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.webapp.WebAppContext;
/** /**
* DiscoverableAnnotationHandler * DiscoverableAnnotationHandler
* *
* * Base class for handling the discovery of an annotation.
*
*/ */
public abstract class AbstractDiscoverableAnnotationHandler extends AbstractHandler public abstract class AbstractDiscoverableAnnotationHandler extends AbstractHandler
{ {
protected WebAppContext _context; protected WebAppContext _context;
protected List<DiscoveredAnnotation> _annotations;
protected Resource _resource;
public AbstractDiscoverableAnnotationHandler(WebAppContext context) public AbstractDiscoverableAnnotationHandler(WebAppContext context)
{ {
@ -45,35 +43,13 @@ public abstract class AbstractDiscoverableAnnotationHandler extends AbstractHand
public AbstractDiscoverableAnnotationHandler(WebAppContext context, List<DiscoveredAnnotation> list) public AbstractDiscoverableAnnotationHandler(WebAppContext context, List<DiscoveredAnnotation> list)
{ {
_context = context; _context = context;
if (list == null)
_annotations = new ArrayList<DiscoveredAnnotation>();
else
_annotations = list;
} }
public Resource getResource()
{
return _resource;
}
public void setResource(Resource resource)
{
_resource = resource;
}
public List<DiscoveredAnnotation> getAnnotationList ()
{
return _annotations;
}
public void resetList()
{
_annotations.clear();
}
public void addAnnotation (DiscoveredAnnotation a) public void addAnnotation (DiscoveredAnnotation a)
{ {
_annotations.add(a); _context.getMetaData().addDiscoveredAnnotation(a);
} }
} }

View File

@ -197,10 +197,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
// In case there is no others then it is // In case there is no others then it is
// WEB-INF/classes + order of the elements. // WEB-INF/classes + order of the elements.
parseWebInfClasses(context, parser); parseWebInfClasses(context, parser);
parseWebInfLib (context, parser); parseWebInfLib (context, parser);
for (AbstractDiscoverableAnnotationHandler h:_discoverableAnnotationHandlers)
context.getMetaData().addDiscoveredAnnotations(((AbstractDiscoverableAnnotationHandler)h).getAnnotationList());
} }
} }
@ -232,7 +229,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
} }
/** /**
* @return a new AnnotationParser. This method can be overridden to use a different impleemntation of * @return a new AnnotationParser. This method can be overridden to use a different implementation of
* the AnnotationParser. Note that this is considered internal API. * the AnnotationParser. Note that this is considered internal API.
*/ */
protected AnnotationParser createAnnotationParser() protected AnnotationParser createAnnotationParser()
@ -259,12 +256,8 @@ public class AnnotationConfiguration extends AbstractConfiguration
public void createServletContainerInitializerAnnotationHandlers (WebAppContext context, List<ServletContainerInitializer> scis) public void createServletContainerInitializerAnnotationHandlers (WebAppContext context, List<ServletContainerInitializer> scis)
throws Exception throws Exception
{ {
if (scis == null || scis.isEmpty()) if (scis == null || scis.isEmpty())
return; // nothing to do return; // nothing to do
List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>(); List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
context.setAttribute(CONTAINER_INITIALIZERS, initializers); context.setAttribute(CONTAINER_INITIALIZERS, initializers);
@ -300,7 +293,6 @@ public class AnnotationConfiguration extends AbstractConfiguration
if (c.isAnnotation()) if (c.isAnnotation())
{ {
if (LOG.isDebugEnabled()) LOG.debug("Registering annotation handler for "+c.getName()); if (LOG.isDebugEnabled()) LOG.debug("Registering annotation handler for "+c.getName());
_containerInitializerAnnotationHandlers.add(new ContainerInitializerAnnotationHandler(initializer, c)); _containerInitializerAnnotationHandlers.add(new ContainerInitializerAnnotationHandler(initializer, c));
} }
} }
@ -410,11 +402,6 @@ public class AnnotationConfiguration extends AbstractConfiguration
//always parse for discoverable annotations as well as class hierarchy and servletcontainerinitializer related annotations //always parse for discoverable annotations as well as class hierarchy and servletcontainerinitializer related annotations
parser.clearHandlers(); parser.clearHandlers();
for (AbstractDiscoverableAnnotationHandler h:_discoverableAnnotationHandlers)
{
if (h instanceof AbstractDiscoverableAnnotationHandler)
((AbstractDiscoverableAnnotationHandler)h).setResource(null); //
}
parser.registerHandlers(_discoverableAnnotationHandlers); parser.registerHandlers(_discoverableAnnotationHandlers);
parser.registerHandler(_classInheritanceHandler); parser.registerHandler(_classInheritanceHandler);
parser.registerHandlers(_containerInitializerAnnotationHandlers); parser.registerHandlers(_containerInitializerAnnotationHandlers);
@ -477,14 +464,7 @@ public class AnnotationConfiguration extends AbstractConfiguration
//only register the discoverable annotation handlers if this fragment is not metadata complete, or has no fragment descriptor //only register the discoverable annotation handlers if this fragment is not metadata complete, or has no fragment descriptor
if (f == null || !isMetaDataComplete(f)) if (f == null || !isMetaDataComplete(f))
{
for (AbstractDiscoverableAnnotationHandler h:_discoverableAnnotationHandlers)
{
if (h instanceof AbstractDiscoverableAnnotationHandler)
((AbstractDiscoverableAnnotationHandler)h).setResource(r);
}
parser.registerHandlers(_discoverableAnnotationHandlers); parser.registerHandlers(_discoverableAnnotationHandlers);
}
parser.parse(uri, new WebAppClassNameResolver(context)); parser.parse(uri, new WebAppClassNameResolver(context));
} }
@ -504,11 +484,6 @@ public class AnnotationConfiguration extends AbstractConfiguration
LOG.debug("Scanning classes in WEB-INF/classes"); LOG.debug("Scanning classes in WEB-INF/classes");
parser.clearHandlers(); parser.clearHandlers();
for (AbstractDiscoverableAnnotationHandler h:_discoverableAnnotationHandlers)
{
if (h instanceof AbstractDiscoverableAnnotationHandler)
((AbstractDiscoverableAnnotationHandler)h).setResource(null); //
}
parser.registerHandlers(_discoverableAnnotationHandlers); parser.registerHandlers(_discoverableAnnotationHandlers);
parser.registerHandler(_classInheritanceHandler); parser.registerHandler(_classInheritanceHandler);
parser.registerHandlers(_containerInitializerAnnotationHandlers); parser.registerHandlers(_containerInitializerAnnotationHandlers);
@ -550,6 +525,4 @@ public class AnnotationConfiguration extends AbstractConfiguration
{ {
return (d!=null && d.getMetaDataComplete() == MetaDataComplete.True); return (d!=null && d.getMetaDataComplete() == MetaDataComplete.True);
} }
} }

View File

@ -116,6 +116,7 @@ public class AnnotationParser
*/ */
public class ClassInfo public class ClassInfo
{ {
final Resource _containingResource;
final String _className; final String _className;
final int _version; final int _version;
final int _access; final int _access;
@ -123,9 +124,10 @@ public class AnnotationParser
final String _superName; final String _superName;
final String[] _interfaces; final String[] _interfaces;
public ClassInfo(String className, int version, int access, String signature, String superName, String[] interfaces) public ClassInfo(Resource resource, String className, int version, int access, String signature, String superName, String[] interfaces)
{ {
super(); super();
_containingResource = resource;
_className = className; _className = className;
_version = version; _version = version;
_access = access; _access = access;
@ -163,6 +165,11 @@ public class AnnotationParser
{ {
return _interfaces; return _interfaces;
} }
public Resource getContainingResource()
{
return _containingResource;
}
} }
@ -173,17 +180,17 @@ public class AnnotationParser
*/ */
public class MethodInfo public class MethodInfo
{ {
final String _className; final ClassInfo _classInfo;
final String _methodName; final String _methodName;
final int _access; final int _access;
final String _desc; final String _desc;
final String _signature; final String _signature;
final String[] _exceptions; final String[] _exceptions;
public MethodInfo(String className, String methodName, int access, String desc, String signature, String[] exceptions) public MethodInfo(ClassInfo classInfo, String methodName, int access, String desc, String signature, String[] exceptions)
{ {
super(); super();
_className = className; _classInfo = classInfo;
_methodName = methodName; _methodName = methodName;
_access = access; _access = access;
_desc = desc; _desc = desc;
@ -191,9 +198,9 @@ public class AnnotationParser
_exceptions = exceptions; _exceptions = exceptions;
} }
public String getClassName() public ClassInfo getClassInfo()
{ {
return _className; return _classInfo;
} }
public String getMethodName() public String getMethodName()
@ -219,7 +226,7 @@ public class AnnotationParser
public String[] getExceptions() public String[] getExceptions()
{ {
return _exceptions; return _exceptions;
} }
} }
@ -232,17 +239,17 @@ public class AnnotationParser
*/ */
public class FieldInfo public class FieldInfo
{ {
final String _className; final ClassInfo _classInfo;
final String _fieldName; final String _fieldName;
final int _access; final int _access;
final String _fieldType; final String _fieldType;
final String _signature; final String _signature;
final Object _value; final Object _value;
public FieldInfo(String className, String fieldName, int access, String fieldType, String signature, Object value) public FieldInfo(ClassInfo classInfo, String fieldName, int access, String fieldType, String signature, Object value)
{ {
super(); super();
_className = className; _classInfo = classInfo;
_fieldName = fieldName; _fieldName = fieldName;
_access = access; _access = access;
_fieldType = fieldType; _fieldType = fieldType;
@ -250,9 +257,9 @@ public class AnnotationParser
_value = value; _value = value;
} }
public String getClassName() public ClassInfo getClassInfo()
{ {
return _className; return _classInfo;
} }
public String getFieldName() public String getFieldName()
@ -355,6 +362,7 @@ public class AnnotationParser
public class MyMethodVisitor extends MethodVisitor public class MyMethodVisitor extends MethodVisitor
{ {
final MethodInfo _mi; final MethodInfo _mi;
/** /**
* @param classname * @param classname
@ -364,7 +372,8 @@ public class AnnotationParser
* @param signature * @param signature
* @param exceptions * @param exceptions
*/ */
public MyMethodVisitor(final String className, public MyMethodVisitor(
final ClassInfo classInfo,
final int access, final int access,
final String name, final String name,
final String methodDesc, final String methodDesc,
@ -372,7 +381,7 @@ public class AnnotationParser
final String[] exceptions) final String[] exceptions)
{ {
super(Opcodes.ASM4); super(Opcodes.ASM4);
_mi = new MethodInfo(className, name, access, methodDesc,signature, exceptions); _mi = new MethodInfo(classInfo, name, access, methodDesc,signature, exceptions);
} }
@ -408,7 +417,7 @@ public class AnnotationParser
/** /**
* @param classname * @param classname
*/ */
public MyFieldVisitor(final String className, public MyFieldVisitor(final ClassInfo classInfo,
final int access, final int access,
final String fieldName, final String fieldName,
final String fieldType, final String fieldType,
@ -416,7 +425,7 @@ public class AnnotationParser
final Object value) final Object value)
{ {
super(Opcodes.ASM4); super(Opcodes.ASM4);
_fieldInfo = new FieldInfo(className, fieldName, access, fieldType, signature, value); _fieldInfo = new FieldInfo(classInfo, fieldName, access, fieldType, signature, value);
} }
@ -447,11 +456,13 @@ public class AnnotationParser
public class MyClassVisitor extends ClassVisitor public class MyClassVisitor extends ClassVisitor
{ {
final Resource _containingResource;
ClassInfo _ci; ClassInfo _ci;
public MyClassVisitor() public MyClassVisitor(Resource containingResource)
{ {
super(Opcodes.ASM4); super(Opcodes.ASM4);
_containingResource = containingResource;
} }
@ -463,7 +474,7 @@ public class AnnotationParser
final String superName, final String superName,
final String[] interfaces) final String[] interfaces)
{ {
_ci = new ClassInfo(normalize(name), version, access, signature, normalize(superName), normalize(interfaces)); _ci = new ClassInfo(_containingResource, normalize(name), version, access, signature, normalize(superName), normalize(interfaces));
_parsedClassNames.add(_ci.getClassName()); _parsedClassNames.add(_ci.getClassName());
@ -501,7 +512,7 @@ public class AnnotationParser
final String[] exceptions) final String[] exceptions)
{ {
return new MyMethodVisitor(_ci.getClassName(), access, name, methodDesc, signature, exceptions); return new MyMethodVisitor(_ci, access, name, methodDesc, signature, exceptions);
} }
/** /**
@ -516,7 +527,7 @@ public class AnnotationParser
final String signature, final String signature,
final Object value) final Object value)
{ {
return new MyFieldVisitor(_ci.getClassName(), access, fieldName, fieldType, signature, value); return new MyFieldVisitor(_ci, access, fieldName, fieldType, signature, value);
} }
} }
@ -601,7 +612,7 @@ public class AnnotationParser
if (resource!= null) if (resource!= null)
{ {
Resource r = Resource.newResource(resource); Resource r = Resource.newResource(resource);
scanClass(r.getInputStream()); scanClass(null, r.getInputStream());
} }
} }
} }
@ -632,7 +643,7 @@ public class AnnotationParser
if (resource!= null) if (resource!= null)
{ {
Resource r = Resource.newResource(resource); Resource r = Resource.newResource(resource);
scanClass(r.getInputStream()); scanClass(null, r.getInputStream());
} }
} }
} }
@ -681,7 +692,7 @@ public class AnnotationParser
if (resource!= null) if (resource!= null)
{ {
Resource r = Resource.newResource(resource); Resource r = Resource.newResource(resource);
scanClass(r.getInputStream()); scanClass(null, r.getInputStream());
} }
} }
} }
@ -695,7 +706,7 @@ public class AnnotationParser
* @param resolver * @param resolver
* @throws Exception * @throws Exception
*/ */
public void parseDir (Resource dir, ClassNameResolver resolver) protected void parseDir (Resource dir, ClassNameResolver resolver)
throws Exception throws Exception
{ {
//skip dirs whose name start with . (ie hidden) //skip dirs whose name start with . (ie hidden)
@ -723,7 +734,7 @@ public class AnnotationParser
{ {
Resource r = Resource.newResource(res.getURL()); Resource r = Resource.newResource(res.getURL());
if (LOG.isDebugEnabled()) {LOG.debug("Scanning class {}", r);}; if (LOG.isDebugEnabled()) {LOG.debug("Scanning class {}", r);};
scanClass(r.getInputStream()); scanClass(dir, r.getInputStream());
} }
} }
@ -763,7 +774,7 @@ public class AnnotationParser
{ {
try try
{ {
parseJarEntry(jarUri, entry, resolver); parseJarEntry(Resource.newResource(jarUri), entry, resolver);
} }
catch (Exception e) catch (Exception e)
{ {
@ -849,13 +860,15 @@ public class AnnotationParser
if (fullname.endsWith(".class")) if (fullname.endsWith(".class"))
{ {
scanClass(r.getInputStream()); scanClass(null, r.getInputStream());
return; return;
} }
if (LOG.isDebugEnabled()) LOG.warn("Resource not scannable for classes: {}", r); if (LOG.isDebugEnabled()) LOG.warn("Resource not scannable for classes: {}", r);
} }
/** /**
* Parse a resource that is a jar file. * Parse a resource that is a jar file.
@ -864,13 +877,13 @@ public class AnnotationParser
* @param resolver * @param resolver
* @throws Exception * @throws Exception
*/ */
public void parseJar (Resource jarResource, final ClassNameResolver resolver) protected void parseJar (Resource jarResource, final ClassNameResolver resolver)
throws Exception throws Exception
{ {
if (jarResource == null) if (jarResource == null)
return; return;
URI uri = jarResource.getURI();
if (jarResource.toString().endsWith(".jar")) if (jarResource.toString().endsWith(".jar"))
{ {
if (LOG.isDebugEnabled()) {LOG.debug("Scanning jar {}", jarResource);}; if (LOG.isDebugEnabled()) {LOG.debug("Scanning jar {}", jarResource);};
@ -879,14 +892,14 @@ public class AnnotationParser
InputStream in = jarResource.getInputStream(); InputStream in = jarResource.getInputStream();
if (in==null) if (in==null)
return; return;
JarInputStream jar_in = new JarInputStream(in); JarInputStream jar_in = new JarInputStream(in);
try try
{ {
JarEntry entry = jar_in.getNextJarEntry(); JarEntry entry = jar_in.getNextJarEntry();
while (entry!=null) while (entry!=null)
{ {
parseJarEntry(uri, entry, resolver); parseJarEntry(jarResource, entry, resolver);
entry = jar_in.getNextJarEntry(); entry = jar_in.getNextJarEntry();
} }
} }
@ -904,7 +917,7 @@ public class AnnotationParser
* @param resolver * @param resolver
* @throws Exception * @throws Exception
*/ */
protected void parseJarEntry (URI jar, JarEntry entry, final ClassNameResolver resolver) protected void parseJarEntry (Resource jar, JarEntry entry, final ClassNameResolver resolver)
throws Exception throws Exception
{ {
if (jar == null || entry == null) if (jar == null || entry == null)
@ -925,9 +938,9 @@ public class AnnotationParser
|| ||
(!resolver.isExcluded(shortName) && (!isParsed(shortName) || resolver.shouldOverride(shortName)))) (!resolver.isExcluded(shortName) && (!isParsed(shortName) || resolver.shouldOverride(shortName))))
{ {
Resource clazz = Resource.newResource("jar:"+jar+"!/"+name); Resource clazz = Resource.newResource("jar:"+jar.getURI()+"!/"+name);
if (LOG.isDebugEnabled()) {LOG.debug("Scanning class from jar {}", clazz);}; if (LOG.isDebugEnabled()) {LOG.debug("Scanning class from jar {}", clazz);};
scanClass(clazz.getInputStream()); scanClass(jar, clazz.getInputStream());
} }
} }
} }
@ -937,14 +950,15 @@ public class AnnotationParser
/** /**
* Use ASM on a class * Use ASM on a class
* *
* @param containingResource the dir or jar that the class is contained within, can be null if not known
* @param is * @param is
* @throws IOException * @throws IOException
*/ */
protected void scanClass (InputStream is) protected void scanClass (Resource containingResource, InputStream is)
throws IOException throws IOException
{ {
ClassReader reader = new ClassReader(is); ClassReader reader = new ClassReader(is);
reader.accept(new MyClassVisitor(), ClassReader.SKIP_CODE|ClassReader.SKIP_DEBUG|ClassReader.SKIP_FRAMES); reader.accept(new MyClassVisitor(containingResource), ClassReader.SKIP_CODE|ClassReader.SKIP_DEBUG|ClassReader.SKIP_FRAMES);
} }
/** /**

View File

@ -36,11 +36,7 @@ public class ClassInheritanceHandler extends AbstractHandler
private static final Logger LOG = Log.getLogger(ClassInheritanceHandler.class); private static final Logger LOG = Log.getLogger(ClassInheritanceHandler.class);
MultiMap<String> _inheritanceMap; MultiMap<String> _inheritanceMap;
public ClassInheritanceHandler()
{
_inheritanceMap = new MultiMap<>();
}
public ClassInheritanceHandler(MultiMap<String> map) public ClassInheritanceHandler(MultiMap<String> map)
{ {
@ -64,14 +60,4 @@ public class ClassInheritanceHandler extends AbstractHandler
LOG.warn(e); LOG.warn(e);
} }
} }
public List<String> getClassNamesExtendingOrImplementing (String className)
{
return _inheritanceMap.getValues(className);
}
public MultiMap<String> getMap ()
{
return _inheritanceMap;
}
} }

View File

@ -69,7 +69,7 @@ public class ContainerInitializerAnnotationHandler extends AbstractHandler
{ {
if (annotationName == null || !_annotation.getName().equals(annotationName)) if (annotationName == null || !_annotation.getName().equals(annotationName))
return; return;
_initializer.addAnnotatedTypeName(info.getClassName()); _initializer.addAnnotatedTypeName(info.getClassInfo().getClassName());
} }
/** /**
@ -81,7 +81,7 @@ public class ContainerInitializerAnnotationHandler extends AbstractHandler
{ {
if (annotationName == null || !_annotation.getName().equals(annotationName)) if (annotationName == null || !_annotation.getName().equals(annotationName))
return; return;
_initializer.addAnnotatedTypeName(info.getClassName()); _initializer.addAnnotatedTypeName(info.getClassInfo().getClassName());
} }

View File

@ -53,7 +53,7 @@ public class WebFilterAnnotationHandler extends AbstractDiscoverableAnnotationHa
if (annotationName == null || !"javax.servlet.annotation.WebFilter".equals(annotationName)) if (annotationName == null || !"javax.servlet.annotation.WebFilter".equals(annotationName))
return; return;
WebFilterAnnotation wfAnnotation = new WebFilterAnnotation(_context, info.getClassName(), _resource); WebFilterAnnotation wfAnnotation = new WebFilterAnnotation(_context, info.getClassName(), info.getContainingResource());
addAnnotation(wfAnnotation); addAnnotation(wfAnnotation);
} }
@ -62,7 +62,7 @@ public class WebFilterAnnotationHandler extends AbstractDiscoverableAnnotationHa
{ {
if (annotationName == null || !"javax.servlet.annotation.WebFilter".equals(annotationName)) if (annotationName == null || !"javax.servlet.annotation.WebFilter".equals(annotationName))
return; return;
LOG.warn ("@WebFilter not applicable for fields: "+info.getClassName()+"."+info.getFieldName()); LOG.warn ("@WebFilter not applicable for fields: "+info.getClassInfo().getClassName()+"."+info.getFieldName());
} }
@Override @Override
@ -70,6 +70,6 @@ public class WebFilterAnnotationHandler extends AbstractDiscoverableAnnotationHa
{ {
if (annotationName == null || !"javax.servlet.annotation.WebFilter".equals(annotationName)) if (annotationName == null || !"javax.servlet.annotation.WebFilter".equals(annotationName))
return; return;
LOG.warn ("@WebFilter not applicable for methods: "+info.getClassName()+"."+info.getMethodName()+" "+info.getSignature()); LOG.warn ("@WebFilter not applicable for methods: "+info.getClassInfo().getClassName()+"."+info.getMethodName()+" "+info.getSignature());
} }
} }

View File

@ -50,7 +50,7 @@ public class WebListenerAnnotationHandler extends AbstractDiscoverableAnnotation
if (annotationName == null || !"javax.servlet.annotation.WebListener".equals(annotationName)) if (annotationName == null || !"javax.servlet.annotation.WebListener".equals(annotationName))
return; return;
WebListenerAnnotation wlAnnotation = new WebListenerAnnotation(_context, info.getClassName(), _resource); WebListenerAnnotation wlAnnotation = new WebListenerAnnotation(_context, info.getClassName(), info.getContainingResource());
addAnnotation(wlAnnotation); addAnnotation(wlAnnotation);
} }
@ -58,13 +58,13 @@ public class WebListenerAnnotationHandler extends AbstractDiscoverableAnnotation
{ {
if (annotationName == null || !"javax.servlet.annotation.WebListener".equals(annotationName)) if (annotationName == null || !"javax.servlet.annotation.WebListener".equals(annotationName))
return; return;
LOG.warn ("@WebListener is not applicable to fields: "+info.getClassName()+"."+info.getFieldName()); LOG.warn ("@WebListener is not applicable to fields: "+info.getClassInfo().getClassName()+"."+info.getFieldName());
} }
public void handle(MethodInfo info, String annotationName) public void handle(MethodInfo info, String annotationName)
{ {
if (annotationName == null || !"javax.servlet.annotation.WebListener".equals(annotationName)) if (annotationName == null || !"javax.servlet.annotation.WebListener".equals(annotationName))
return; return;
LOG.warn ("@WebListener is not applicable to methods: "+info.getClassName()+"."+info.getMethodName()+" "+info.getSignature()); LOG.warn ("@WebListener is not applicable to methods: "+info.getClassInfo().getClassName()+"."+info.getMethodName()+" "+info.getSignature());
} }
} }

View File

@ -61,7 +61,7 @@ public class WebServletAnnotationHandler extends AbstractDiscoverableAnnotationH
if (annotationName == null || !"javax.servlet.annotation.WebServlet".equals(annotationName)) if (annotationName == null || !"javax.servlet.annotation.WebServlet".equals(annotationName))
return; return;
WebServletAnnotation annotation = new WebServletAnnotation (_context, info.getClassName(), _resource); WebServletAnnotation annotation = new WebServletAnnotation (_context, info.getClassName(), info.getContainingResource());
addAnnotation(annotation); addAnnotation(annotation);
} }

View File

@ -64,14 +64,14 @@ public class TestAnnotationInheritance
{ {
if (annotation == null || !"org.eclipse.jetty.annotations.Sample".equals(annotation)) if (annotation == null || !"org.eclipse.jetty.annotations.Sample".equals(annotation))
return; return;
annotatedFields.add(info.getClassName()+"."+info.getFieldName()); annotatedFields.add(info.getClassInfo().getClassName()+"."+info.getFieldName());
} }
public void handle(MethodInfo info, String annotation) public void handle(MethodInfo info, String annotation)
{ {
if (annotation == null || !"org.eclipse.jetty.annotations.Sample".equals(annotation)) if (annotation == null || !"org.eclipse.jetty.annotations.Sample".equals(annotation))
return; return;
annotatedMethods.add(info.getClassName()+"."+info.getMethodName()); annotatedMethods.add(info.getClassInfo().getClassName()+"."+info.getMethodName());
} }
} }
@ -205,8 +205,10 @@ public class TestAnnotationInheritance
@Test @Test
public void testTypeInheritanceHandling() throws Exception public void testTypeInheritanceHandling() throws Exception
{ {
MultiMap map = new MultiMap();
AnnotationParser parser = new AnnotationParser(); AnnotationParser parser = new AnnotationParser();
ClassInheritanceHandler handler = new ClassInheritanceHandler(); ClassInheritanceHandler handler = new ClassInheritanceHandler(map);
parser.registerHandler(handler); parser.registerHandler(handler);
class Foo implements InterfaceD class Foo implements InterfaceD
@ -221,7 +223,6 @@ public class TestAnnotationInheritance
parser.parse(classNames, null); parser.parse(classNames, null);
MultiMap map = handler.getMap();
assertNotNull(map); assertNotNull(map);
assertFalse(map.isEmpty()); assertFalse(map.isEmpty());
assertEquals(2, map.size()); assertEquals(2, map.size());

View File

@ -102,7 +102,7 @@ public class TestAnnotationParser
{ {
if (annotation == null || !"org.eclipse.jetty.annotations.Sample".equals(annotation)) if (annotation == null || !"org.eclipse.jetty.annotations.Sample".equals(annotation))
return; return;
assertEquals("org.eclipse.jetty.annotations.ClassA",info.getClassName()); assertEquals("org.eclipse.jetty.annotations.ClassA",info.getClassInfo().getClassName());
assertTrue(methods.contains(info.getMethodName())); assertTrue(methods.contains(info.getMethodName()));
assertEquals("org.eclipse.jetty.annotations.Sample",annotation); assertEquals("org.eclipse.jetty.annotations.Sample",annotation);
} }
@ -157,7 +157,7 @@ public class TestAnnotationParser
{ {
if (annotation == null || ! "org.eclipse.jetty.annotations.Multi".equals(annotation)) if (annotation == null || ! "org.eclipse.jetty.annotations.Multi".equals(annotation))
return; return;
assertTrue("org.eclipse.jetty.annotations.ClassB".equals(info.getClassName())); assertTrue("org.eclipse.jetty.annotations.ClassB".equals(info.getClassInfo().getClassName()));
assertTrue("a".equals(info.getMethodName())); assertTrue("a".equals(info.getMethodName()));
} }
} }

View File

@ -31,6 +31,7 @@ import java.util.List;
import org.eclipse.jetty.security.ConstraintSecurityHandler; import org.eclipse.jetty.security.ConstraintSecurityHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.servlet.ServletMapping; import org.eclipse.jetty.servlet.ServletMapping;
import org.eclipse.jetty.webapp.DiscoveredAnnotation;
import org.eclipse.jetty.webapp.WebAppContext; import org.eclipse.jetty.webapp.WebAppContext;
import org.junit.Test; import org.junit.Test;
@ -41,6 +42,27 @@ import org.junit.Test;
*/ */
public class TestServletAnnotations public class TestServletAnnotations
{ {
public class TestWebServletAnnotationHandler extends WebServletAnnotationHandler
{
List<DiscoveredAnnotation> _list = null;
public TestWebServletAnnotationHandler(WebAppContext context, List<DiscoveredAnnotation> list)
{
super(context);
_list = list;
}
@Override
public void addAnnotation(DiscoveredAnnotation a)
{
super.addAnnotation(a);
_list.add(a);
}
}
@Test @Test
public void testServletAnnotation() throws Exception public void testServletAnnotation() throws Exception
{ {
@ -49,7 +71,9 @@ public class TestServletAnnotations
AnnotationParser parser = new AnnotationParser(); AnnotationParser parser = new AnnotationParser();
WebAppContext wac = new WebAppContext(); WebAppContext wac = new WebAppContext();
WebServletAnnotationHandler handler = new WebServletAnnotationHandler(wac); List<DiscoveredAnnotation> results = new ArrayList<DiscoveredAnnotation>();
TestWebServletAnnotationHandler handler = new TestWebServletAnnotationHandler(wac, results);
parser.registerHandler(handler); parser.registerHandler(handler);
parser.parse(classes, new ClassNameResolver () parser.parse(classes, new ClassNameResolver ()
@ -65,10 +89,11 @@ public class TestServletAnnotations
} }
}); });
assertEquals(1, handler.getAnnotationList().size());
assertTrue(handler.getAnnotationList().get(0) instanceof WebServletAnnotation); assertEquals(1, results.size());
assertTrue(results.get(0) instanceof WebServletAnnotation);
handler.getAnnotationList().get(0).apply(); results.get(0).apply();
ServletHolder[] holders = wac.getServletHandler().getServlets(); ServletHolder[] holders = wac.getServletHandler().getServlets();
assertNotNull(holders); assertNotNull(holders);

View File

@ -51,11 +51,6 @@ public class MavenAnnotationConfiguration extends AnnotationConfiguration
throw new IllegalStateException ("No metadata"); throw new IllegalStateException ("No metadata");
parser.clearHandlers(); parser.clearHandlers();
for (AbstractDiscoverableAnnotationHandler h:_discoverableAnnotationHandlers)
{
if (h instanceof AbstractDiscoverableAnnotationHandler)
((AbstractDiscoverableAnnotationHandler)h).setResource(null); //
}
parser.registerHandlers(_discoverableAnnotationHandlers); parser.registerHandlers(_discoverableAnnotationHandlers);
parser.registerHandler(_classInheritanceHandler); parser.registerHandler(_classInheritanceHandler);
parser.registerHandlers(_containerInitializerAnnotationHandlers); parser.registerHandlers(_containerInitializerAnnotationHandlers);
@ -87,7 +82,7 @@ public class MavenAnnotationConfiguration extends AnnotationConfiguration
public void doParse (final WebAppContext context, final AnnotationParser parser, Resource resource) public void doParse (final WebAppContext context, final AnnotationParser parser, Resource resource)
throws Exception throws Exception
{ {
parser.parseDir(resource, new ClassNameResolver() parser.parse(resource, new ClassNameResolver()
{ {
public boolean isExcluded (String name) public boolean isExcluded (String name)
{ {

View File

@ -152,16 +152,6 @@ public class AnnotationConfiguration extends org.eclipse.jetty.annotations.Annot
Resource bundleRes = parser.getResource(bundle); Resource bundleRes = parser.getResource(bundle);
parser.clearHandlers(); parser.clearHandlers();
for (AbstractDiscoverableAnnotationHandler h:_discoverableAnnotationHandlers)
{
if (h instanceof AbstractDiscoverableAnnotationHandler)
{
if (webbundle == bundle)
((AbstractDiscoverableAnnotationHandler)h).setResource(null);
else
((AbstractDiscoverableAnnotationHandler)h).setResource(bundleRes);
}
}
parser.registerHandlers(_discoverableAnnotationHandlers); parser.registerHandlers(_discoverableAnnotationHandlers);
parser.registerHandler(_classInheritanceHandler); parser.registerHandler(_classInheritanceHandler);
parser.registerHandlers(_containerInitializerAnnotationHandlers); parser.registerHandlers(_containerInitializerAnnotationHandlers);

View File

@ -190,7 +190,7 @@ public class AnnotationParser extends org.eclipse.jetty.annotations.AnnotationPa
//transform into a classname to pass to the resolver //transform into a classname to pass to the resolver
String shortName = name.replace('/', '.').substring(0,name.length()-6); String shortName = name.replace('/', '.').substring(0,name.length()-6);
if ((resolver == null)|| (!resolver.isExcluded(shortName) && (!isParsed(shortName) || resolver.shouldOverride(shortName)))) if ((resolver == null)|| (!resolver.isExcluded(shortName) && (!isParsed(shortName) || resolver.shouldOverride(shortName))))
scanClass(classUrl.openStream()); scanClass(getResource(bundle), classUrl.openStream());
} }
} }

View File

@ -0,0 +1,130 @@
//
// ========================================================================
// Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//
package org.eclipse.jetty.util.resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
/**
* EmptyResource
*
* Represents a resource that does does not refer to any file, url, jar etc.
*/
public class EmptyResource extends Resource
{
public static final Resource INSTANCE = new EmptyResource();
private EmptyResource()
{
}
@Override
public boolean isContainedIn(Resource r) throws MalformedURLException
{
return false;
}
@Override
public void close()
{
}
@Override
public boolean exists()
{
return false;
}
@Override
public boolean isDirectory()
{
return false;
}
@Override
public long lastModified()
{
return 0;
}
@Override
public long length()
{
return 0;
}
@Override
public URL getURL()
{
return null;
}
@Override
public File getFile() throws IOException
{
return null;
}
@Override
public String getName()
{
return null;
}
@Override
public InputStream getInputStream() throws IOException
{
return null;
}
@Override
public ReadableByteChannel getReadableByteChannel() throws IOException
{
return null;
}
@Override
public boolean delete() throws SecurityException
{
return false;
}
@Override
public boolean renameTo(Resource dest) throws SecurityException
{
return false;
}
@Override
public String[] list()
{
return null;
}
@Override
public Resource addPath(String path) throws IOException, MalformedURLException
{
return null;
}
}

View File

@ -18,6 +18,12 @@
package org.eclipse.jetty.webapp; package org.eclipse.jetty.webapp;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.channels.ReadableByteChannel;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -28,6 +34,8 @@ import javax.servlet.ServletContext;
import org.eclipse.jetty.util.log.Log; import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger; import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.resource.EmptyResource;
import org.eclipse.jetty.util.resource.JarResource;
import org.eclipse.jetty.util.resource.Resource; import org.eclipse.jetty.util.resource.Resource;
@ -43,18 +51,18 @@ public class MetaData
private static final Logger LOG = Log.getLogger(MetaData.class); private static final Logger LOG = Log.getLogger(MetaData.class);
public static final String ORDERED_LIBS = "javax.servlet.context.orderedLibs"; public static final String ORDERED_LIBS = "javax.servlet.context.orderedLibs";
public static final Resource NON_FRAG_RESOURCE = EmptyResource.INSTANCE;
protected Map<String, OriginInfo> _origins =new HashMap<String,OriginInfo>(); protected Map<String, OriginInfo> _origins =new HashMap<String,OriginInfo>();
protected WebDescriptor _webDefaultsRoot; protected WebDescriptor _webDefaultsRoot;
protected WebDescriptor _webXmlRoot; protected WebDescriptor _webXmlRoot;
protected final List<WebDescriptor> _webOverrideRoots=new ArrayList<WebDescriptor>(); protected final List<WebDescriptor> _webOverrideRoots=new ArrayList<WebDescriptor>();
protected boolean _metaDataComplete; protected boolean _metaDataComplete;
protected final List<DiscoveredAnnotation> _annotations = new ArrayList<DiscoveredAnnotation>();
protected final List<DescriptorProcessor> _descriptorProcessors = new ArrayList<DescriptorProcessor>(); protected final List<DescriptorProcessor> _descriptorProcessors = new ArrayList<DescriptorProcessor>();
protected final List<FragmentDescriptor> _webFragmentRoots = new ArrayList<FragmentDescriptor>(); protected final List<FragmentDescriptor> _webFragmentRoots = new ArrayList<FragmentDescriptor>();
protected final Map<String,FragmentDescriptor> _webFragmentNameMap = new HashMap<String,FragmentDescriptor>(); protected final Map<String,FragmentDescriptor> _webFragmentNameMap = new HashMap<String,FragmentDescriptor>();
protected final Map<Resource, FragmentDescriptor> _webFragmentResourceMap = new HashMap<Resource, FragmentDescriptor>(); protected final Map<Resource, FragmentDescriptor> _webFragmentResourceMap = new HashMap<Resource, FragmentDescriptor>();
protected final Map<Resource, List<DiscoveredAnnotation>> _webFragmentAnnotations = new HashMap<Resource, List<DiscoveredAnnotation>>(); protected final Map<Resource, List<DiscoveredAnnotation>> _annotations = new HashMap<Resource, List<DiscoveredAnnotation>>();
protected final List<Resource> _webInfClasses = new ArrayList<Resource>(); protected final List<Resource> _webInfClasses = new ArrayList<Resource>();
protected final List<Resource> _webInfJars = new ArrayList<Resource>(); protected final List<Resource> _webInfJars = new ArrayList<Resource>();
protected final List<Resource> _orderedWebInfJars = new ArrayList<Resource>(); protected final List<Resource> _orderedWebInfJars = new ArrayList<Resource>();
@ -135,7 +143,7 @@ public class MetaData
_webFragmentRoots.clear(); _webFragmentRoots.clear();
_webFragmentNameMap.clear(); _webFragmentNameMap.clear();
_webFragmentResourceMap.clear(); _webFragmentResourceMap.clear();
_webFragmentAnnotations.clear(); _annotations.clear();
_webInfJars.clear(); _webInfJars.clear();
_orderedWebInfJars.clear(); _orderedWebInfJars.clear();
_orderedContainerResources.clear(); _orderedContainerResources.clear();
@ -276,45 +284,37 @@ public class MetaData
return; return;
for (DiscoveredAnnotation a:annotations) for (DiscoveredAnnotation a:annotations)
{ {
Resource r = a.getResource(); addDiscoveredAnnotation(a);
if (r == null || !_webInfJars.contains(r))
_annotations.add(a);
else
addDiscoveredAnnotation(a.getResource(), a);
} }
} }
public void addDiscoveredAnnotation(Resource resource, DiscoveredAnnotation annotation)
{
List<DiscoveredAnnotation> list = _webFragmentAnnotations.get(resource);
if (list == null)
{
list = new ArrayList<DiscoveredAnnotation>();
_webFragmentAnnotations.put(resource, list);
}
list.add(annotation);
}
public void addDiscoveredAnnotations(Resource resource, List<DiscoveredAnnotation> annotations) public void addDiscoveredAnnotation (DiscoveredAnnotation annotation)
{ {
List<DiscoveredAnnotation> list = _webFragmentAnnotations.get(resource); if (annotation == null)
return;
//if no resource associated with an annotation or the resource is not one of the WEB-INF/lib jars,
//map it to empty resource
Resource resource = annotation.getResource();
if (resource == null || !_webInfJars.contains(resource))
resource = EmptyResource.INSTANCE;
List<DiscoveredAnnotation> list = _annotations.get(resource);
if (list == null) if (list == null)
{ {
list = new ArrayList<DiscoveredAnnotation>(); list = new ArrayList<DiscoveredAnnotation>();
_webFragmentAnnotations.put(resource, list); _annotations.put(resource, list);
} }
list.add(annotation);
list.addAll(annotations);
} }
public void addDescriptorProcessor(DescriptorProcessor p) public void addDescriptorProcessor(DescriptorProcessor p)
{ {
_descriptorProcessors.add(p); _descriptorProcessors.add(p);
} }
public void orderFragments () public void orderFragments ()
{ {
//if we have already ordered them don't do it again //if we have already ordered them don't do it again
@ -373,13 +373,20 @@ public class MetaData
} }
} }
for (DiscoveredAnnotation a:_annotations) //get an apply the annotations that are not associated with a fragment (and hence for
//which no ordering applies
List<DiscoveredAnnotation> nonFragAnnotations = _annotations.get(NON_FRAG_RESOURCE);
if (nonFragAnnotations != null)
{ {
LOG.debug("apply {}",a); for (DiscoveredAnnotation a:nonFragAnnotations)
a.apply(); {
LOG.debug("apply {}",a);
a.apply();
}
} }
//apply the annotations that are associated with a fragment, according to the
//established ordering
List<Resource> resources = getOrderedWebInfJars(); List<Resource> resources = getOrderedWebInfJars();
for (Resource r:resources) for (Resource r:resources)
{ {
@ -393,7 +400,7 @@ public class MetaData
} }
} }
List<DiscoveredAnnotation> fragAnnotations = _webFragmentAnnotations.get(r); List<DiscoveredAnnotation> fragAnnotations = _annotations.get(r);
if (fragAnnotations != null) if (fragAnnotations != null)
{ {
for (DiscoveredAnnotation a:fragAnnotations) for (DiscoveredAnnotation a:fragAnnotations)

View File

@ -62,7 +62,7 @@ public class ServerEndpointAnnotationHandler extends AbstractDiscoverableAnnotat
return; return;
} }
ServerEndpointAnnotation annotation = new ServerEndpointAnnotation(_context,info.getClassName(),_resource); ServerEndpointAnnotation annotation = new ServerEndpointAnnotation(_context,info.getClassName(),info.getContainingResource());
addAnnotation(annotation); addAnnotation(annotation);
} }
} }