use of org.objectweb.asm.Type in project buck by facebook.
the class FirstOrderHelper method addTypeAndSupers.
private void addTypeAndSupers(Type type) {
addType(type);
FirstOrderTypeInfo info = knownTypes.get(type);
if (info != null) {
addTypeAndSupers(info.superType);
for (Type interfaceType : info.interfaceTypes) {
addTypeAndSupers(interfaceType);
}
}
}
use of org.objectweb.asm.Type in project gradle by gradle.
the class MethodMember method toString.
@Override
public String toString() {
StringBuilder methodDesc = new StringBuilder();
methodDesc.append(Modifier.toString(getAccess())).append(" ");
methodDesc.append(Type.getReturnType(getTypeDesc()).getClassName()).append(" ");
methodDesc.append(getName());
methodDesc.append("(");
Type[] argumentTypes = Type.getArgumentTypes(getTypeDesc());
for (int i = 0, argumentTypesLength = argumentTypes.length; i < argumentTypesLength; i++) {
Type type = argumentTypes[i];
methodDesc.append(type.getClassName());
if (i < argumentTypesLength - 1) {
methodDesc.append(", ");
}
}
methodDesc.append(")");
return methodDesc.toString();
}
use of org.objectweb.asm.Type in project gradle by gradle.
the class ClasspathInferer method find.
/**
* Locates the classpath required by the given target class. Traverses the dependency graph of classes used by the specified class and collects the result in the given collection.
*/
private void find(Class<?> target, Collection<Class<?>> visited, Collection<URL> dest) {
ClassLoader targetClassLoader = target.getClassLoader();
if (targetClassLoader == null || targetClassLoader == ClassLoaderUtils.getPlatformClassLoader()) {
// A system class, skip it
return;
}
if (!visited.add(target)) {
// Already seen this class, skip it
return;
}
String resourceName = target.getName().replace('.', '/') + ".class";
URL resource = targetClassLoader.getResource(resourceName);
try {
if (resource == null) {
LOGGER.warn("Could not determine classpath for {}", target);
return;
}
File classPathRoot = ClasspathUtil.getClasspathForClass(target);
dest.add(classPathRoot.toURI().toURL());
// To determine the dependencies of the class, load up the byte code and look for CONSTANT_Class entries in the constant pool
ClassReader reader;
URLConnection urlConnection = resource.openConnection();
if (urlConnection instanceof JarURLConnection) {
// Using the caches for these connections leaves the Jar files open. Don't use the cache, so that the Jar file is closed when the stream is closed below
// There are other options for solving this that may be more performant. However a class is inspected this way once and the result reused, so this approach is probably fine
urlConnection.setUseCaches(false);
}
InputStream inputStream = urlConnection.getInputStream();
try {
reader = new Java9ClassReader(ByteStreams.toByteArray(inputStream));
} finally {
inputStream.close();
}
char[] charBuffer = new char[reader.getMaxStringLength()];
for (int i = 1; i < reader.getItemCount(); i++) {
int itemOffset = reader.getItem(i);
if (itemOffset > 0 && reader.readByte(itemOffset - 1) == 7) {
// A CONSTANT_Class entry, read the class descriptor
String classDescriptor = reader.readUTF8(itemOffset, charBuffer);
Type type = Type.getObjectType(classDescriptor);
while (type.getSort() == Type.ARRAY) {
type = type.getElementType();
}
if (type.getSort() != Type.OBJECT) {
// A primitive type
continue;
}
String className = type.getClassName();
if (className.equals(target.getName())) {
// A reference to this class
continue;
}
Class<?> cl;
try {
cl = Class.forName(className, false, targetClassLoader);
} catch (ClassNotFoundException e) {
// This is fine, just ignore it
LOGGER.warn("Could not determine classpath for {}", target);
continue;
}
find(cl, visited, dest);
}
}
} catch (Exception e) {
throw new GradleException(String.format("Could not determine the class-path for %s.", target), e);
}
}
use of org.objectweb.asm.Type in project groovy-core by groovy.
the class AsmClassGenerator method visitAnnotationDefaultExpression.
void visitAnnotationDefaultExpression(AnnotationVisitor av, ClassNode type, Expression exp) {
if (exp instanceof ClosureExpression) {
ClassNode closureClass = controller.getClosureWriter().getOrAddClosureClass((ClosureExpression) exp, ACC_PUBLIC);
Type t = Type.getType(BytecodeHelper.getTypeDescription(closureClass));
av.visit(null, t);
} else if (type.isArray()) {
ListExpression list = (ListExpression) exp;
AnnotationVisitor avl = av.visitArray(null);
ClassNode componentType = type.getComponentType();
for (Expression lExp : list.getExpressions()) {
visitAnnotationDefaultExpression(avl, componentType, lExp);
}
} else if (ClassHelper.isPrimitiveType(type) || type.equals(ClassHelper.STRING_TYPE)) {
ConstantExpression constExp = (ConstantExpression) exp;
av.visit(null, constExp.getValue());
} else if (ClassHelper.CLASS_Type.equals(type)) {
ClassNode clazz = exp.getType();
Type t = Type.getType(BytecodeHelper.getTypeDescription(clazz));
av.visit(null, t);
} else if (type.isDerivedFrom(ClassHelper.Enum_Type)) {
PropertyExpression pExp = (PropertyExpression) exp;
ClassExpression cExp = (ClassExpression) pExp.getObjectExpression();
String desc = BytecodeHelper.getTypeDescription(cExp.getType());
String name = pExp.getPropertyAsString();
av.visitEnum(null, desc, name);
} else if (type.implementsInterface(ClassHelper.Annotation_TYPE)) {
AnnotationConstantExpression avExp = (AnnotationConstantExpression) exp;
AnnotationNode value = (AnnotationNode) avExp.getValue();
AnnotationVisitor avc = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(avExp.getType()));
visitAnnotationAttributes(value, avc);
} else {
throw new GroovyBugError("unexpected annotation type " + type.getName());
}
av.visitEnd();
}
use of org.objectweb.asm.Type in project gradle by gradle.
the class ManagedCollectionProxyClassGenerator method generateConstructors.
private <T> void generateConstructors(ClassWriter visitor, Class<? extends T> implClass, Type superclassType) {
for (Constructor<?> constructor : implClass.getConstructors()) {
Type[] paramTypes = new Type[constructor.getParameterTypes().length];
for (int i = 0; i < paramTypes.length; i++) {
paramTypes[i] = Type.getType(constructor.getParameterTypes()[i]);
}
String methodDescriptor = Type.getMethodDescriptor(Type.VOID_TYPE, paramTypes);
MethodVisitor constructorVisitor = visitor.visitMethod(Opcodes.ACC_PUBLIC, CONSTRUCTOR_NAME, methodDescriptor, CONCRETE_SIGNATURE, NO_EXCEPTIONS);
constructorVisitor.visitCode();
putThisOnStack(constructorVisitor);
for (int i = 0; i < paramTypes.length; i++) {
constructorVisitor.visitVarInsn(paramTypes[i].getOpcode(Opcodes.ILOAD), i + 1);
}
constructorVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, superclassType.getInternalName(), CONSTRUCTOR_NAME, methodDescriptor, false);
finishVisitingMethod(constructorVisitor);
}
}
Aggregations