Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

Commit acfa97d

Browse files
committed
Initial Commit
0 parents  commit acfa97d

File tree

19 files changed

+518
-0
lines changed

19 files changed

+518
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
target
3+
JavaClassLib.iml

pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.github.seggan.javaclasslib</groupId>
8+
<artifactId>JavaClassLib</artifactId>
9+
<version>0.1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>11</maven.compiler.source>
13+
<maven.compiler.target>11</maven.compiler.target>
14+
</properties>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<groupId>org.apache.maven.plugins</groupId>
20+
<artifactId>maven-shade-plugin</artifactId>
21+
<version>3.2.4</version>
22+
<configuration>
23+
<relocations>
24+
<relocation>
25+
<pattern>com.google.common</pattern>
26+
<shadedPattern>io.github.seggan.javaclasslib.common</shadedPattern>
27+
</relocation>
28+
</relocations>
29+
</configuration>
30+
<executions>
31+
<execution>
32+
<phase>package</phase>
33+
<goals>
34+
<goal>shade</goal>
35+
</goals>
36+
</execution>
37+
</executions>
38+
</plugin>
39+
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-surefire-plugin</artifactId>
43+
<version>3.0.0-M5</version>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
<dependencies>
49+
<dependency>
50+
<groupId>com.google.code.findbugs</groupId>
51+
<artifactId>jsr305</artifactId>
52+
<version>3.0.2</version>
53+
<scope>provided</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>com.google.guava</groupId>
58+
<artifactId>guava</artifactId>
59+
<version>30.1.1-jre</version>
60+
<scope>compile</scope>
61+
</dependency>
62+
63+
<dependency>
64+
<groupId>org.junit.jupiter</groupId>
65+
<artifactId>junit-jupiter-api</artifactId>
66+
<version>5.6.0</version>
67+
<scope>test</scope>
68+
</dependency>
69+
</dependencies>
70+
</project>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.github.seggan.javaclasslib;
2+
3+
import javax.annotation.Nonnull;
4+
import java.nio.ByteBuffer;
5+
import java.nio.ByteOrder;
6+
7+
public final class ByteUtils {
8+
9+
private ByteUtils() {}
10+
11+
@Nonnull
12+
public static byte[] twoBytesFromInt(int i) {
13+
byte[] data = new byte[2];
14+
data[0] = (byte) ((i >> 8) & 0xFF);
15+
data[1] = (byte) (i & 0xFF);
16+
return data;
17+
}
18+
19+
@Nonnull
20+
public static byte[] floatToBytes(float f) {
21+
return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putFloat(f).array();
22+
}
23+
24+
@Nonnull
25+
public static byte[] intToBytes(int i) {
26+
return ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(i).array();
27+
}
28+
29+
@Nonnull
30+
public static byte[] longToBytes(long l) {
31+
return ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN).putLong(l).array();
32+
}
33+
34+
@Nonnull
35+
public static byte[] doubleToBytes(double d) {
36+
return ByteBuffer.allocate(8).order(ByteOrder.BIG_ENDIAN).putDouble(d).array();
37+
}
38+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package io.github.seggan.javaclasslib;
2+
3+
import io.github.seggan.javaclasslib.constantpool.ConstantPoolEntry;
4+
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
public final class JavaClass {
9+
10+
private final List<ConstantPoolEntry> constantPool = new LinkedList<>();
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package io.github.seggan.javaclasslib.constantpool;
2+
3+
import com.google.common.primitives.Bytes;
4+
5+
import javax.annotation.Nonnull;
6+
import java.util.List;
7+
8+
public final class ClassEntry extends ConstantPoolEntry {
9+
10+
private final UTF8Entry name;
11+
12+
public ClassEntry(List<ConstantPoolEntry> constantPool, UTF8Entry name) {
13+
super(constantPool, ConstantPoolTag.CLASS);
14+
this.name = name;
15+
}
16+
17+
@Nonnull
18+
@Override
19+
public byte[] getBytes() {
20+
return Bytes.concat(getTag().getByte(), name.getIndexBytes());
21+
}
22+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package io.github.seggan.javaclasslib.constantpool;
2+
3+
import io.github.seggan.javaclasslib.ByteUtils;
4+
5+
import javax.annotation.Nonnull;
6+
import javax.annotation.ParametersAreNonnullByDefault;
7+
import java.util.Arrays;
8+
import java.util.List;
9+
import java.util.Objects;
10+
11+
public abstract class ConstantPoolEntry {
12+
13+
private final List<ConstantPoolEntry> constantPool;
14+
private final ConstantPoolTag tag;
15+
16+
@ParametersAreNonnullByDefault
17+
public ConstantPoolEntry(List<ConstantPoolEntry> constantPool, ConstantPoolTag tag) {
18+
this.constantPool = constantPool;
19+
this.tag = tag;
20+
constantPool.add(this);
21+
}
22+
23+
public final ConstantPoolTag getTag() {
24+
return tag;
25+
}
26+
27+
public final int getIndex() {
28+
return constantPool.indexOf(this);
29+
}
30+
31+
@Nonnull
32+
public final byte[] getIndexBytes() {
33+
return ByteUtils.twoBytesFromInt(getIndex());
34+
}
35+
36+
@Nonnull
37+
public abstract byte[] getBytes();
38+
39+
@Override
40+
public boolean equals(Object o) {
41+
if (this == o) return true;
42+
if (!(o instanceof ConstantPoolEntry)) return false;
43+
ConstantPoolEntry that = (ConstantPoolEntry) o;
44+
return getTag() == that.getTag() && Arrays.equals(getBytes(), that.getBytes());
45+
}
46+
47+
@Override
48+
public int hashCode() {
49+
int result = Objects.hash(getTag());
50+
result = 31 * result + Arrays.hashCode(getBytes());
51+
return result;
52+
}
53+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.github.seggan.javaclasslib.constantpool;
2+
3+
import javax.annotation.Nonnull;
4+
5+
public enum ConstantPoolTag {
6+
CLASS(7),
7+
FIELDREF(9),
8+
METHODREF(10),
9+
INTERFACE_METHODREF(11),
10+
STRING(8),
11+
INTEGER(3),
12+
FLOAT(4),
13+
LONG(5),
14+
DOUBLE(6),
15+
NAME_AND_TYPE(12),
16+
UTF_8(1),
17+
METHOD_HANDLE(15),
18+
METHOD_TYPE(16),
19+
INVOKE_DYNAMIC(18);
20+
21+
private final byte binary;
22+
23+
ConstantPoolTag(int binary) {
24+
this.binary = (byte) binary;
25+
}
26+
27+
@Nonnull
28+
public byte[] getByte() {
29+
return new byte[]{binary};
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.github.seggan.javaclasslib.constantpool;
2+
3+
import com.google.common.primitives.Bytes;
4+
5+
import javax.annotation.Nonnull;
6+
import java.util.List;
7+
8+
public final class NameAndTypeEntry extends ConstantPoolEntry {
9+
10+
private final UTF8Entry name;
11+
private final UTF8Entry descriptor;
12+
13+
public NameAndTypeEntry(List<ConstantPoolEntry> constantPool, UTF8Entry name, UTF8Entry descriptor) {
14+
super(constantPool, ConstantPoolTag.NAME_AND_TYPE);
15+
this.name = name;
16+
this.descriptor = descriptor;
17+
}
18+
19+
@Nonnull
20+
@Override
21+
public byte[] getBytes() {
22+
return Bytes.concat(getTag().getByte(), name.getIndexBytes(), descriptor.getIndexBytes());
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.seggan.javaclasslib.constantpool;
2+
3+
import com.google.common.primitives.Bytes;
4+
import io.github.seggan.javaclasslib.ByteUtils;
5+
6+
import javax.annotation.Nonnull;
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.List;
9+
10+
public final class UTF8Entry extends ConstantPoolEntry {
11+
12+
private final byte[] data;
13+
14+
public UTF8Entry(List<ConstantPoolEntry> constantPool, byte[] data) {
15+
super(constantPool, ConstantPoolTag.UTF_8);
16+
this.data = data;
17+
}
18+
19+
public UTF8Entry(List<ConstantPoolEntry> constantPool, String s) {
20+
this(constantPool, s.getBytes(StandardCharsets.UTF_8));
21+
}
22+
23+
@Nonnull
24+
@Override
25+
public byte[] getBytes() {
26+
return Bytes.concat(getTag().getByte(), ByteUtils.twoBytesFromInt(data.length), data);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.github.seggan.javaclasslib.constantpool.classmembers;
2+
3+
import com.google.common.primitives.Bytes;
4+
import io.github.seggan.javaclasslib.constantpool.ClassEntry;
5+
import io.github.seggan.javaclasslib.constantpool.ConstantPoolEntry;
6+
import io.github.seggan.javaclasslib.constantpool.ConstantPoolTag;
7+
import io.github.seggan.javaclasslib.constantpool.NameAndTypeEntry;
8+
9+
import javax.annotation.Nonnull;
10+
import java.util.List;
11+
12+
abstract class ClassMemberEntry extends ConstantPoolEntry {
13+
14+
private final ClassEntry clazz;
15+
private final NameAndTypeEntry nameAndType;
16+
17+
public ClassMemberEntry(List<ConstantPoolEntry> constantPool, ConstantPoolTag key, ClassEntry clazz, NameAndTypeEntry nameAndType) {
18+
super(constantPool, key);
19+
this.clazz = clazz;
20+
this.nameAndType = nameAndType;
21+
}
22+
23+
@Nonnull
24+
@Override
25+
public final byte[] getBytes() {
26+
return Bytes.concat(getTag().getByte(), clazz.getIndexBytes(), nameAndType.getIndexBytes());
27+
}
28+
}

0 commit comments

Comments
 (0)