This is an automated email from the ASF dual-hosted git repository.

remm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/main by this push:
     new 5eb8c57b77 Refine metadata complete
5eb8c57b77 is described below

commit 5eb8c57b77dea35ec85af8e7eb288b7d4aac378b
Author: remm <r...@apache.org>
AuthorDate: Thu Dec 1 11:21:33 2022 +0100

    Refine metadata complete
    
    Although this seems unusable to me, I read some rather official and
    relatively recent clarifications on metadata complete that mean:
    everything that has a metadata equivalent is affected by
    metadata-complete. not simply the annotations that require a full scan.
    Since injection target or post construct and pre destroy exist in XML,
    then the annotations should be ignored. However, processing of the
    "annotations" from the equivalent XML metadata should occur.
---
 .../catalina/core/DefaultInstanceManager.java      | 138 +++++++++++----------
 .../apache/catalina/startup/WebAnnotationSet.java  |  57 +++++----
 2 files changed, 100 insertions(+), 95 deletions(-)

diff --git a/java/org/apache/catalina/core/DefaultInstanceManager.java 
b/java/org/apache/catalina/core/DefaultInstanceManager.java
index 8ef9c3ea28..79b120b7c4 100644
--- a/java/org/apache/catalina/core/DefaultInstanceManager.java
+++ b/java/org/apache/catalina/core/DefaultInstanceManager.java
@@ -104,6 +104,7 @@ public class DefaultInstanceManager implements 
InstanceManager {
     protected final ClassLoader containerClassLoader;
     protected final boolean privileged;
     protected final boolean ignoreAnnotations;
+    protected final boolean metadataComplete;
     private final Set<String> restrictedClasses;
     private final ManagedConcurrentWeakHashMap<Class<?>, 
AnnotationCacheEntry[]> annotationCache =
             new ManagedConcurrentWeakHashMap<>();
@@ -118,6 +119,7 @@ public class DefaultInstanceManager implements 
InstanceManager {
         privileged = catalinaContext.getPrivileged();
         this.containerClassLoader = containerClassLoader;
         ignoreAnnotations = catalinaContext.getIgnoreAnnotations();
+        metadataComplete = catalinaContext.getMetadataComplete();
         Log log = catalinaContext.getLogger();
         Set<String> classNames = new HashSet<>();
         loadProperties(classNames,
@@ -328,45 +330,47 @@ public class DefaultInstanceManager implements 
InstanceManager {
                                 continue;
                             }
                         }
-                        Resource resourceAnnotation;
-                        Annotation ejbAnnotation;
-                        Annotation webServiceRefAnnotation;
-                        Annotation persistenceContextAnnotation;
-                        Annotation persistenceUnitAnnotation;
-                        if ((resourceAnnotation = 
method.getAnnotation(Resource.class)) != null) {
-                            annotations.add(new AnnotationCacheEntry(
-                                    method.getName(),
-                                    method.getParameterTypes(),
-                                    resourceAnnotation.name(),
-                                    AnnotationCacheEntryType.SETTER));
-                        } else if (EJB_PRESENT &&
-                                (ejbAnnotation = 
method.getAnnotation(EJB.class)) != null) {
-                            annotations.add(new AnnotationCacheEntry(
-                                    method.getName(),
-                                    method.getParameterTypes(),
-                                    ((EJB) ejbAnnotation).name(),
-                                    AnnotationCacheEntryType.SETTER));
-                        } else if (WS_PRESENT && (webServiceRefAnnotation =
-                                method.getAnnotation(WebServiceRef.class)) != 
null) {
-                            annotations.add(new AnnotationCacheEntry(
-                                    method.getName(),
-                                    method.getParameterTypes(),
-                                    ((WebServiceRef) 
webServiceRefAnnotation).name(),
-                                    AnnotationCacheEntryType.SETTER));
-                        } else if (JPA_PRESENT && 
(persistenceContextAnnotation =
-                                
method.getAnnotation(PersistenceContext.class)) != null) {
-                            annotations.add(new AnnotationCacheEntry(
-                                    method.getName(),
-                                    method.getParameterTypes(),
-                                    ((PersistenceContext) 
persistenceContextAnnotation).name(),
-                                    AnnotationCacheEntryType.SETTER));
-                        } else if (JPA_PRESENT && (persistenceUnitAnnotation =
-                                method.getAnnotation(PersistenceUnit.class)) 
!= null) {
-                            annotations.add(new AnnotationCacheEntry(
-                                    method.getName(),
-                                    method.getParameterTypes(),
-                                    ((PersistenceUnit) 
persistenceUnitAnnotation).name(),
-                                    AnnotationCacheEntryType.SETTER));
+                        if (!metadataComplete) {
+                            Resource resourceAnnotation;
+                            Annotation ejbAnnotation;
+                            Annotation webServiceRefAnnotation;
+                            Annotation persistenceContextAnnotation;
+                            Annotation persistenceUnitAnnotation;
+                            if ((resourceAnnotation = 
method.getAnnotation(Resource.class)) != null) {
+                                annotations.add(new AnnotationCacheEntry(
+                                        method.getName(),
+                                        method.getParameterTypes(),
+                                        resourceAnnotation.name(),
+                                        AnnotationCacheEntryType.SETTER));
+                            } else if (EJB_PRESENT &&
+                                    (ejbAnnotation = 
method.getAnnotation(EJB.class)) != null) {
+                                annotations.add(new AnnotationCacheEntry(
+                                        method.getName(),
+                                        method.getParameterTypes(),
+                                        ((EJB) ejbAnnotation).name(),
+                                        AnnotationCacheEntryType.SETTER));
+                            } else if (WS_PRESENT && (webServiceRefAnnotation =
+                                    method.getAnnotation(WebServiceRef.class)) 
!= null) {
+                                annotations.add(new AnnotationCacheEntry(
+                                        method.getName(),
+                                        method.getParameterTypes(),
+                                        ((WebServiceRef) 
webServiceRefAnnotation).name(),
+                                        AnnotationCacheEntryType.SETTER));
+                            } else if (JPA_PRESENT && 
(persistenceContextAnnotation =
+                                    
method.getAnnotation(PersistenceContext.class)) != null) {
+                                annotations.add(new AnnotationCacheEntry(
+                                        method.getName(),
+                                        method.getParameterTypes(),
+                                        ((PersistenceContext) 
persistenceContextAnnotation).name(),
+                                        AnnotationCacheEntryType.SETTER));
+                            } else if (JPA_PRESENT && 
(persistenceUnitAnnotation =
+                                    
method.getAnnotation(PersistenceUnit.class)) != null) {
+                                annotations.add(new AnnotationCacheEntry(
+                                        method.getName(),
+                                        method.getParameterTypes(),
+                                        ((PersistenceUnit) 
persistenceUnitAnnotation).name(),
+                                        AnnotationCacheEntryType.SETTER));
+                            }
                         }
                     }
 
@@ -410,29 +414,31 @@ public class DefaultInstanceManager implements 
InstanceManager {
                                     fieldName, null,
                                     injections.get(fieldName),
                                     AnnotationCacheEntryType.FIELD));
-                        } else if ((resourceAnnotation =
-                                field.getAnnotation(Resource.class)) != null) {
-                            annotations.add(new 
AnnotationCacheEntry(fieldName, null,
-                                    resourceAnnotation.name(), 
AnnotationCacheEntryType.FIELD));
-                        } else if (EJB_PRESENT &&
-                                (ejbAnnotation = 
field.getAnnotation(EJB.class)) != null) {
-                            annotations.add(new 
AnnotationCacheEntry(fieldName, null,
-                                    ((EJB) ejbAnnotation).name(), 
AnnotationCacheEntryType.FIELD));
-                        } else if (WS_PRESENT && (webServiceRefAnnotation =
-                                field.getAnnotation(WebServiceRef.class)) != 
null) {
-                            annotations.add(new 
AnnotationCacheEntry(fieldName, null,
-                                    ((WebServiceRef) 
webServiceRefAnnotation).name(),
-                                    AnnotationCacheEntryType.FIELD));
-                        } else if (JPA_PRESENT && 
(persistenceContextAnnotation =
-                                field.getAnnotation(PersistenceContext.class)) 
!= null) {
-                            annotations.add(new 
AnnotationCacheEntry(fieldName, null,
-                                    ((PersistenceContext) 
persistenceContextAnnotation).name(),
-                                    AnnotationCacheEntryType.FIELD));
-                        } else if (JPA_PRESENT && (persistenceUnitAnnotation =
-                                field.getAnnotation(PersistenceUnit.class)) != 
null) {
-                            annotations.add(new 
AnnotationCacheEntry(fieldName, null,
-                                    ((PersistenceUnit) 
persistenceUnitAnnotation).name(),
-                                    AnnotationCacheEntryType.FIELD));
+                        } else if (!metadataComplete) {
+                            if ((resourceAnnotation =
+                                    field.getAnnotation(Resource.class)) != 
null) {
+                                annotations.add(new 
AnnotationCacheEntry(fieldName, null,
+                                        resourceAnnotation.name(), 
AnnotationCacheEntryType.FIELD));
+                            } else if (EJB_PRESENT &&
+                                    (ejbAnnotation = 
field.getAnnotation(EJB.class)) != null) {
+                                annotations.add(new 
AnnotationCacheEntry(fieldName, null,
+                                        ((EJB) ejbAnnotation).name(), 
AnnotationCacheEntryType.FIELD));
+                            } else if (WS_PRESENT && (webServiceRefAnnotation =
+                                    field.getAnnotation(WebServiceRef.class)) 
!= null) {
+                                annotations.add(new 
AnnotationCacheEntry(fieldName, null,
+                                        ((WebServiceRef) 
webServiceRefAnnotation).name(),
+                                        AnnotationCacheEntryType.FIELD));
+                            } else if (JPA_PRESENT && 
(persistenceContextAnnotation =
+                                    
field.getAnnotation(PersistenceContext.class)) != null) {
+                                annotations.add(new 
AnnotationCacheEntry(fieldName, null,
+                                        ((PersistenceContext) 
persistenceContextAnnotation).name(),
+                                        AnnotationCacheEntryType.FIELD));
+                            } else if (JPA_PRESENT && 
(persistenceUnitAnnotation =
+                                    
field.getAnnotation(PersistenceUnit.class)) != null) {
+                                annotations.add(new 
AnnotationCacheEntry(fieldName, null,
+                                        ((PersistenceUnit) 
persistenceUnitAnnotation).name(),
+                                        AnnotationCacheEntryType.FIELD));
+                            }
                         }
                     }
                 }
