myfaces-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tandrasc...@apache.org
Subject [myfaces] branch master updated: cleanup
Date Thu, 03 Oct 2019 20:59:52 GMT
This is an automated email from the ASF dual-hosted git repository.

tandraschko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/myfaces.git


The following commit(s) were added to refs/heads/master by this push:
     new b9472b6  cleanup
b9472b6 is described below

commit b9472b63499dfcbb6b2b2368bf1601a868989335
Author: Thomas Andraschko <tandraschko@apache.org>
AuthorDate: Thu Oct 3 22:59:43 2019 +0200

    cleanup
---
 .../annotation/DefaultAnnotationProvider.java      | 115 ++++++++-------------
 .../{_PackageInfo.java => PackageInfo.java}        | 107 ++++---------------
 .../annotation/_ClassByteCodeAnnotationFilter.java |   6 +-
 .../org/apache/myfaces/config/util/JarUtils.java   |  87 ----------------
 4 files changed, 66 insertions(+), 249 deletions(-)

diff --git a/impl/src/main/java/org/apache/myfaces/config/annotation/DefaultAnnotationProvider.java
b/impl/src/main/java/org/apache/myfaces/config/annotation/DefaultAnnotationProvider.java
index 2da85bc..bfaa296 100644
--- a/impl/src/main/java/org/apache/myfaces/config/annotation/DefaultAnnotationProvider.java
+++ b/impl/src/main/java/org/apache/myfaces/config/annotation/DefaultAnnotationProvider.java
@@ -87,18 +87,18 @@ public class DefaultAnnotationProvider extends AnnotationProvider
      * inside application JARs.</p>
      */
     private static final String FACES_CONFIG_IMPLICIT = "META-INF/faces-config.xml";
-    
-    private final _ClassByteCodeAnnotationFilter _filter;
 
     /**
      * This set contains the annotation names that this AnnotationConfigurator is able to
scan
      * in the format that is read from .class file.
      */
-    private static Set<String> byteCodeAnnotationsNames;
+    private static final Set<String> JSF_ANNOTATION_NAMES;
+    
+    private static final Set<Class<? extends Annotation>> JSF_ANNOTATION_CLASSES;
 
     static
     {
-        Set<String> bcan = new HashSet<String>(10, 1f);
+        Set<String> bcan = new HashSet<>(10, 1f);
         bcan.add("Ljavax/faces/component/FacesComponent;");
         bcan.add("Ljavax/faces/component/behavior/FacesBehavior;");
         bcan.add("Ljavax/faces/convert/FacesConverter;");
@@ -109,30 +109,23 @@ public class DefaultAnnotationProvider extends AnnotationProvider
         //bcan.add("Ljavax/faces/event/ListenersFor;");
         bcan.add("Ljavax/faces/render/FacesBehaviorRenderer;");
         bcan.add("Ljavax/faces/view/facelets/FaceletsResourceResolver;");
-
-        byteCodeAnnotationsNames = Collections.unmodifiableSet(bcan);
-    }
-    
-    private static final Set<Class<? extends Annotation>> JSF_ANNOTATION_CLASSES;
-    
-    static
-    {
-        Set<Class<? extends Annotation>> bcan = new HashSet<Class<? extends
Annotation>>(10, 1f);
-        bcan.add(FacesComponent.class);
-        bcan.add(FacesBehavior.class);
-        bcan.add(FacesConverter.class);
-        bcan.add(FacesValidator.class);
-        bcan.add(FacesRenderer.class);
-        bcan.add(NamedEvent.class);
-        bcan.add(FacesBehaviorRenderer.class);
-        bcan.add(FaceletsResourceResolver.class);
-        JSF_ANNOTATION_CLASSES = Collections.unmodifiableSet(bcan);
+        JSF_ANNOTATION_NAMES = Collections.unmodifiableSet(bcan);
+
+        Set<Class<? extends Annotation>> ancl = new HashSet<>(10, 1f);
+        ancl.add(FacesComponent.class);
+        ancl.add(FacesBehavior.class);
+        ancl.add(FacesConverter.class);
+        ancl.add(FacesValidator.class);
+        ancl.add(FacesRenderer.class);
+        ancl.add(NamedEvent.class);
+        ancl.add(FacesBehaviorRenderer.class);
+        ancl.add(FaceletsResourceResolver.class);
+        JSF_ANNOTATION_CLASSES = Collections.unmodifiableSet(ancl);
     }
-    
+
     public DefaultAnnotationProvider()
     {
         super();
-        _filter = new _ClassByteCodeAnnotationFilter();
     }
     
     @Override
