Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
   *
   * Copyright (c) 2010-2012 Oracle and/or its affiliates. All rights reserved.
   *
   * The contents of this file are subject to the terms of either the GNU
   * General Public License Version 2 only ("GPL") or the Common Development
   * and Distribution License("CDDL") (collectively, the "License").  You
   * may not use this file except in compliance with the License.  You can
  * obtain a copy of the License at
  * http://glassfish.java.net/public/CDDL+GPL_1_1.html
  * or packager/legal/LICENSE.txt.  See the License for the specific
  * language governing permissions and limitations under the License.
  *
  * When distributing the software, include this License Header Notice in each
  * file and include the License file at packager/legal/LICENSE.txt.
  *
  * GPL Classpath Exception:
  * Oracle designates this particular file as subject to the "Classpath"
  * exception as provided by Oracle in the GPL Version 2 section of the License
  * file that accompanied this code.
  *
  * Modifications:
  * If applicable, add the following below the License Header, with the fields
  * enclosed by brackets [] replaced by your own identifying information:
  * "Portions Copyright [year] [name of copyright owner]"
  *
  * Contributor(s):
  * If you wish your version of this file to be governed by only the CDDL or
  * only the GPL Version 2, indicate your decision by adding "[Contributor]
  * elects to include this software in this distribution under the [CDDL or GPL
  * Version 2] license."  If you don't indicate a single choice of license, a
  * recipient has the option to distribute your version of this file under
  * either the CDDL, the GPL Version 2 or to extend the choice of license to
  * its licensees as provided above.  However, if you add GPL Version 2 code
  * and therefore, elected the GPL Version 2 license, then the option applies
  * only if the new code is made subject to such option by the copyright
  * holder.
  */
 package com.sun.jersey.spi.scanning;
 
 import java.util.Set;
 
 
A scanner listener that processes Java class files (resource names ending in ".class") annotated with one or more of a set of declared annotations.

Java classes of a Java class file are processed, using ASM, to ascertain if those classes are annotated with one or more of the set of declared annotations.

Such an annotated Java class of a Java class file is loaded if the class is public or is an inner class that is static and public.

Author(s):
Paul.Sandoz@Sun.Com
 
 public class AnnotationScannerListener implements ScannerListener {
     private final ClassLoader classloader;
 
     private final Set<Class<?>> classes;
 
     private final Set<Stringannotations;
 
     private final AnnotatedClassVisitor classVisitor;

    
Create a scanner listener to check for annotated Java classes in Java class files.

Parameters:
annotations the set of annotation classes to check on Java class files.
 
     public AnnotationScannerListener(Class<? extends Annotation>... annotations) {
         this(ReflectionHelper.getContextClassLoader(), annotations);
     }

    
Create a scanner listener to check for annotated Java classes in Java class files.

Parameters:
classloader the class loader to use to load Java classes that are annotated with any one of the annotations.
annotations the set of annotation classes to check on Java class files.
    public AnnotationScannerListener(ClassLoader classloader,
                                     Class<? extends Annotation>... annotations) {
        this. = classloader;
        this. = new LinkedHashSet<Class<?>>();
        this. = getAnnotationSet(annotations);
        this. = new AnnotatedClassVisitor();
    }

    
Get the set of annotated classes.

Returns:
the set of annotated classes.
    public Set<Class<?>> getAnnotatedClasses() {
        return ;
    }
    private Set<StringgetAnnotationSet(Class<? extends Annotation>... annotations) {
        Set<Stringa = new HashSet<String>();
        for (Class c : annotations) {
            a.add("L" + c.getName().replaceAll("\\.""/") + ";");
        }
        return a;
    }
    // ScannerListener
    public boolean onAccept(String name) {
        return name.endsWith(".class");
    }
    public void onProcess(String nameInputStream inthrows IOException {
        new ClassReader(in).accept(, 0);
    }
    //
    private final class AnnotatedClassVisitor implements ClassVisitor {

        
The name of the visited class.
        private String className;
        
True if the class has the correct scope
        private boolean isScoped;
        
True if the class has the correct declared annotations
        private boolean isAnnotated;
        public void visit(int versionint accessString name,
                          String signatureString superNameString[] interfaces) {
             = name;
             = (access & .) != 0;
             = false;
        }
        public AnnotationVisitor visitAnnotation(String descboolean visible) {
             |= .contains(desc);
            return null;
        }
        public void visitInnerClass(String nameString outerName,
                                    String innerNameint access) {
            // If the name of the class that was visited is equal
            // to the name of this visited inner class then
            // this access field needs to be used for checking the scope
            // of the inner class
            if (.equals(name)) {
                 = (access & .) != 0;
                // Inner classes need to be statically scoped
                 &= (access & .) == .;
            }
        }
        public void visitEnd() {
            if ( && ) {
                // Correctly scoped and annotated
                // add to the set of matching classes.
                .add(getClassForName(.replaceAll("/"".")));
            }
        }
        public void visitOuterClass(String stringString string0,
                                    String string1) {
            // Do nothing
        }
        public FieldVisitor visitField(int iString string,
                                       String string0String string1Object object) {
            // Do nothing
            return null;
        }
        public void visitSource(String stringString string0) {
            // Do nothing
        }
        public void visitAttribute(Attribute attribute) {
            // Do nothing
        }
        public MethodVisitor visitMethod(int iString string,
                                         String string0String string1String[] string2) {
            // Do nothing
            return null;
        }
        private Class getClassForName(String className) {
            try {
                final OsgiRegistry osgiRegistry = ReflectionHelper.getOsgiRegistryInstance();
                if (osgiRegistry != null) {
                    return osgiRegistry.classForNameWithException(className);
                } else {
                    return ReflectionHelper.classForNameWithException(className);
                }
            } catch (ClassNotFoundException ex) {
                String s = "A class file of the class name, " +
                        className +
                        "is identified but the class could not be found";
                throw new RuntimeException(sex);
            }
        }
    }
New to GrepCode? Check out our FAQ X