Search in sources :

Example 6 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project bytecode-viewer by Konloch.

the class Textifier method visitLocalVariable.

@Override
public void visitLocalVariable(final String name, final String desc, final String signature, final Label start, final Label end, final int index) {
    buf.setLength(0);
    buf.append(tab2).append("LOCALVARIABLE ").append(name).append(' ');
    appendDescriptor(FIELD_DESCRIPTOR, desc);
    buf.append(' ');
    appendLabel(start);
    buf.append(' ');
    appendLabel(end);
    buf.append(' ').append(index).append('\n');
    if (signature != null) {
        buf.append(tab2);
        appendDescriptor(FIELD_SIGNATURE, signature);
        TraceSignatureVisitor sv = new TraceSignatureVisitor(0);
        SignatureReader r = new SignatureReader(signature);
        r.acceptType(sv);
        buf.append(tab2).append("// declaration: ").append(sv.getDeclaration()).append('\n');
    }
    text.add(buf.toString());
}
Also used : SignatureReader(org.objectweb.asm.signature.SignatureReader)

Example 7 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project sonar-java by SonarSource.

the class BytecodeVisitor method visitMethod.

@Override
public MethodVisitor visitMethod(int flags, String name, String desc, @Nullable String signature, @Nullable String[] exceptions) {
    Preconditions.checkNotNull(name);
    Preconditions.checkNotNull(desc);
    if (isNotSynthetic(flags)) {
        if ((flags & Opcodes.ACC_BRIDGE) != 0) {
            LOG.warn("bridge method {} not marked as synthetic in class {}", name, className);
            return null;
        }
        // TODO(Godin): according to JVMS 4.7.24 - parameter can be marked as synthetic
        MethodJavaType type = new MethodJavaType(convertAsmTypes(org.objectweb.asm.Type.getArgumentTypes(desc)), convertAsmType(org.objectweb.asm.Type.getReturnType(desc)), getCompletedClassSymbolsType(exceptions), classSymbol);
        int methodFlags = Flags.filterAccessBytecodeFlags(flags);
        if (classSymbol.isInterface() && Flags.isNotFlagged(methodFlags, Flags.ABSTRACT | Flags.PRIVATE | Flags.STATIC)) {
            // abstract, static nor private method of interface is a default method
            methodFlags |= Flags.DEFAULT;
        }
        final JavaSymbol.MethodJavaSymbol methodSymbol = new JavaSymbol.MethodJavaSymbol(methodFlags, name, type, classSymbol);
        methodSymbol.desc = desc;
        classSymbol.members.enter(methodSymbol);
        if (signature != null) {
            SignatureReader signatureReader = new SignatureReader(signature);
            signatureReader.accept(new TypeParameterDeclaration(methodSymbol));
            signatureReader.accept(new ReadMethodSignature(methodSymbol));
        }
        methodSymbol.parameters = new Scope(methodSymbol);
        for (int i = 0; i < type.argTypes.size(); i += 1) {
            methodSymbol.parameters.enter(new JavaSymbol.VariableJavaSymbol(0, "arg" + i, methodSymbol));
        }
        // checks for annotations on the method and its parameters
        return new BytecodeMethodVisitor(methodSymbol, this);
    }
    return null;
}
Also used : SignatureReader(org.objectweb.asm.signature.SignatureReader)

Example 8 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project sonar-java by SonarSource.

the class BytecodeVisitor method visit.

@Override
public void visit(int version, int flags, String name, @Nullable String signature, @Nullable String superName, @Nullable String[] interfaces) {
    Preconditions.checkState(name.endsWith(classSymbol.name), "Name : '%s' should ends with %s", name, classSymbol.name);
    Preconditions.checkState(name.endsWith("package-info") || isNotSynthetic(flags), "%s is synthetic", name);
    className = name;
    if (signature != null) {
        SignatureReader signatureReader = new SignatureReader(signature);
        signatureReader.accept(new TypeParameterDeclaration(classSymbol));
        ReadGenericSignature readGenericSignature = new ReadGenericSignature();
        signatureReader.accept(readGenericSignature);
        ((ClassJavaType) classSymbol.type).interfaces = readGenericSignature.interfaces();
    } else {
        if (superName == null) {
            Preconditions.checkState("java/lang/Object".equals(className), "superName must be null only for java/lang/Object, but not for %s", className);
        // TODO(Godin): what about interfaces and annotations
        } else {
            ((ClassJavaType) classSymbol.type).supertype = getClassSymbol(superName).type;
        }
        ((ClassJavaType) classSymbol.type).interfaces = getCompletedClassSymbolsType(interfaces);
    }
    // The important access flags are the one defined in the outer class.
    if ((classSymbol.flags & Flags.ACCESS_FLAGS) != 0) {
        classSymbol.flags |= Flags.filterAccessBytecodeFlags(flags & ~Flags.ACCESS_FLAGS);
    } else {
        classSymbol.flags |= Flags.filterAccessBytecodeFlags(flags);
    }
    classSymbol.members = new Scope(classSymbol);
}
Also used : SignatureReader(org.objectweb.asm.signature.SignatureReader)

