001/*
002 * Copyright 2010-2019 The jdependency developers.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.vafer.jdependency.asm;
017
018import java.util.HashSet;
019import java.util.Set;
020import java.io.File;
021
022import org.objectweb.asm.AnnotationVisitor;
023import org.objectweb.asm.ClassVisitor;
024import org.objectweb.asm.FieldVisitor;
025import org.objectweb.asm.Label;
026import org.objectweb.asm.MethodVisitor;
027import org.objectweb.asm.Opcodes;
028import org.objectweb.asm.TypePath;
029import org.objectweb.asm.commons.ClassRemapper;
030import org.objectweb.asm.commons.Remapper;
031
032/**
033 * interal - do not use
034 */
035
036public final class DependenciesClassAdapter extends ClassRemapper {
037
038    private static final EmptyVisitor ev = new EmptyVisitor();
039
040    public DependenciesClassAdapter() {
041        super(ev, new CollectingRemapper());
042    }
043
044    public Set<String> getDependencies() {
045        return ((CollectingRemapper) super.remapper).classes;
046    }
047
048    private static class CollectingRemapper extends Remapper {
049
050        final Set<String> classes = new HashSet<String>();
051
052        public String map(String pClassName) {
053            classes.add(pClassName.replace('/', '.'));
054            return pClassName;
055        }
056    }
057
058    static class EmptyVisitor extends ClassVisitor {
059
060        private static final AnnotationVisitor av = new AnnotationVisitor(Opcodes.ASM7) {
061            @Override
062            public AnnotationVisitor visitAnnotation(String name, String desc) {
063                return this;
064            }
065
066            @Override
067            public AnnotationVisitor visitArray(String name) {
068                return this;
069            }
070        };
071
072        private static final MethodVisitor mv = new MethodVisitor( Opcodes.ASM6) {
073            @Override
074            public AnnotationVisitor visitAnnotationDefault() {
075                return av;
076            }
077
078            @Override
079            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
080                return av;
081            }
082
083            @Override
084            public AnnotationVisitor visitParameterAnnotation(
085                int parameter, String desc, boolean visible) {
086                return av;
087            }
088
089            @Override
090            public AnnotationVisitor visitInsnAnnotation( int typeRef, TypePath typePath, String descriptor,
091                                                          boolean visible ) {
092                return av;
093            }
094
095            @Override
096            public AnnotationVisitor visitLocalVariableAnnotation( int typeRef, TypePath typePath, Label[] start,
097                                                                   Label[] end, int[] index, String descriptor,
098                                                                   boolean visible ) {
099                return av;
100            }
101
102            @Override
103            public AnnotationVisitor visitTryCatchAnnotation( int typeRef, TypePath typePath, String descriptor,
104                                                              boolean visible ) {
105                return av;
106            }
107
108            @Override
109            public AnnotationVisitor visitTypeAnnotation( int typeRef, TypePath typePath, String descriptor,
110                                                          boolean visible ) {
111                return av;
112            }
113        };
114
115        private static final FieldVisitor fieldVisitor = new FieldVisitor( Opcodes.ASM7 ) {
116            @Override
117            public AnnotationVisitor visitAnnotation( String desc, boolean visible ) {
118                return av;
119            }
120            @Override
121            public AnnotationVisitor visitTypeAnnotation( int typeRef, TypePath typePath, String descriptor,
122                                                          boolean visible ) {
123                return av;
124            }
125        };
126
127        public EmptyVisitor() {
128            super(Opcodes.ASM7);
129        }
130
131        @Override
132        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
133            return av;
134        }
135
136        @Override
137        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
138            return fieldVisitor;
139        }
140
141        @Override
142        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
143            return mv;
144        }
145
146        @Override
147        public AnnotationVisitor visitTypeAnnotation( int typeRef, TypePath typePath, String descriptor,
148                                                      boolean visible ) {
149            return av;
150        }
151    }
152}