aboutsummaryrefslogtreecommitdiff
path: root/plugin/src/main/java/ch/jodersky/sbt/jni/javah/ClassName.java
blob: 8cb8f28ec56e72414bd521462d45e89d55a89003 (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
package ch.jodersky.sbt.jni.javah;

import java.util.Objects;

public final class ClassName {
    private final String moduleName;
    private final String className;
    private final String simpleName;
    private final String mangledName;

    public static ClassName of(String moduleName, String className) {
        Objects.requireNonNull(className, "Class name is null");

        if (moduleName != null && !Utils.FULL_NAME_PATTERN.matcher(moduleName).matches()) {
            throw new IllegalArgumentException("Illegal module name: " + moduleName);
        }
        if (!Utils.FULL_NAME_PATTERN.matcher(className).matches()) {
            throw new IllegalArgumentException("Illegal class name: " + moduleName);
        }

        return new ClassName(moduleName, className);
    }

    public static ClassName of(String fullName) {
        Objects.requireNonNull(fullName, "class name is null");
        int idx = fullName.indexOf('/');
        if (idx == -1) {
            return ClassName.of(null, fullName);
        }

        return ClassName.of(fullName.substring(0, idx), fullName.substring(idx + 1));
    }

    private ClassName(String moduleName, String className) {
        this.moduleName = moduleName;
        this.className = className;
        this.simpleName = className.substring(className.lastIndexOf('.') + 1);
        this.mangledName = Utils.mangleName(className);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ClassName)) return false;
        ClassName className1 = (ClassName) o;
        return Objects.equals(moduleName, className1.moduleName) && className.equals(className1.className);
    }

    @Override
    public int hashCode() {
        return Objects.hash(moduleName, className);
    }

    @Override
    public String toString() {
        if (moduleName == null) {
            return className;
        }
        return moduleName + '/' + className;
    }


    //
    // Getters and Setters
    //

    public final String moduleName() {
        return moduleName;
    }

    public final String className() {
        return className;
    }

    public final String simpleName() {
        return simpleName;
    }

    public final String mangledName() {
        return mangledName;
    }

    public final String relativePath() {
        return className.replace('.', '/') + ".class";
    }
}