Example 9 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project evosuite by EvoSuite.

the class CastClassAnalyzer method handleMethodNode.

/**
 * Add all possible calls for a given method
 *
 * @param mn
 */
@SuppressWarnings("unchecked")
public void handleMethodNode(ClassNode cn, MethodNode mn, int depth) {
    if (mn.signature != null) {
        logger.debug("Visiting signature: " + mn.signature);
        CollectParameterTypesVisitor visitor = new CollectParameterTypesVisitor(cn.name);
        new SignatureReader(mn.signature).accept(visitor);
        for (Type castType : visitor.getClasses()) {
            if (!castClassMap.containsKey(castType)) {
                logger.debug("Adding new cast class from signature visitor: " + castType);
                castClassMap.put(castType, depth + 1);
            }
        }
    }
    InsnList instructions = mn.instructions;
    Iterator<AbstractInsnNode> iterator = instructions.iterator();
    // TODO: This really shouldn't be here but in its own class
    while (iterator.hasNext()) {
        AbstractInsnNode insn = iterator.next();
        if (insn.getOpcode() == Opcodes.CHECKCAST) {
            TypeInsnNode typeNode = (TypeInsnNode) insn;
            Type castType = Type.getObjectType(typeNode.desc);
            while (castType.getSort() == Type.ARRAY) {
                castType = castType.getElementType();
            }
            logger.debug("Adding new cast class from cast: " + castType);
            if (!castClassMap.containsKey(castType))
                castClassMap.put(castType, depth + 1);
        } else if (insn.getOpcode() == Opcodes.INSTANCEOF) {
            TypeInsnNode typeNode = (TypeInsnNode) insn;
            Type castType = Type.getObjectType(typeNode.desc);
            while (castType.getSort() == Type.ARRAY) {
                castType = castType.getElementType();
            }
            logger.debug("Adding new cast class from instanceof: " + castType);
            if (!castClassMap.containsKey(castType))
                castClassMap.put(castType, depth + 1);
        } else if (insn.getOpcode() == Opcodes.LDC) {
            LdcInsnNode ldcNode = (LdcInsnNode) insn;
            if (ldcNode.cst instanceof Type) {
                Type type = (Type) ldcNode.cst;
                while (type.getSort() == Type.ARRAY) {
                    type = type.getElementType();
                }
                if (!castClassMap.containsKey(type))
                    castClassMap.put(type, depth + 1);
            }
        }
    }
}
Also used : Type(org.objectweb.asm.Type) LdcInsnNode(org.objectweb.asm.tree.LdcInsnNode) SignatureReader(org.objectweb.asm.signature.SignatureReader) TypeInsnNode(org.objectweb.asm.tree.TypeInsnNode) InsnList(org.objectweb.asm.tree.InsnList) AbstractInsnNode(org.objectweb.asm.tree.AbstractInsnNode)

Example 10 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project pmd by pmd.

the class ConstructorNode method getMember.

@Override
public Constructor<?> getMember() {
    if (ClassLoaderUtil.CLINIT.equals(name)) {
        return null;
    } else {
        Constructor<?> constructor = constructorReference == null ? null : constructorReference.get();
        if (constructor == null) {
            SignatureReader signatureReader = new SignatureReader(desc);
            TypeSignatureVisitor visitor = new TypeSignatureVisitor();
            signatureReader.accept(visitor);
            constructor = ClassLoaderUtil.getConstructor(super.getClassNode().getType(), name, visitor.getMethodParameterTypes());
            constructorReference = new WeakReference<Constructor<?>>(constructor);
        }
        return constructor;
    }
}
Also used : Constructor(java.lang.reflect.Constructor) SignatureReader(org.objectweb.asm.signature.SignatureReader) TypeSignatureVisitor(net.sourceforge.pmd.dcd.asm.TypeSignatureVisitor)

Aggregations

SignatureReader (org.objectweb.asm.signature.SignatureReader)41 SignatureWriter (org.objectweb.asm.signature.SignatureWriter)12 ArrayList (java.util.ArrayList)5 ClassNode (org.codehaus.groovy.ast.ClassNode)4 GenericsType (org.codehaus.groovy.ast.GenericsType)3 Type (org.objectweb.asm.Type)3 SignatureVisitor (org.objectweb.asm.signature.SignatureVisitor)3 BoundDescriptor (com.buschmais.jqassistant.plugin.java.api.model.generics.BoundDescriptor)2 AbstractBoundVisitor (com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.generics.AbstractBoundVisitor)2 List (java.util.List)2 Map (java.util.Map)2 TypeSignatureVisitor (net.sourceforge.pmd.dcd.asm.TypeSignatureVisitor)2 EntryMessage (org.apache.logging.log4j.message.EntryMessage)2 ConstantExpression (org.codehaus.groovy.ast.expr.ConstantExpression)2 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)2 DelegatingMethodVisitor (com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.delegate.DelegatingMethodVisitor)1 ClassSignatureVisitor (com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.generics.ClassSignatureVisitor)1 MethodSignatureVisitor (com.buschmais.jqassistant.plugin.java.impl.scanner.visitor.generics.MethodSignatureVisitor)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1