Search in sources :

Example 36 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project dex2jar by pxb1988.

the class Dex2Asm method isSignatureNotValid.

/**
 * @see org.objectweb.asm.commons.Remapper#mapSignature(String, boolean)
 */
private static boolean isSignatureNotValid(String signature, boolean typeSignature) {
    if (signature == null) {
        return false;
    }
    try {
        SignatureReader r = new SignatureReader(signature);
        SignatureWriter a = new SignatureWriter();
        if (typeSignature) {
            r.acceptType(a);
        } else {
            r.accept(a);
        }
    } catch (Exception ignore) {
        return true;
    }
    return false;
}
Also used : SignatureReader(org.objectweb.asm.signature.SignatureReader) SignatureWriter(org.objectweb.asm.signature.SignatureWriter) IOException(java.io.IOException)

Example 37 with SignatureReader

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

the class Remapper method mapSignature.

/**
 * @param typeSignature true if signature is a FieldTypeSignature, such as the
 *                      signature parameter of the ClassVisitor.visitField or
 *                      MethodVisitor.visitLocalVariable methods
 */
@Override
public String mapSignature(String signature, boolean typeSignature) {
    if (signature == null) {
        return null;
    }
    SignatureReader r = new SignatureReader(signature);
    SignatureWriter w = new SignatureWriter();
    SignatureVisitor a = createSignatureRemapper(w);
    if (typeSignature) {
        r.acceptType(a);
    } else {
        r.accept(a);
    }
    return w.toString();
}
Also used : SignatureReader(org.objectweb.asm.signature.SignatureReader) SignatureVisitor(org.objectweb.asm.signature.SignatureVisitor) SignatureWriter(org.objectweb.asm.signature.SignatureWriter)

Example 38 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project groovy by apache.

the class MemberSignatureParser method createMethodNode.

static MethodNode createMethodNode(final AsmReferenceResolver resolver, final MethodStub method) {
    GenericsType[] typeParameters = null;
    ClassNode[] returnType = resolve(resolver, Type.getReturnType(method.desc));
    ClassNode[] parameterTypes = resolve(resolver, Type.getArgumentTypes(method.desc));
    ClassNode[] exceptionTypes = resolve(resolver, method.exceptions);
    if (method.signature != null) {
        FormalParameterParser parser = new FormalParameterParser(resolver) {

            private int exceptionIndex, parameterIndex;

            @Override
            public SignatureVisitor visitReturnType() {
                return new TypeSignatureParser(resolver) {

                    @Override
                    void finished(final ClassNode result) {
                        returnType[0] = applyErasure(result, returnType[0]);
                    }
                };
            }

            @Override
            public SignatureVisitor visitParameterType() {
                return new TypeSignatureParser(resolver) {

                    @Override
                    void finished(final ClassNode result) {
                        parameterTypes[parameterIndex] = applyErasure(result, parameterTypes[parameterIndex]);
                        parameterIndex += 1;
                    }
                };
            }

            @Override
            public SignatureVisitor visitExceptionType() {
                return new TypeSignatureParser(resolver) {

                    @Override
                    void finished(final ClassNode result) {
                        exceptionTypes[exceptionIndex] = applyErasure(result, exceptionTypes[exceptionIndex]);
                        exceptionIndex += 1;
                    }
                };
            }
        };
        new SignatureReader(method.signature).accept(parser);
        typeParameters = parser.getTypeParameters();
    } else {
        returnType[0] = GenericsUtils.nonGeneric(returnType[0]);
        for (int i = 0, n = parameterTypes.length; i < n; i += 1) {
            parameterTypes[i] = GenericsUtils.nonGeneric(parameterTypes[i]);
        }
    }
    int nParameters = parameterTypes.length;
    Parameter[] parameters = new Parameter[nParameters];
    List<String> parameterNames = method.parameterNames;
    for (int i = 0; i < nParameters; i += 1) {
        String parameterName = "param" + i;
        if (parameterNames != null && i < parameterNames.size()) {
            String decompiledName = parameterNames.get(i);
            if (decompiledName != null) {
                parameterName = decompiledName;
            }
        }
        parameters[i] = new Parameter(parameterTypes[i], parameterName);
    }
    if (method.parameterAnnotations != null) {
        for (Map.Entry<Integer, List<AnnotationStub>> entry : method.parameterAnnotations.entrySet()) {
            for (AnnotationStub stub : entry.getValue()) {
                AnnotationNode annotationNode = Annotations.createAnnotationNode(stub, resolver);
                if (annotationNode != null) {
                    parameters[entry.getKey()].addAnnotation(annotationNode);
                }
            }
        }
    }
    MethodNode result;
    if ("<init>".equals(method.methodName)) {
        result = new ConstructorNode(method.accessModifiers, parameters, exceptionTypes, null);
    } else {
        result = new MethodNode(method.methodName, method.accessModifiers, returnType[0], parameters, exceptionTypes, null);
        Object annDefault = method.annotationDefault;
        if (annDefault != null) {
            if (annDefault instanceof TypeWrapper) {
                annDefault = resolver.resolveType(Type.getType(((TypeWrapper) annDefault).desc));
            }
            result.setCode(new ReturnStatement(new ConstantExpression(annDefault)));
            result.setAnnotationDefault(true);
        } else {
            // seems wrong but otherwise some tests fail (e.g. TestingASTTransformsTest)
            result.setCode(new ReturnStatement(new ConstantExpression(null)));
        }
    }
    if (typeParameters != null && typeParameters.length > 0) {
        result.setGenericsTypes(typeParameters);
    }
    return result;
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ConstantExpression(org.codehaus.groovy.ast.expr.ConstantExpression) MethodNode(org.codehaus.groovy.ast.MethodNode) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) ConstructorNode(org.codehaus.groovy.ast.ConstructorNode) GenericsType(org.codehaus.groovy.ast.GenericsType) SignatureReader(org.objectweb.asm.signature.SignatureReader) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) Parameter(org.codehaus.groovy.ast.Parameter) List(java.util.List) Map(java.util.Map)