@@ -213,7 +206,7 @@ public class DefaultAnnotationProvider extends AnnotationProvider
     {
         if (urls != null && !urls.isEmpty())
         {
-            List<Class<?>> list = new ArrayList<Class<?>>();
+            List<Class<?>> list = new ArrayList<>();
             for (URL url : urls)
             {
                 try
@@ -234,11 +227,6 @@ public class DefaultAnnotationProvider extends AnnotationProvider
         return Collections.emptyList();
     }
 
-    protected Collection<Class<?>> getAnnotatedMyfacesImplClasses(ExternalContext
ctx, URL url)
-    {
-        return Collections.emptyList();
-    }
-
     protected Collection<Class<?>> getAnnotatedWebInfClasses(ExternalContext
ctx) throws IOException
     {
         String scanPackages = MyfacesConfig.getCurrentInstance(ctx).getScanPackages();
@@ -248,11 +236,7 @@ public class DefaultAnnotationProvider extends AnnotationProvider
             {
                 return packageClasses(ctx, scanPackages);
             }
-            catch (ClassNotFoundException e)
-            {
-                throw new FacesException(e);
-            }
-            catch (IOException e)
+            catch (ClassNotFoundException | IOException e)
             {
                 throw new FacesException(e);
             }
@@ -272,11 +256,10 @@ public class DefaultAnnotationProvider extends AnnotationProvider
      * @exception ClassNotFoundException if a located class cannot be loaded
      * @exception IOException if an input/output error occurs
      */
-    private List<Class<?>> packageClasses(final ExternalContext externalContext,
-            final String scanPackages) throws ClassNotFoundException, IOException
+    private List<Class<?>> packageClasses(final ExternalContext externalContext,
final String scanPackages)
+            throws ClassNotFoundException, IOException
     {
-
-        List<Class<?>> list = new ArrayList<Class<?>>();
+        List<Class<?>> list = new ArrayList<>();
 
         String[] scanPackageTokens = scanPackages.split(",");
         for (String scanPackageToken : scanPackageTokens)
@@ -292,9 +275,8 @@ public class DefaultAnnotationProvider extends AnnotationProvider
             }
             else
             {
-                List<Class> list2 = new ArrayList<Class>();
-                _PackageInfo.getInstance().getClasses(list2, scanPackageToken);
-                for (Class c : list2)
+                Class[] classes = PackageInfo.getClasses(scanPackageToken);
+                for (Class c : classes)
                 {
                     list.add(c);                    
                 }
@@ -344,7 +326,8 @@ public class DefaultAnnotationProvider extends AnnotationProvider
             try
             {
                 in = new DataInputStream(jar.getInputStream(entry));
-                couldContainAnnotation = _filter.couldContainAnnotationsOnClassDef(in, byteCodeAnnotationsNames);
+                couldContainAnnotation = _ClassByteCodeAnnotationFilter.couldContainAnnotationsOnClassDef(in,
+                        JSF_ANNOTATION_NAMES);
             }
             catch (IOException e)
             {
@@ -381,14 +364,11 @@ public class DefaultAnnotationProvider extends AnnotationProvider
                 {
                     clazz = loader.loadClass(name.replace('/', '.'));
                 }
-                catch (NoClassDefFoundError e)
-                {
-                    // Skip this class - we cannot analyze classes we cannot load
-                }
-                catch (Exception e)
+                catch (NoClassDefFoundError | Exception e)
                 {
                     // Skip this class - we cannot analyze classes we cannot load
                 }
+                // Skip this class - we cannot analyze classes we cannot load
                 if (clazz != null)
                 {
                     list.add(clazz);
@@ -412,7 +392,7 @@ public class DefaultAnnotationProvider extends AnnotationProvider
      */
     private List<Class<?>> webClasses(ExternalContext externalContext)
     {
-        List<Class<?>> list = new ArrayList<Class<?>>();
+        List<Class<?>> list = new ArrayList<>();
         webClasses(externalContext, WEB_CLASSES_PREFIX, list);
         return list;
     }
@@ -428,21 +408,18 @@ public class DefaultAnnotationProvider extends AnnotationProvider
      *
      * @exception ClassNotFoundException if a located class cannot be loaded
      */
-    private void webClasses(ExternalContext externalContext, String prefix,
-            List<Class<?>> list)
+    private void webClasses(ExternalContext externalContext, String prefix, List<Class<?>>
list)
     {
-
         ClassLoader loader = ClassUtils.getCurrentLoader(this);
 
         Set<String> paths = externalContext.getResourcePaths(prefix);
-        if(paths == null)
+        if (paths == null)
         {
             return; //need this in case there is no WEB-INF/classes directory
         }
         if (log.isLoggable(Level.FINEST))
         {
-            log.finest("webClasses(" + prefix + ") - Received " + paths.size()
-                    + " paths to check");
+            log.finest("webClasses(" + prefix + ") - Received " + paths.size() + " paths
to check");
         }
 
         String path = null;
@@ -451,15 +428,14 @@ public class DefaultAnnotationProvider extends AnnotationProvider
         {
             if (log.isLoggable(Level.WARNING))
             {
-                log
-                        .warning("AnnotationConfigurator does not found classes "
-                                + "for annotations in "
-                                + prefix
-                                + " ."
-                                + " This could happen because maven jetty plugin is used"
-                                + " (goal jetty:run). Try configure "
-                                + MyfacesConfig.SCAN_PACKAGES + " init parameter "
-                                + "or use jetty:run-exploded instead.");
+                log.warning("AnnotationConfigurator does not found classes "
+                            + "for annotations in "
+                            + prefix
+                            + " ."
+                            + " This could happen because maven jetty plugin is used"
+                            + " (goal jetty:run). Try configure "
+                            + MyfacesConfig.SCAN_PACKAGES + " init parameter "
+                            + "or use jetty:run-exploded instead.");
             }
         }
         else
@@ -478,8 +454,8 @@ public class DefaultAnnotationProvider extends AnnotationProvider
                     try
                     {
                         in = new DataInputStream(externalContext.getResourceAsStream(path));
-                        couldContainAnnotation = _filter.couldContainAnnotationsOnClassDef(in,
-                                byteCodeAnnotationsNames);
+                        couldContainAnnotation = _ClassByteCodeAnnotationFilter.couldContainAnnotationsOnClassDef(in,
+                                JSF_ANNOTATION_NAMES);
                     }
                     catch (IOException e)
                     {
@@ -520,14 +496,11 @@ public class DefaultAnnotationProvider extends AnnotationProvider
                         {
                             clazz = loader.loadClass(path);
                         }
-                        catch (NoClassDefFoundError e)
-                        {
-                            // Skip this class - we cannot analyze classes we cannot load
-                        }
-                        catch (Exception e)
+                        catch (NoClassDefFoundError | Exception e)
                         {
                             // Skip this class - we cannot analyze classes we cannot load
                         }
+                        // Skip this class - we cannot analyze classes we cannot load
                         if (clazz != null)
                         {
                             list.add(clazz);
diff --git a/impl/src/main/java/org/apache/myfaces/config/annotation/_PackageInfo.java b/impl/src/main/java/org/apache/myfaces/config/annotation/PackageInfo.java
similarity index 70%
rename from impl/src/main/java/org/apache/myfaces/config/annotation/_PackageInfo.java
rename to impl/src/main/java/org/apache/myfaces/config/annotation/PackageInfo.java
index 76cab16..0ec6fbe 100644
--- a/impl/src/main/java/org/apache/myfaces/config/annotation/_PackageInfo.java
+++ b/impl/src/main/java/org/apache/myfaces/config/annotation/PackageInfo.java
@@ -25,6 +25,7 @@ import java.net.JarURLConnection;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.jar.JarEntry;
@@ -35,8 +36,6 @@ import java.util.logging.Logger;
 import org.apache.myfaces.util.lang.ClassUtils;
 
 /**
- * Copied from org.apache.shale.tiger.view.faces.PackageInfo
- * 
  * <p>Utility class with methods that support getting a recursive list of
  * classes starting with a specific package name.</p>
  * 
@@ -44,48 +43,27 @@ import org.apache.myfaces.util.lang.ClassUtils;
  * @author Leonardo Uribe (latest modification by $Author$)
  * @version $Revision$ $Date$
  */
-class _PackageInfo
+class PackageInfo
 {
-
-    /**
-     * <p>The <code>Log</code> instance we will be using.</p>
-     */
-    private transient Logger log = null;
-
-    /**
-     * the singleton for this class
-     */
-    private final static _PackageInfo INSTANCE = new _PackageInfo();
-
-    /**
-     * <p>Get the singleton instance of this class.</p>
-     */
-    public final static _PackageInfo getInstance()
-    {
-
-        return INSTANCE;
-
-    }
+    private static final Logger LOG = Logger.getLogger(PackageInfo.class.getName());
 
     /**
      * <p>Return an array of all classes, visible to our application class loader,
      * in the specified Java package.</p>
      *
-     * @param classes List of matching classes being accumulated
      * @param pckgname Package name used to select matching classes
      *
      * @throws ClassNotFoundException
      */
-    public Class[] getClasses(final List<Class> classes, final String pckgname)
-            throws ClassNotFoundException
+    public static Class[] getClasses(final String pckgname) throws ClassNotFoundException
     {
-
+        List<Class> classes = new ArrayList<>();
+        
         Enumeration resources;
         ClassLoader cld;
         String path;
         try
         {
-
             // convert the package name to a path
             path = pckgname.replace('.', '/');
 
@@ -101,19 +79,10 @@ class _PackageInfo
             {
                 throw new ClassNotFoundException("No resource for " + path);
             }
-
-        }
-        catch (NullPointerException e)
-        {
-            throw (ClassNotFoundException) new ClassNotFoundException(pckgname
-                    + " (" + pckgname
-                    + ") does not appear to be a valid package", e);
         }
-        catch (IOException e)
+        catch (NullPointerException | IOException e)
         {
-            throw (ClassNotFoundException) new ClassNotFoundException(pckgname
-                    + " (" + pckgname
-                    + ") does not appear to be a valid package", e);
+            throw new ClassNotFoundException(pckgname + " does not appear to be a valid package",
e);
         }
 
         // iterate through all resources containing the package in question
@@ -127,9 +96,7 @@ class _PackageInfo
             }
             catch (IOException e)
             {
-                throw (ClassNotFoundException) new ClassNotFoundException(
-                        pckgname + " (" + pckgname
-                                + ") does not appear to be a valid package", e);
+                throw new ClassNotFoundException(pckgname + " does not appear to be a valid
package", e);
             }
 
             if (connection instanceof JarURLConnection)
@@ -143,10 +110,7 @@ class _PackageInfo
                 }
                 catch (IOException e)
                 {
-                    throw (ClassNotFoundException) new ClassNotFoundException(
-                            pckgname + " (" + pckgname
-                                    + ") does not appear to be a valid package",
-                            e);
+                    throw new ClassNotFoundException(pckgname + " does not appear to be a
valid package", e);
                 }
                 Enumeration<JarEntry> entries = jarFile.entries();
                 while (entries.hasMoreElements())
@@ -175,7 +139,7 @@ class _PackageInfo
                 }
                 catch (URISyntaxException e)
                 {
-                    log().log(Level.WARNING, "error loading directory " + connection, e);
+                    LOG.log(Level.WARNING, "error loading directory " + connection, e);
                     continue;
                 }
 
@@ -185,8 +149,7 @@ class _PackageInfo
 
         if (classes.size() < 1)
         {
-            throw new ClassNotFoundException(pckgname
-                    + " does not appear to be a valid package");
+            throw new ClassNotFoundException(pckgname + " does not appear to be a valid package");
         }
 
         Class[] resolvedClasses = new Class[classes.size()];
@@ -200,11 +163,9 @@ class _PackageInfo
      *
      * @param entryName Filename to be converted
      */
-    protected String filenameToClassname(String entryName)
+    protected static String filenameToClassname(String entryName)
     {
-
         return entryName.substring(0, entryName.length() - 6).replace('/', '.');
-
     }
 
     /**
@@ -215,23 +176,16 @@ class _PackageInfo
      * @param cld ClassLoader from which to load the specified class
      * @param className Name of the class to be loaded
      */
-    protected void loadClass(List<Class> classes, ClassLoader cld,
-            String className)
+    protected static void loadClass(List<Class> classes, ClassLoader cld, String className)
     {
-
         try
         {
             classes.add(cld.loadClass(className));
         }
-        catch (NoClassDefFoundError e)
-        {
-            log().log(Level.WARNING, "error loading class " + className, e);
-        }
-        catch (ClassNotFoundException e)
+        catch (NoClassDefFoundError | ClassNotFoundException e)
         {
-            log().log(Level.WARNING, "error loading class " + className, e);
+            LOG.log(Level.WARNING, "error loading class " + className, e);
         }
-
     }
 
     /**
@@ -243,19 +197,17 @@ class _PackageInfo
      * @param cld ClassLoader being searched for matching classes
      * @param pckgname Package name used to select matching classes
      */
-    protected void listFilesRecursive(final List<Class> classes,
+    protected static void listFilesRecursive(final List<Class> classes,
             final File base, final ClassLoader cld, final String pckgname)
     {
-
         base.listFiles(new FileFilter()
         {
-
+            @Override
             public boolean accept(File file)
             {
                 if (file.isDirectory())
                 {
-                    listFilesRecursive(classes, file, cld, pckgname + '.'
-                            + file.getName());
+                    listFilesRecursive(classes, file, cld, pckgname + '.' + file.getName());
                     return false;
                 }
                 if (!file.getName().toLowerCase().endsWith(".class"))
@@ -263,30 +215,11 @@ class _PackageInfo
                     return false;
                 }
 
-                String className = filenameToClassname(pckgname + '.'
-                        + file.getName());
+                String className = filenameToClassname(pckgname + '.' + file.getName());
                 loadClass(classes, cld, className);
 
                 return false;
             }
-
         });
-
     }
-
-    /**
-     * <p>Return the <code>Log</code> instance to be used for this class,
-     * instantiating a new one if necessary.</p>
-     */
-    private Logger log()
-    {
-
-        if (log == null)
-        {
-            log = Logger.getLogger(_PackageInfo.class.getName());
-        }
-        return log;
-
-    }
-
 }
diff --git a/impl/src/main/java/org/apache/myfaces/config/annotation/_ClassByteCodeAnnotationFilter.java
b/impl/src/main/java/org/apache/myfaces/config/annotation/_ClassByteCodeAnnotationFilter.java
index 2c73f71..856ae87 100644
--- a/impl/src/main/java/org/apache/myfaces/config/annotation/_ClassByteCodeAnnotationFilter.java
+++ b/impl/src/main/java/org/apache/myfaces/config/annotation/_ClassByteCodeAnnotationFilter.java
@@ -33,7 +33,6 @@ import java.util.logging.Logger;
  */
 class _ClassByteCodeAnnotationFilter
 {
-    
     private static final Logger log = Logger.getLogger(_ClassByteCodeAnnotationFilter.class.getName());
     
     //Constants used to define type in cp_info structure
@@ -63,9 +62,8 @@ class _ClassByteCodeAnnotationFilter
      * @return
      * @throws IOException
      */
-    public boolean couldContainAnnotationsOnClassDef(DataInput in,
-            Set<String> byteCodeAnnotationsNames)
-        throws IOException
+    public static boolean couldContainAnnotationsOnClassDef(DataInput in, Set<String>
byteCodeAnnotationsNames)
+            throws IOException
     {
         /* According to Java VM Spec, each .class file contains
          * a single class or interface definition. The structure
diff --git a/impl/src/main/java/org/apache/myfaces/config/util/JarUtils.java b/impl/src/main/java/org/apache/myfaces/config/util/JarUtils.java
deleted file mode 100644
index c9617c7..0000000
--- a/impl/src/main/java/org/apache/myfaces/config/util/JarUtils.java
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied.  See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.myfaces.config.util;
-
-import java.io.IOException;
-import java.net.JarURLConnection;
-import java.net.URL;
-import java.net.URLConnection;
-import java.util.jar.JarFile;
-
-/**
- *
- * @author Leonardo Uribe
- */
-public class JarUtils
-{
-
-    public static JarFile getJarFile(URL url) throws IOException
-    {
-        URLConnection conn = url.openConnection();
-        conn.setUseCaches(false);
-        conn.setDefaultUseCaches(false);
-
-        JarFile jarFile;
-        if (conn instanceof JarURLConnection)
-        {
-            jarFile = ((JarURLConnection) conn).getJarFile();
-        }
-        else
-        {
-            jarFile = _getAlternativeJarFile(url);
-        }
-        return jarFile;
-    }
-    
-    /**
-     * taken from org.apache.myfaces.view.facelets.util.Classpath
-     * 
-     * For URLs to JARs that do not use JarURLConnection - allowed by the servlet spec -
attempt to produce a JarFile
-     * object all the same. Known servlet engines that function like this include Weblogic
and OC4J. This is not a full
-     * solution, since an unpacked WAR or EAR will not have JAR "files" as such.
-     */
-    private static JarFile _getAlternativeJarFile(URL url) throws IOException
-    {
-        String urlFile = url.getFile();
-
-        // Trim off any suffix - which is prefixed by "!/" on Weblogic
-        int separatorIndex = urlFile.indexOf("!/");
-
-        // OK, didn't find that. Try the less safe "!", used on OC4J
-        if (separatorIndex == -1)
-        {
-            separatorIndex = urlFile.indexOf('!');
-        }
-
-        if (separatorIndex != -1)
-        {
-            String jarFileUrl = urlFile.substring(0, separatorIndex);
-            // And trim off any "file:" prefix.
-            if (jarFileUrl.startsWith("file:"))
-            {
-                jarFileUrl = jarFileUrl.substring("file:".length());
-            }
-
-            return new JarFile(jarFileUrl);
-        }
-
-        return null;
-    }
-
-}


Mime
View raw message