summaryrefslogtreecommitdiffstats
path: root/abi-check-plugin/src/main/java/com/yahoo/abicheck/collector/PublicSignatureCollector.java
blob: 4fee85c823a975286917b6ecd5552d2e7bce8551 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.yahoo.abicheck.collector;

import com.yahoo.abicheck.signature.JavaClassSignature;
import com.yahoo.abicheck.signature.JavaMethodSignature;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

public class PublicSignatureCollector extends ClassVisitor {

  private final Map<String, JavaClassSignature> classSignatures = new LinkedHashMap<>();

  private String currentName;
  private int currentAccess;
  private Map<String, JavaMethodSignature> currentMethods;

  public PublicSignatureCollector() {
    super(Opcodes.ASM6);
  }

  private static String methodNameWithArguments(String name, List<String> argumentTypes) {
    return String.format("%s(%s)", name, String.join(", ", argumentTypes));
  }

  private static boolean testBit(long access, long mask) {
    return (access & mask) != 0;
  }

  @Override
  public void visit(int version, int access, String name, String signature, String superName,
      String[] interfaces) {
    currentName = Type.getObjectType(name).getClassName();
    currentAccess = access;
    currentMethods = new LinkedHashMap<>();
  }

  @Override
  public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
      String[] exceptions) {
    if (isVisibleMethod(access)) {
      Type method = Type.getMethodType(descriptor);
      List<String> argumentTypes = Arrays.stream(method.getArgumentTypes()).map(Type::getClassName)
          .collect(Collectors.toList());
      name = methodNameWithArguments(name, argumentTypes);
      currentMethods.put(name,
          new JavaMethodSignature(Util.convertAccess(access, Util.methodFlags),
              method.getReturnType().getClassName()));
    }
    return null;
  }

  private boolean isVisibleMethod(int access) {
    // Public methods are visible
    if (testBit(access, Opcodes.ACC_PUBLIC)) {
      return true;
    }
    // Protected non-static methods are visible if the class is not final (can be called from
    // extending classes)
    if (!testBit(access, Opcodes.ACC_STATIC) && testBit(access, Opcodes.ACC_PROTECTED) && !testBit(
        currentAccess, Opcodes.ACC_FINAL)) {
      return true;
    }
    // Otherwise not visible
    return false;
  }

  @Override
  public void visitEnd() {
    if ((currentAccess & Opcodes.ACC_PUBLIC) != 0) {
      classSignatures.put(currentName,
          new JavaClassSignature(Util.convertAccess(currentAccess, Util.classFlags),
              currentMethods));
    }
  }

  public Map<String, JavaClassSignature> getClassSignatures() {
    return classSignatures;
  }
}