Example 39 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project groovy by apache.

the class ClassSignatureParser method parseClassSignature.

private static void parseClassSignature(final ClassNode classNode, String signature, final AsmReferenceResolver resolver) {
    final List<ClassNode> interfaces = new ArrayList<ClassNode>();
    FormalParameterParser v = new FormalParameterParser(resolver) {

        @Override
        public SignatureVisitor visitSuperclass() {
            flushTypeParameter();
            return new TypeSignatureParser(resolver) {

                @Override
                void finished(ClassNode result) {
                    classNode.setSuperClass(result);
                }
            };
        }

        @Override
        public SignatureVisitor visitInterface() {
            flushTypeParameter();
            return new TypeSignatureParser(resolver) {

                @Override
                void finished(ClassNode result) {
                    interfaces.add(result);
                }
            };
        }
    };
    new SignatureReader(signature).accept(v);
    GenericsType[] typeParameters = v.getTypeParameters();
    if (typeParameters.length > 0) {
        classNode.setGenericsTypes(typeParameters);
    }
    classNode.setInterfaces(interfaces.toArray(ClassNode.EMPTY_ARRAY));
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ArrayList(java.util.ArrayList) SignatureReader(org.objectweb.asm.signature.SignatureReader) GenericsType(org.codehaus.groovy.ast.GenericsType)

Example 40 with SignatureReader

use of org.objectweb.asm.signature.SignatureReader in project groovy by apache.

the class ClassSignatureParser method configureClass.

static void configureClass(ClassNode classNode, ClassStub stub, AsmReferenceResolver resolver) {
    if (stub.signature != null) {
        parseClassSignature(classNode, stub.signature, resolver);
        return;
    }
    if (stub.superName != null) {
        classNode.setSuperClass(resolver.resolveClass(AsmDecompiler.fromInternalName(stub.superName)));
    }
    ClassNode[] interfaces = new ClassNode[stub.interfaceNames.length];
    for (int i = 0; i < stub.interfaceNames.length; i++) {
        interfaces[i] = resolver.resolveClass(AsmDecompiler.fromInternalName(stub.interfaceNames[i]));
    }
    classNode.setInterfaces(interfaces);
    if (!stub.permittedSubclasses.isEmpty()) {
        List<ClassNode> permittedSubclasses = classNode.getPermittedSubclasses();
        for (String name : stub.permittedSubclasses) {
            permittedSubclasses.add(resolver.resolveClass(AsmDecompiler.fromInternalName(name)));
        }
    }
    if (!stub.recordComponents.isEmpty()) {
        classNode.setRecordComponents(stub.recordComponents.stream().map(r -> {
            ClassNode type = resolver.resolveType(Type.getType(r.descriptor));
            ObjectHolder<ClassNode> typeHolder = new ObjectHolder<>(type);
            if (null != r.signature) {
                new SignatureReader(r.signature).accept(new TypeSignatureParser(resolver) {

                    @Override
                    void finished(final ClassNode result) {
                        typeHolder.setObject(applyErasure(result, typeHolder.getObject()));
                    }
                });
            }
            ClassNode cn = typeHolder.getObject();
            Annotations.addTypeAnnotations(r, cn, resolver);
            RecordComponentNode recordComponentNode = new RecordComponentNode(classNode, r.name, cn);
            Annotations.addAnnotations(r, recordComponentNode, resolver);
            return recordComponentNode;
        }).collect(Collectors.toList()));
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) ObjectHolder(org.apache.groovy.util.ObjectHolder) RecordComponentNode(org.codehaus.groovy.ast.RecordComponentNode) SignatureReader(org.objectweb.asm.signature.SignatureReader)

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