001/* 002 * Copyright 2010-2024 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.Label; 025import org.objectweb.asm.MethodVisitor; 026import org.objectweb.asm.ModuleVisitor; 027import org.objectweb.asm.Opcodes; 028import org.objectweb.asm.RecordComponentVisitor; 029import org.objectweb.asm.TypePath; 030import org.objectweb.asm.commons.ClassRemapper; 031import org.objectweb.asm.commons.Remapper; 032 033/** 034 * internal - do not use 035 */ 036 037public final class DependenciesClassAdapter extends ClassRemapper { 038 039 private static final int OPCODES = Opcodes.ASM9; 040 041 private static final EmptyVisitor ev = new EmptyVisitor(); 042 043 public DependenciesClassAdapter() { 044 super(ev, new CollectingRemapper()); 045 } 046 047 public Set<String> getDependencies() { 048 return ((CollectingRemapper) super.remapper).classes; 049 } 050 051 private static class CollectingRemapper extends Remapper { 052 053 final Set<String> classes = new HashSet<String>(); 054 055 public String map(String pClassName) { 056 classes.add(pClassName.replace('/', '.')); 057 return pClassName; 058 } 059 } 060 061 static class EmptyVisitor extends ClassVisitor { 062 063 private static final AnnotationVisitor av = new AnnotationVisitor(OPCODES) { 064 @Override 065 public AnnotationVisitor visitAnnotation(String name, String desc) { 066 return this; 067 } 068 069 @Override 070 public AnnotationVisitor visitArray(String name) { 071 return this; 072 } 073 }; 074 075 private static final MethodVisitor mv = new MethodVisitor(OPCODES) { 076 @Override 077 public AnnotationVisitor visitAnnotationDefault() { 078 return av; 079 } 080 081 @Override 082 public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 083 return av; 084 } 085 086 @Override 087 public AnnotationVisitor visitParameterAnnotation( 088 int parameter, String desc, boolean visible) { 089 return av; 090 } 091 092 @Override 093 public AnnotationVisitor visitInsnAnnotation( int typeRef, TypePath typePath, String descriptor, 094 boolean visible ) { 095 return av; 096 } 097 098 @Override 099 public AnnotationVisitor visitLocalVariableAnnotation( int typeRef, TypePath typePath, Label[] start, 100 Label[] end, int[] index, String descriptor, 101 boolean visible ) { 102 return av; 103 } 104 105 @Override 106 public AnnotationVisitor visitTryCatchAnnotation( int typeRef, TypePath typePath, String descriptor, 107 boolean visible ) { 108 return av; 109 } 110 111 @Override 112 public AnnotationVisitor visitTypeAnnotation( int typeRef, TypePath typePath, String descriptor, 113 boolean visible ) { 114 return av; 115 } 116 }; 117 118 private static final FieldVisitor fieldVisitor = new FieldVisitor(OPCODES) { 119 @Override 120 public AnnotationVisitor visitAnnotation( String desc, boolean visible ) { 121 return av; 122 } 123 @Override 124 public AnnotationVisitor visitTypeAnnotation( int typeRef, TypePath typePath, String descriptor, 125 boolean visible ) { 126 return av; 127 } 128 }; 129 130 private static final ModuleVisitor moduleVisitor = new ModuleVisitor(OPCODES) { 131 }; 132 133 private static final RecordComponentVisitor recordComponentVisitor = new RecordComponentVisitor(OPCODES) { 134 @Override 135 public AnnotationVisitor visitAnnotation(String descriptor, boolean visible) { 136 return av; 137 } 138 139 @Override 140 public AnnotationVisitor visitTypeAnnotation(int typeRef, TypePath typePath, String descriptor, boolean visible) { 141 return av; 142 } 143 }; 144 145 public EmptyVisitor() { 146 super(OPCODES); 147 } 148 149 @Override 150 public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 151 return av; 152 } 153 154 @Override 155 public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { 156 return fieldVisitor; 157 } 158 159 @Override 160 public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { 161 return mv; 162 } 163 164 @Override 165 public AnnotationVisitor visitTypeAnnotation( int typeRef, TypePath typePath, String descriptor, 166 boolean visible ) { 167 return av; 168 } 169 170 @Override 171 public ModuleVisitor visitModule(String name, int access, String version) { 172 return moduleVisitor; 173 } 174 175 @Override 176 public RecordComponentVisitor visitRecordComponent(String name, String descriptor, String signature) { 177 return recordComponentVisitor; 178 } 179 } 180}