/**
* Licensed to the Austrian Association for Software Tool Integration (AASTI)
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. The AASTI 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.openengsb.core.weaver.service.internal.model;
import javassist.CtClass;
import javassist.CtField;
import javassist.bytecode.AnnotationsAttribute;
import javassist.bytecode.ClassFile;
import javassist.bytecode.FieldInfo;
/**
* Util class which help with javassist issues which aren't that correctly implemented in the javassist library.
*/
public final class JavassistUtils {
private JavassistUtils() {
}
/**
* Returns true if the given class has an annotation set with the given name, returns false otherwise.
*/
public static boolean hasAnnotation(CtClass clazz, String annotationName) {
ClassFile cf = clazz.getClassFile2();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
cf.getAttribute(AnnotationsAttribute.invisibleTag);
AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
cf.getAttribute(AnnotationsAttribute.visibleTag);
return checkAnnotation(ainfo, ainfo2, annotationName);
}
/**
* Returns true if the given field has an annotation set with the given name, returns false otherwise.
*/
public static boolean hasAnnotation(CtField field, String annotationName) {
FieldInfo info = field.getFieldInfo();
AnnotationsAttribute ainfo = (AnnotationsAttribute)
info.getAttribute(AnnotationsAttribute.invisibleTag);
AnnotationsAttribute ainfo2 = (AnnotationsAttribute)
info.getAttribute(AnnotationsAttribute.visibleTag);
return checkAnnotation(ainfo, ainfo2, annotationName);
}
/**
* Checks if an annotation with the given name is either in the invisible or in the visible annotation attributes.
*/
private static boolean checkAnnotation(AnnotationsAttribute invisible, AnnotationsAttribute visible,
String annotationName) {
boolean exist1 = false;
boolean exist2 = false;
if (invisible != null) {
exist1 = invisible.getAnnotation(annotationName) != null;
}
if (visible != null) {
exist2 = visible.getAnnotation(annotationName) != null;
}
return exist1 || exist2;
}
}