@@ -693,19 +699,19 @@ public class DefaultInstanceManager implements 
InstanceManager {
     }
 
 
-    private static Method findPostConstruct(Method currentPostConstruct,
+    private Method findPostConstruct(Method currentPostConstruct,
             String postConstructFromXml, Method method) {
         return findLifecycleCallback(currentPostConstruct,
             postConstructFromXml, method, PostConstruct.class);
     }
 
-    private static Method findPreDestroy(Method currentPreDestroy,
+    private Method findPreDestroy(Method currentPreDestroy,
         String preDestroyFromXml, Method method) {
         return findLifecycleCallback(currentPreDestroy,
             preDestroyFromXml, method, PreDestroy.class);
     }
 
-    private static Method findLifecycleCallback(Method currentMethod,
+    private Method findLifecycleCallback(Method currentMethod,
             String methodNameFromXml, Method method,
             Class<? extends Annotation> annotation) {
         Method result = currentMethod;
@@ -717,7 +723,7 @@ public class DefaultInstanceManager implements 
InstanceManager {
                 }
                 result = method;
             }
-        } else {
+        } else if (!metadataComplete) {
             if (method.isAnnotationPresent(annotation)) {
                 if (currentMethod != null || 
!Introspection.isValidLifecycleCallback(method)) {
                     throw new IllegalArgumentException(
diff --git a/java/org/apache/catalina/startup/WebAnnotationSet.java 
b/java/org/apache/catalina/startup/WebAnnotationSet.java
index feba189271..99e67143b4 100644
--- a/java/org/apache/catalina/startup/WebAnnotationSet.java
+++ b/java/org/apache/catalina/startup/WebAnnotationSet.java
@@ -64,9 +64,11 @@ public class WebAnnotationSet {
      * @param context The context which will have its annotations processed
      */
     public static void loadApplicationAnnotations(Context context) {
-        loadApplicationListenerAnnotations(context);
-        loadApplicationFilterAnnotations(context);
-        loadApplicationServletAnnotations(context);
+        if (!context.getMetadataComplete()) {
+            loadApplicationListenerAnnotations(context);
+            loadApplicationFilterAnnotations(context);
+            loadApplicationServletAnnotations(context);
+        }
     }
 
 
@@ -137,23 +139,21 @@ public class WebAnnotationSet {
                 loadFieldsAnnotation(context, clazz);
                 loadMethodsAnnotation(context, clazz);
 
-                if (!context.getMetadataComplete()) {
-                    /* Process RunAs annotation which can be only on servlets.
-                     * Ref JSR 250, equivalent to the run-as element in
-                     * the deployment descriptor
-                     */
-                    RunAs runAs = clazz.getAnnotation(RunAs.class);
-                    if (runAs != null) {
-                        wrapper.setRunAs(runAs.value());
-                    }
+                /* Process RunAs annotation which can be only on servlets.
+                 * Ref JSR 250, equivalent to the run-as element in
+                 * the deployment descriptor
+                 */
+                RunAs runAs = clazz.getAnnotation(RunAs.class);
+                if (runAs != null) {
+                    wrapper.setRunAs(runAs.value());
+                }
 
-                    // Process ServletSecurity annotation
-                    ServletSecurity servletSecurity = 
clazz.getAnnotation(ServletSecurity.class);
-                    if (servletSecurity != null) {
-                        context.addServletSecurity(
-                                new ApplicationServletRegistration(wrapper, 
context),
-                                new ServletSecurityElement(servletSecurity));
-                    }
+                // Process ServletSecurity annotation
+                ServletSecurity servletSecurity = 
clazz.getAnnotation(ServletSecurity.class);
+                if (servletSecurity != null) {
+                    context.addServletSecurity(
+                            new ApplicationServletRegistration(wrapper, 
context),
+                            new ServletSecurityElement(servletSecurity));
                 }
             }
         }
@@ -253,16 +253,15 @@ public class WebAnnotationSet {
             }
         }
         */
-        if (!context.getMetadataComplete()) {
-            /* Process DeclareRoles annotation.
-             * Ref JSR 250, equivalent to the security-role element in
-             * the deployment descriptor
-             */
-            DeclareRoles declareRolesAnnotation = 
clazz.getAnnotation(DeclareRoles.class);
-            if (declareRolesAnnotation != null && 
declareRolesAnnotation.value() != null) {
-                for (String role : declareRolesAnnotation.value()) {
-                    context.addSecurityRole(role);
-                }
+
+        /* Process DeclareRoles annotation.
+         * Ref JSR 250, equivalent to the security-role element in
+         * the deployment descriptor
+         */
+        DeclareRoles declareRolesAnnotation = 
clazz.getAnnotation(DeclareRoles.class);
+        if (declareRolesAnnotation != null && declareRolesAnnotation.value() 
!= null) {
+            for (String role : declareRolesAnnotation.value()) {
+                context.addSecurityRole(role);
             }
         }
     }


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to