aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/glavo/javah/NativeMethod.java
blob: 1500697e5abc9d84a53c9a6232af641d5b83e12f (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
86
87
88
89
90
91
92
package org.glavo.javah;

import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import java.util.Objects;
import java.util.regex.Matcher;

import static org.glavo.javah.Utils.*;

public final class NativeMethod {
    private final int access;
    private final String name;
    private final Type type;
    private final String mangledName;
    private final String longMangledName;

    public static NativeMethod of(String name, String descriptor) {
        return NativeMethod.of(0, name, descriptor);
    }

    public static NativeMethod of(String name, Type type) {
        return NativeMethod.of(0, name, type);
    }

    public static NativeMethod of(int access, String name, String descriptor) {
        Objects.requireNonNull(name);
        Objects.requireNonNull(descriptor);
        return NativeMethod.of(access, name, Type.getType(descriptor));
    }

    public static NativeMethod of(int access, String name, Type type) {
        Objects.requireNonNull(name);
        Objects.requireNonNull(type);
        if (!METHOD_NAME_PATTERN.matcher(name).matches()) {
            throw new IllegalArgumentException(String.format("\"%s\" is not a qualified method name", name));
        }
        Matcher m = METHOD_TYPE_PATTERN.matcher(type.toString());
        if (!m.matches()) {
            throw new IllegalArgumentException(String.format("\"%s\" is not a method type", type));
        }
        return new NativeMethod(access, name, type, m.group("args"));
    }

    private NativeMethod(int access, String name, Type type, String arguments) {
        this.access = access;
        this.name = name;
        this.type = type;
        this.mangledName = mangleName(name);
        this.longMangledName = mangledName + "__" + mangleName(arguments);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof NativeMethod)) {
            return false;
        }
        NativeMethod that = (NativeMethod) o;
        return name.equals(that.name) && type.equals(that.type);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, type);
    }

    @Override
    public String toString() {
        return String.format("NativeMethod[name=%s, type=%s}", name, type);
    }

    public String name() {
        return name;
    }

    public Type type() {
        return type;
    }

    public String mangledName() {
        return mangledName;
    }

    public String longMangledName() {
        return longMangledName;
    }

    public boolean isStatic() {
        return (access & Opcodes.ACC_STATIC) != 0;
    }
}