001/*
002 * Copyright 2010-2018 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;
020
021import org.objectweb.asm.AnnotationVisitor;
022import org.objectweb.asm.ClassVisitor;
023import org.objectweb.asm.FieldVisitor;
024import org.objectweb.asm.MethodVisitor;
025import org.objectweb.asm.Opcodes;
026import org.objectweb.asm.commons.ClassRemapper;
027import org.objectweb.asm.commons.Remapper;
028
029public final class DependenciesClassAdapter extends ClassRemapper {
030
031    private static final EmptyVisitor ev = new EmptyVisitor();
032
033    public DependenciesClassAdapter() {
034        super(ev, new CollectingRemapper());
035    }
036
037    public Set<String> getDependencies() {
038        return ((CollectingRemapper) super.remapper).classes;
039    }
040
041    private static class CollectingRemapper extends Remapper {
042
043        final Set<String> classes = new HashSet<String>();
044
045        public String map(String pClassName) {
046            classes.add(pClassName.replace('/', '.'));
047            return pClassName;
048        }
049    }
050
051    static class EmptyVisitor extends ClassVisitor {
052
053        private static final AnnotationVisitor av = new AnnotationVisitor(Opcodes.ASM5) {
054
055            public AnnotationVisitor visitAnnotation(String name, String desc) {
056                return this;
057            }
058
059            public AnnotationVisitor visitArray(String name) {
060                return this;
061            }
062        };
063
064        private static final MethodVisitor mv = new MethodVisitor( Opcodes.ASM5) {
065
066            public AnnotationVisitor visitAnnotationDefault() {
067                return av;
068            }
069
070            public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
071                return av;
072            }
073
074            public AnnotationVisitor visitParameterAnnotation(
075                int parameter, String desc, boolean visible) {
076                return av;
077            }
078        };
079
080        private static final FieldVisitor fieldVisitor = new FieldVisitor( Opcodes.ASM5 ) {
081            public AnnotationVisitor visitAnnotation( String desc, boolean visible ) {
082                return av;
083            }
084        };
085
086        public EmptyVisitor() {
087            super(Opcodes.ASM5);
088        }
089
090        public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
091            return av;
092        }
093
094        public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
095            return fieldVisitor;
096        }
097
098        public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
099            return mv;
100        }
101    }
102}