Author: markt Date: Wed Jun 18 19:21:21 2014 New Revision: 1603592 URL: http://svn.apache.org/r1603592 Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=56611 Refactor code to remove inefficient calls to Method.isAnnotationPresent(). Based on a patch by Jian Mou.
Modified: tomcat/tc7.0.x/trunk/ (props changed) tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Propchange: tomcat/tc7.0.x/trunk/ ------------------------------------------------------------------------------ Merged /tomcat/trunk:r1603591 Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java?rev=1603592&r1=1603591&r2=1603592&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java Wed Jun 18 19:21:21 2014 @@ -298,38 +298,38 @@ public class DefaultInstanceManager impl // JNDI is enabled Field[] fields = Introspection.getDeclaredFields(clazz); for (Field field : fields) { + Resource resourceAnnotation; + EJB ejbAnnotation; + WebServiceRef webServiceRefAnnotation; + PersistenceContext persistenceContextAnnotation; + PersistenceUnit persistenceUnitAnnotation; if (injections != null && injections.containsKey(field.getName())) { annotations.add(new AnnotationCacheEntry( field.getName(), null, injections.get(field.getName()), AnnotationCacheEntryType.FIELD)); - } else if (field.isAnnotationPresent(Resource.class)) { - Resource annotation = field.getAnnotation(Resource.class); - annotations.add(new AnnotationCacheEntry( - field.getName(), null, annotation.name(), - AnnotationCacheEntryType.FIELD)); - } else if (field.isAnnotationPresent(EJB.class)) { - EJB annotation = field.getAnnotation(EJB.class); - annotations.add(new AnnotationCacheEntry( - field.getName(), null, annotation.name(), + } else if ((resourceAnnotation = + field.getAnnotation(Resource.class)) != null) { + annotations.add(new AnnotationCacheEntry(field.getName(), null, + resourceAnnotation.name(), AnnotationCacheEntryType.FIELD)); + } else if ((ejbAnnotation = + field.getAnnotation(EJB.class)) != null) { + annotations.add(new AnnotationCacheEntry(field.getName(), null, + ejbAnnotation.name(), AnnotationCacheEntryType.FIELD)); + } else if ((webServiceRefAnnotation = + field.getAnnotation(WebServiceRef.class)) != null) { + annotations.add(new AnnotationCacheEntry(field.getName(), null, + webServiceRefAnnotation.name(), AnnotationCacheEntryType.FIELD)); - } else if (field.isAnnotationPresent(WebServiceRef.class)) { - WebServiceRef annotation = - field.getAnnotation(WebServiceRef.class); - annotations.add(new AnnotationCacheEntry( - field.getName(), null, annotation.name(), + } else if ((persistenceContextAnnotation = + field.getAnnotation(PersistenceContext.class)) != null) { + annotations.add(new AnnotationCacheEntry(field.getName(), null, + persistenceContextAnnotation.name(), AnnotationCacheEntryType.FIELD)); - } else if (field.isAnnotationPresent(PersistenceContext.class)) { - PersistenceContext annotation = - field.getAnnotation(PersistenceContext.class); - annotations.add(new AnnotationCacheEntry( - field.getName(), null, annotation.name(), - AnnotationCacheEntryType.FIELD)); - } else if (field.isAnnotationPresent(PersistenceUnit.class)) { - PersistenceUnit annotation = - field.getAnnotation(PersistenceUnit.class); - annotations.add(new AnnotationCacheEntry( - field.getName(), null, annotation.name(), + } else if ((persistenceUnitAnnotation = + field.getAnnotation(PersistenceUnit.class)) != null) { + annotations.add(new AnnotationCacheEntry(field.getName(), null, + persistenceUnitAnnotation.name(), AnnotationCacheEntryType.FIELD)); } } @@ -356,43 +356,44 @@ public class DefaultInstanceManager impl continue; } } - if (method.isAnnotationPresent(Resource.class)) { - Resource annotation = method.getAnnotation(Resource.class); + Resource resourceAnnotation; + EJB ejbAnnotation; + WebServiceRef webServiceRefAnnotation; + PersistenceContext persistenceContextAnnotation; + PersistenceUnit persistenceUnitAnnotation; + if ((resourceAnnotation = + method.getAnnotation(Resource.class)) != null) { annotations.add(new AnnotationCacheEntry( method.getName(), method.getParameterTypes(), - annotation.name(), + resourceAnnotation.name(), AnnotationCacheEntryType.SETTER)); - } else if (method.isAnnotationPresent(EJB.class)) { - EJB annotation = method.getAnnotation(EJB.class); + } else if ((ejbAnnotation = + method.getAnnotation(EJB.class)) != null) { annotations.add(new AnnotationCacheEntry( method.getName(), method.getParameterTypes(), - annotation.name(), + ejbAnnotation.name(), AnnotationCacheEntryType.SETTER)); - } else if (method.isAnnotationPresent(WebServiceRef.class)) { - WebServiceRef annotation = - method.getAnnotation(WebServiceRef.class); + } else if ((webServiceRefAnnotation = + method.getAnnotation(WebServiceRef.class)) != null) { annotations.add(new AnnotationCacheEntry( method.getName(), method.getParameterTypes(), - annotation.name(), + webServiceRefAnnotation.name(), AnnotationCacheEntryType.SETTER)); - } else if (method.isAnnotationPresent(PersistenceContext.class)) { - PersistenceContext annotation = - method.getAnnotation(PersistenceContext.class); + } else if ((persistenceContextAnnotation = + method.getAnnotation(PersistenceContext.class)) != null) { annotations.add(new AnnotationCacheEntry( method.getName(), method.getParameterTypes(), - annotation.name(), + persistenceContextAnnotation.name(), AnnotationCacheEntryType.SETTER)); - } else if (method.isAnnotationPresent(PersistenceUnit.class)) { - PersistenceUnit annotation = - method.getAnnotation(PersistenceUnit.class); + } else if ((persistenceUnitAnnotation = method.getAnnotation(PersistenceUnit.class)) != null) { annotations.add(new AnnotationCacheEntry( method.getName(), method.getParameterTypes(), - annotation.name(), + persistenceUnitAnnotation.name(), AnnotationCacheEntryType.SETTER)); } } Modified: tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java?rev=1603592&r1=1603591&r2=1603592&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java (original) +++ tomcat/tc7.0.x/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java Wed Jun 18 19:21:21 2014 @@ -261,10 +261,9 @@ public class WebAnnotationSet { Field[] fields = Introspection.getDeclaredFields(classClass); if (fields != null && fields.length > 0) { for (Field field : fields) { - if (field.isAnnotationPresent(Resource.class)) { - Resource annotation = field.getAnnotation(Resource.class); - String defaultName = - classClass.getName() + SEPARATOR + field.getName(); + Resource annotation = field.getAnnotation(Resource.class); + if (annotation != null) { + String defaultName = classClass.getName() + SEPARATOR + field.getName(); Class<?> defaultType = field.getType(); addResource(context, annotation, defaultName, defaultType); } @@ -279,9 +278,8 @@ public class WebAnnotationSet { Method[] methods = Introspection.getDeclaredMethods(classClass); if (methods != null && methods.length > 0) { for (Method method : methods) { - if (method.isAnnotationPresent(Resource.class)) { - Resource annotation = method.getAnnotation(Resource.class); - + Resource annotation = method.getAnnotation(Resource.class); + if (annotation != null) { if (!Introspection.isValidSetter(method)) { throw new IllegalArgumentException(sm.getString( "webAnnotationSet.invalidInjection")); Modified: tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml?rev=1603592&r1=1603591&r2=1603592&view=diff ============================================================================== --- tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml (original) +++ tomcat/tc7.0.x/trunk/webapps/docs/changelog.xml Wed Jun 18 19:21:21 2014 @@ -101,6 +101,11 @@ digester rules and documentation for <code>MemoryRealm</code>. (markt/kkolinko) </fix> + <scode> + <bug>56611</bug>: Refactor code to remove inefficient calls to + <code>Method.isAnnotationPresent()</code>. Based on a patch by Jian Mou. + (markt) + </scode> </changelog> </subsection> <subsection name="Coyote"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org