org.reflections.adapters.JavassistAdapter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swagger-all Show documentation
Show all versions of swagger-all Show documentation
swagger-all is a rebundled verison of Swagger as one OSGi bundle.
package org.reflections.adapters;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import javassist.bytecode.*;
import javassist.bytecode.annotation.Annotation;
import org.reflections.ReflectionsException;
import org.reflections.util.Utils;
import org.reflections.vfs.Vfs;
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.List;
import static javassist.bytecode.AccessFlag.isPrivate;
import static javassist.bytecode.AccessFlag.isProtected;
/**
*
*/
public class JavassistAdapter implements MetadataAdapter {
/**setting this to false will result in returning only visible annotations from the relevant methods here (only {@link java.lang.annotation.RetentionPolicy#RUNTIME})*/
public static boolean includeInvisibleTag = true;
public List getFields(final ClassFile cls) {
//noinspection unchecked
return cls.getFields();
}
public List getMethods(final ClassFile cls) {
//noinspection unchecked
return cls.getMethods();
}
public String getMethodName(final MethodInfo method) {
return method.getName();
}
public List getParameterNames(final MethodInfo method) {
String descriptor = method.getDescriptor();
descriptor = descriptor.substring(descriptor.indexOf("(") + 1, descriptor.lastIndexOf(")"));
return splitDescriptorToTypeNames(descriptor);
}
public List getClassAnnotationNames(final ClassFile aClass) {
return getAnnotationNames((AnnotationsAttribute) aClass.getAttribute(AnnotationsAttribute.visibleTag),
includeInvisibleTag ? (AnnotationsAttribute) aClass.getAttribute(AnnotationsAttribute.invisibleTag) : null);
}
public List getFieldAnnotationNames(final FieldInfo field) {
return getAnnotationNames((AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.visibleTag),
includeInvisibleTag ? (AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.invisibleTag) : null);
}
public List getMethodAnnotationNames(final MethodInfo method) {
return getAnnotationNames((AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.visibleTag),
includeInvisibleTag ? (AnnotationsAttribute) method.getAttribute(AnnotationsAttribute.invisibleTag) : null);
}
public List getParameterAnnotationNames(final MethodInfo method, final int parameterIndex) {
List result = Lists.newArrayList();
List parameterAnnotationsAttributes = Lists.newArrayList((ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.visibleTag),
(ParameterAnnotationsAttribute) method.getAttribute(ParameterAnnotationsAttribute.invisibleTag));
if (parameterAnnotationsAttributes != null) {
for (ParameterAnnotationsAttribute parameterAnnotationsAttribute : parameterAnnotationsAttributes) {
if (parameterAnnotationsAttribute != null) {
Annotation[][] annotations = parameterAnnotationsAttribute.getAnnotations();
if (parameterIndex < annotations.length) {
Annotation[] annotation = annotations[parameterIndex];
result.addAll(getAnnotationNames(annotation));
}
}
}
}
return result;
}
public String getReturnTypeName(final MethodInfo method) {
String descriptor = method.getDescriptor();
descriptor = descriptor.substring(descriptor.lastIndexOf(")") + 1);
return splitDescriptorToTypeNames(descriptor).get(0);
}
public String getFieldName(final FieldInfo field) {
return field.getName();
}
public ClassFile getOfCreateClassObject(final Vfs.File file) {
InputStream inputStream = null;
try {
inputStream = file.openInputStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(inputStream));
return new ClassFile(dis);
} catch (IOException e) {
throw new ReflectionsException("could not create class file from " + file.getName(), e);
} finally {
Utils.close(inputStream);
}
}
public String getMethodModifier(MethodInfo method) {
int accessFlags = method.getAccessFlags();
return isPrivate(accessFlags) ? "private" :
isProtected(accessFlags) ? "protected" :
isPublic(accessFlags) ? "public" : "";
}
public String getMethodKey(ClassFile cls, MethodInfo method) {
return getMethodName(method) + "(" + Joiner.on(", ").join(getParameterNames(method)) + ")";
}
public String getMethodFullKey(ClassFile cls, MethodInfo method) {
return getClassName(cls) + "." + getMethodKey(cls, method);
}
public boolean isPublic(Object o) {
Integer accessFlags =
o instanceof ClassFile ? ((ClassFile) o).getAccessFlags() :
o instanceof FieldInfo ? ((FieldInfo) o).getAccessFlags() :
o instanceof MethodInfo ? ((MethodInfo) o).getAccessFlags() : null;
return accessFlags != null && AccessFlag.isPublic(accessFlags);
}
//
public String getClassName(final ClassFile cls) {
return cls.getName();
}
public String getSuperclassName(final ClassFile cls) {
return cls.getSuperclass();
}
public List getInterfacesNames(final ClassFile cls) {
return Arrays.asList(cls.getInterfaces());
}
public boolean acceptsInput(String file) {
return file.endsWith(".class");
}
//
private List getAnnotationNames(final AnnotationsAttribute... annotationsAttributes) {
List result = Lists.newArrayList();
if (annotationsAttributes != null) {
for (AnnotationsAttribute annotationsAttribute : annotationsAttributes) {
if (annotationsAttribute != null) {
for (Annotation annotation : annotationsAttribute.getAnnotations()) {
result.add(annotation.getTypeName());
}
}
}
}
return result;
}
private List getAnnotationNames(final Annotation[] annotations) {
List result = Lists.newArrayList();
for (Annotation annotation : annotations) {
result.add(annotation.getTypeName());
}
return result;
}
private List splitDescriptorToTypeNames(final String descriptors) {
List result = Lists.newArrayList();
if (descriptors != null && descriptors.length() != 0) {
List indices = Lists.newArrayList();
Descriptor.Iterator iterator = new Descriptor.Iterator(descriptors);
while (iterator.hasNext()) {
indices.add(iterator.next());
}
indices.add(descriptors.length());
for (int i = 0; i < indices.size() - 1; i++) {
String s1 = Descriptor.toString(descriptors.substring(indices.get(i), indices.get(i + 1)));
result.add(s1);
}
}
return result;
}
}