Author: markt
Date: Wed Jun 18 19:19:14 2014
New Revision: 1603591
URL: http://svn.apache.org/r1603591
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/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java
tomcat/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java
tomcat/trunk/webapps/docs/changelog.xml
Modified: tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java?rev=1603591&r1=1603590&r2=1603591&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java
(original)
+++ tomcat/trunk/java/org/apache/catalina/core/DefaultInstanceManager.java Wed
Jun 18 19:19:14 2014
@@ -316,38 +316,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));
}
}
@@ -374,43 +374,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/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java
URL:
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java?rev=1603591&r1=1603590&r2=1603591&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java
(original)
+++ tomcat/trunk/java/org/apache/catalina/startup/WebAnnotationSet.java Wed Jun
18 19:19:14 2014
@@ -258,10 +258,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);
}
@@ -276,9 +275,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/trunk/webapps/docs/changelog.xml
URL:
http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1603591&r1=1603590&r2=1603591&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Wed Jun 18 19:19:14 2014
@@ -142,6 +142,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: [email protected]
For additional commands, e-mail: [email protected]