Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  // CHECKSTYLE:OFF
ASM examples: examples showing how ASM can be used Copyright (c) 2000-2007 INRIA, France Telecom All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
 package org.fuin.units4j.dependency;
 
 import java.util.Map;
 import java.util.Set;
 
 import  org.objectweb.asm.AnnotationVisitor;
 import  org.objectweb.asm.Attribute;
 import  org.objectweb.asm.ClassVisitor;
 import  org.objectweb.asm.FieldVisitor;
 import  org.objectweb.asm.Label;
 import  org.objectweb.asm.MethodVisitor;
 import  org.objectweb.asm.Type;
 import  org.objectweb.asm.signature.SignatureReader;
 import  org.objectweb.asm.signature.SignatureVisitor;

DependencyVisitor

Author(s):
Eugene Kuleshov
 
 public class DependencyVisitor implements AnnotationVisitor, SignatureVisitor, ClassVisitor,
         FieldVisitor, MethodVisitor {
     Set<Stringpackages = new HashSet<String>();
 
     Map<StringMap<StringInteger>> groups = new HashMap<StringMap<StringInteger>>();
 
     Map<StringIntegercurrent;
 
     public Map<StringMap<StringInteger>> getGlobals() {
         return ;
     }
 
     public Set<StringgetPackages() {
         return ;
     }
 
     // ClassVisitor
 
     public void visit(final int versionfinal int accessfinal String name,
             final String signaturefinal String superNamefinal String[] interfaces) {
         String p = getGroupKey(name);
          = .get(p);
         if ( == null) {
              = new HashMap<StringInteger>();
             .put(p);
         }
 
         if (signature == null) {
             addInternalName(superName);
             addInternalNames(interfaces);
         } else {
             addSignature(signature);
         }
     }
 
     public AnnotationVisitor visitAnnotation(final String descfinal boolean visible) {
         addDesc(desc);
         return this;
     }
 
     public void visitAttribute(final Attribute attr) {
     }
 
     public FieldVisitor visitField(final int accessfinal String namefinal String desc,
             final String signaturefinal Object value) {
         if (signature == null) {
             addDesc(desc);
        } else {
            addTypeSignature(signature);
        }
        if (value instanceof Type) {
            addType((Type) value);
        }
        return this;
    }
    public MethodVisitor visitMethod(final int accessfinal String namefinal String desc,
            final String signaturefinal String[] exceptions) {
        if (signature == null) {
            addMethodDesc(desc);
        } else {
            addSignature(signature);
        }
        addInternalNames(exceptions);
        return this;
    }
    public void visitSource(final String sourcefinal String debug) {
    }
    public void visitInnerClass(final String namefinal String outerNamefinal String innerName,
            final int access) {
        // addName( outerName);
        // addName( innerName);
    }
    public void visitOuterClass(final String ownerfinal String namefinal String desc) {
        // addName(owner);
        // addMethodDesc(desc);
    }
    // MethodVisitor
    public AnnotationVisitor visitParameterAnnotation(final int parameterfinal String desc,
            final boolean visible) {
        addDesc(desc);
        return this;
    }
    public void visitTypeInsn(final int opcodefinal String type) {
        addType(Type.getObjectType(type));
    }
    public void visitFieldInsn(final int opcodefinal String ownerfinal String name,
            final String desc) {
        addInternalName(owner);
        addDesc(desc);
    }
    public void visitMethodInsn(final int opcodefinal String ownerfinal String name,
            final String desc) {
        addInternalName(owner);
        addMethodDesc(desc);
    }
    public void visitLdcInsn(final Object cst) {
        if (cst instanceof Type) {
            addType((Type) cst);
        }
    }
    public void visitMultiANewArrayInsn(final String descfinal int dims) {
        addDesc(desc);
    }
    public void visitLocalVariable(final String namefinal String descfinal String signature,
            final Label startfinal Label endfinal int index) {
        addTypeSignature(signature);
    }
    public AnnotationVisitor visitAnnotationDefault() {
        return this;
    }
    public void visitCode() {
    }
    public void visitFrame(final int typefinal int nLocalfinal Object[] local,
            final int nStackfinal Object[] stack) {
    }
    public void visitInsn(final int opcode) {
    }
    public void visitIntInsn(final int opcodefinal int operand) {
    }
    public void visitVarInsn(final int opcodefinal int var) {
    }
    public void visitJumpInsn(final int opcodefinal Label label) {
    }
    public void visitLabel(final Label label) {
    }
    public void visitIincInsn(final int varfinal int increment) {
    }
    public void visitTableSwitchInsn(final int minfinal int maxfinal Label dflt,
            final Label[] labels) {
    }
    public void visitLookupSwitchInsn(final Label dfltfinal int[] keysfinal Label[] labels) {
    }
    public void visitTryCatchBlock(final Label startfinal Label endfinal Label handler,
            final String type) {
        addInternalName(type);
    }
    public void visitLineNumber(final int linefinal Label start) {
    }
    public void visitMaxs(final int maxStackfinal int maxLocals) {
    }
    // AnnotationVisitor
    public void visit(final String namefinal Object value) {
        if (value instanceof Type) {
            addType((Type) value);
        }
    }
    public void visitEnum(final String namefinal String descfinal String value) {
        addDesc(desc);
    }
    public AnnotationVisitor visitAnnotation(final String namefinal String desc) {
        addDesc(desc);
        return this;
    }
    public AnnotationVisitor visitArray(final String name) {
        return this;
    }
    // SignatureVisitor
    public void visitFormalTypeParameter(final String name) {
    }
    public SignatureVisitor visitClassBound() {
        return this;
    }
    public SignatureVisitor visitInterfaceBound() {
        return this;
    }
    public SignatureVisitor visitSuperclass() {
        return this;
    }
    public SignatureVisitor visitInterface() {
        return this;
    }
    public SignatureVisitor visitParameterType() {
        return this;
    }
    public SignatureVisitor visitReturnType() {
        return this;
    }
    public SignatureVisitor visitExceptionType() {
        return this;
    }
    public void visitBaseType(final char descriptor) {
    }
    public void visitTypeVariable(final String name) {
    }
    public SignatureVisitor visitArrayType() {
        return this;
    }
    public void visitClassType(final String name) {
         = name;
        addInternalName(name);
    }
    public void visitInnerClassType(final String name) {
         =  + "$" + name;
    }
    public void visitTypeArgument() {
    }
    public SignatureVisitor visitTypeArgument(final char wildcard) {
        return this;
    }
    // common
    public void visitEnd() {
    }
    // ---------------------------------------------
    private String getGroupKey(String name) {
        int n = name.lastIndexOf('/');
        if (n > -1) {
            name = name.substring(0, n);
        }
        .add(name);
        return name;
    }
    private void addName(final String name) {
        if (name == null) {
            return;
        }
        String p = getGroupKey(name);
        if (.containsKey(p)) {
            .put(p.get(p) + 1);
        } else {
            .put(p, 1);
        }
    }
    private void addInternalName(final String name) {
        if (name != null) {
            addType(Type.getObjectType(name));
        }
    }
    private void addInternalNames(final String[] names) {
        for (int i = 0; names != null && i < names.lengthi++) {
            addInternalName(names[i]);
        }
    }
    private void addDesc(final String desc) {
        addType(Type.getType(desc));
    }
    private void addMethodDesc(final String desc) {
        addType(Type.getReturnType(desc));
        Type[] types = Type.getArgumentTypes(desc);
        for (int i = 0; i < types.length; i++) {
            addType(types[i]);
        }
    }
    private void addType(final Type t) {
        switch (t.getSort()) {
            case Type.ARRAY:
                addType(t.getElementType());
                break;
            case Type.OBJECT:
                addName(t.getInternalName());
                break;
        }
    }
    private void addSignature(final String signature) {
        if (signature != null) {
            new SignatureReader(signature).accept(this);
        }
    }
    private void addTypeSignature(final String signature) {
        if (signature != null) {
            new SignatureReader(signature).acceptType(this);
        }
    }
//CHECKSTYLE:ON
New to GrepCode? Check out our FAQ X