-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathFastClassAccessor.java
More file actions
169 lines (141 loc) · 4.81 KB
/
FastClassAccessor.java
File metadata and controls
169 lines (141 loc) · 4.81 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
* This file is part of FalsePatternLib.
*
* Copyright (C) 2022-2025 FalsePattern
* All Rights Reserved
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* FalsePatternLib is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, only version 3 of the License.
*
* FalsePatternLib is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FalsePatternLib. If not, see <https://www.gnu.org/licenses/>.
*/
package com.falsepattern.lib.turboasm;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.ClassNode;
import java.lang.reflect.Modifier;
/** An accessor to metadata about a class that is quickly accessible without fully parsing one. */
public interface FastClassAccessor {
/** Accessible from outside its package */
boolean isPublic();
/** No subclasses allowed */
boolean isFinal();
/** Is an interface instead of a class */
boolean isInterface();
/** Is an abstract class that should not be instantiated */
boolean isAbstract();
/** Is not present in source code (often used by obfuscated code too) */
boolean isSynthetic();
/** Is an annotation interface */
boolean isAnnotation();
/** Is an enum class */
boolean isEnum();
/** Binary (slash-separated packages) name of the class */
@NotNull
String binaryThisName();
/** Binary (slash-separated packages) name of the super-class, null for the Object class */
@Nullable
String binarySuperName();
static OfLoaded ofLoaded(Class<?> loadedClass) {
return new OfLoaded(loadedClass);
}
static OfAsmNode ofAsmNode(ClassNode handle) {
return new OfAsmNode(handle);
}
final class OfAsmNode implements FastClassAccessor {
public final ClassNode handle;
public OfAsmNode(ClassNode handle) {
this.handle = handle;
}
@Override
public boolean isPublic() {
return (handle.access & Opcodes.ACC_PUBLIC) != 0;
}
@Override
public boolean isFinal() {
return (handle.access & Opcodes.ACC_FINAL) != 0;
}
@Override
public boolean isInterface() {
return (handle.access & Opcodes.ACC_INTERFACE) != 0;
}
@Override
public boolean isAbstract() {
return (handle.access & Opcodes.ACC_ABSTRACT) != 0;
}
@Override
public boolean isSynthetic() {
return (handle.access & Opcodes.ACC_SYNTHETIC) != 0;
}
@Override
public boolean isAnnotation() {
return (handle.access & Opcodes.ACC_ANNOTATION) != 0;
}
@Override
public boolean isEnum() {
return (handle.access & Opcodes.ACC_ENUM) != 0;
}
@Override
public @NotNull String binaryThisName() {
return handle.name;
}
@Override
public @Nullable String binarySuperName() {
return handle.superName;
}
}
final class OfLoaded implements FastClassAccessor {
public final Class<?> handle;
private OfLoaded(Class<?> handle) {
this.handle = handle;
}
@Override
public boolean isPublic() {
return Modifier.isPublic(handle.getModifiers());
}
@Override
public boolean isFinal() {
return Modifier.isFinal(handle.getModifiers());
}
@Override
public boolean isInterface() {
return Modifier.isInterface(handle.getModifiers());
}
@Override
public boolean isAbstract() {
return Modifier.isAbstract(handle.getModifiers());
}
@Override
public boolean isSynthetic() {
return handle.isSynthetic();
}
@Override
public boolean isAnnotation() {
return handle.isAnnotation();
}
@Override
public boolean isEnum() {
return handle.isEnum();
}
@Override
public @NotNull String binaryThisName() {
return handle.getName().replace('.', '/');
}
@Override
public @Nullable String binarySuperName() {
final Class<?> superclass = handle.getSuperclass();
return superclass == null ? null : superclass.getName().replace('.', '/');
}
}
}