001/*
002 *  Copyright 2012 Chris Pheby
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.jadira.scanner.core.helper;
017
018import java.lang.annotation.Annotation;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.Set;
022
023import javassist.ClassPool;
024import javassist.bytecode.AnnotationsAttribute;
025import javassist.bytecode.ClassFile;
026import javassist.bytecode.FieldInfo;
027import javassist.bytecode.MethodInfo;
028import javassist.bytecode.ParameterAnnotationsAttribute;
029
030import org.jadira.scanner.core.exception.ClasspathAccessException;
031
032public final class JavassistAnnotationsHelper {
033
034    private static final ClassPool CLASS_POOL = ClassPool.getDefault();
035        
036        private JavassistAnnotationsHelper() {
037        }
038
039        public static Annotation[] getAnnotationsForClass(ClassFile classFile) {
040
041                AnnotationsAttribute visible = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.visibleTag);
042                AnnotationsAttribute invisible = (AnnotationsAttribute) classFile.getAttribute(AnnotationsAttribute.invisibleTag);
043
044                Set<Annotation> retVal = new HashSet<Annotation>();
045
046                retVal.addAll(findAnnotationsForAnnotationsAttribute(visible));
047                retVal.addAll(findAnnotationsForAnnotationsAttribute(invisible));
048
049                return retVal.toArray(new Annotation[retVal.size()]);
050        }
051        
052        public static Annotation[] getAnnotationsForMethod(MethodInfo methodInfo) {
053
054                AnnotationsAttribute visible = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.visibleTag);
055                AnnotationsAttribute invisible = (AnnotationsAttribute) methodInfo.getAttribute(AnnotationsAttribute.invisibleTag);
056
057                Set<Annotation> retVal = new HashSet<Annotation>();
058
059                retVal.addAll(findAnnotationsForAnnotationsAttribute(visible));
060                retVal.addAll(findAnnotationsForAnnotationsAttribute(invisible));
061
062                return retVal.toArray(new Annotation[retVal.size()]);
063        }
064
065        public static Annotation[] getAnnotationsForMethodParameter(MethodInfo methodInfo, int index) {
066
067                ParameterAnnotationsAttribute visible = (ParameterAnnotationsAttribute) methodInfo.getAttribute(ParameterAnnotationsAttribute.visibleTag);
068                ParameterAnnotationsAttribute invisible = (ParameterAnnotationsAttribute) methodInfo.getAttribute(ParameterAnnotationsAttribute.invisibleTag);
069
070                Set<Annotation> retVal = new HashSet<Annotation>();
071
072                retVal.addAll(findAnnotationsForAnnotationsArray(visible.getAnnotations()[index]));
073                retVal.addAll(findAnnotationsForAnnotationsArray(invisible.getAnnotations()[index]));
074
075                return retVal.toArray(new Annotation[retVal.size()]);
076        }
077
078        public static Annotation[] getAnnotationsForFieldInfo(FieldInfo fieldInfo) {
079
080                AnnotationsAttribute visible = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.visibleTag);
081                AnnotationsAttribute invisible = (AnnotationsAttribute) fieldInfo.getAttribute(AnnotationsAttribute.invisibleTag);
082
083                Set<Annotation> retVal = new HashSet<Annotation>();
084
085                retVal.addAll(findAnnotationsForAnnotationsAttribute(visible));
086                retVal.addAll(findAnnotationsForAnnotationsAttribute(invisible));
087
088                return retVal.toArray(new Annotation[retVal.size()]);
089        }
090
091        private static Set<Annotation> findAnnotationsForAnnotationsAttribute(AnnotationsAttribute attr) {
092
093                if (attr != null) {
094                        javassist.bytecode.annotation.Annotation[] anns = attr.getAnnotations();
095                        return findAnnotationsForAnnotationsArray(anns);
096                }
097                return Collections.emptySet();
098        }
099
100        private static Set<Annotation> findAnnotationsForAnnotationsArray(javassist.bytecode.annotation.Annotation[] anns) {
101
102                final Set<Annotation> retVal = new HashSet<Annotation>();
103                for (javassist.bytecode.annotation.Annotation next : anns) {
104
105                        try {
106                                final Annotation toAdd = (Annotation) (next.toAnnotationType(JavassistAnnotationsHelper.class.getClassLoader(), CLASS_POOL));
107                                retVal.add(toAdd);
108                        } catch (ClassNotFoundException e) {
109                                throw new ClasspathAccessException("Problem finding class for annotation: " + e.getMessage(), e);
110                        }
111                }
112                return retVal;
113        }
114}