Skip to content

Commit e111a85

Browse files
committed
Port to java
Overall, this makes the library _much_ clear, and likely fixes long-lasting bugs.
1 parent a4fb3f3 commit e111a85

38 files changed

+2494
-1172
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@ target/
121121

122122
# A big file I use to test performance:
123123
joined.srg
124+
125+
# Maven
126+
target

pom.xml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>net.techcable</groupId>
4+
<artifactId>srglib</artifactId>
5+
<version>0.1.0-alpha1-SNAPSHOT</version>
6+
<properties>
7+
<java.version>1.8</java.version>
8+
</properties>
9+
10+
<dependencies>
11+
<dependency>
12+
<groupId>com.google.guava</groupId>
13+
<artifactId>guava</artifactId>
14+
<version>20.0</version>
15+
</dependency>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>4.12</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.google.code.findbugs</groupId>
24+
<artifactId>jsr305</artifactId>
25+
<version>1.3.9</version>
26+
<scope>provided</scope>
27+
<optional>true</optional>
28+
</dependency>
29+
</dependencies>
30+
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.apache.maven.plugins</groupId>
35+
<artifactId>maven-compiler-plugin</artifactId>
36+
<version>3.5.1</version>
37+
<configuration>
38+
<source>${java.version}</source>
39+
<target>${java.version}</target>
40+
</configuration>
41+
</plugin>
42+
</plugins>
43+
</build>
44+
</project>

profiling.py

-7
This file was deleted.

setup.py

-12
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package net.techcable.srglib;
2+
3+
import static java.util.Objects.*;
4+
5+
/**
6+
* An array type
7+
*/
8+
/* package */ final class ArrayType implements JavaType {
9+
private final JavaType elementType;
10+
public ArrayType(JavaType elementType) {
11+
this.elementType = requireNonNull(elementType);
12+
}
13+
14+
/**
15+
* Return the element type of this array.
16+
*
17+
* @return the element type
18+
*/
19+
@Override
20+
public JavaType getElementType() {
21+
return elementType;
22+
}
23+
24+
@Override
25+
public JavaTypeSort getSort() {
26+
return JavaTypeSort.ARRAY_TYPE;
27+
}
28+
29+
@Override
30+
public String getInternalName() {
31+
return elementType.getInternalName() + "[]";
32+
}
33+
34+
@Override
35+
public String getDescriptor() {
36+
return "[" + elementType.getDescriptor();
37+
}
38+
39+
@Override
40+
public String getName() {
41+
return elementType.getName() + "[]";
42+
}
43+
44+
private JavaType innermostType;
45+
private JavaType getInnermostType() {
46+
if (innermostType == null) {
47+
JavaType innermostType = this.elementType;
48+
while (innermostType instanceof ArrayType) {
49+
innermostType = ((ArrayType) innermostType).elementType;
50+
}
51+
this.innermostType = innermostType;
52+
}
53+
return innermostType;
54+
}
55+
56+
@Override
57+
public int hashCode() {
58+
// Invert the bits to distribute it better
59+
return ~getInnermostType().hashCode();
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
return obj == this || obj != null
65+
&& obj.getClass() == ArrayType.class
66+
&& this.getInnermostType().equals(((ArrayType) obj).getInnermostType());
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return getName();
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package net.techcable.srglib;
2+
3+
import java.util.function.UnaryOperator;
4+
5+
import static com.google.common.base.Preconditions.*;
6+
import static java.util.Objects.*;
7+
8+
/**
9+
* A field's name and declaring type.
10+
*/
11+
public final class FieldData {
12+
private final JavaType declaringType;
13+
private final String name;
14+
15+
private FieldData(JavaType declaringType, String name) {
16+
this.declaringType = requireNonNull(declaringType, "Null declaring type");
17+
this.name = requireNonNull(name, "Null name");
18+
checkArgument(SrgLib.isValidIdentifier(name), "Invalid name: %s", name);
19+
}
20+
21+
/**
22+
* Return the declaring type of this field
23+
*
24+
* @return the declaring type
25+
*/
26+
public JavaType getDeclaringType() {
27+
return declaringType;
28+
}
29+
30+
/**
31+
* Return the name of this field
32+
*
33+
* @return the name
34+
*/
35+
public String getName() {
36+
return name;
37+
}
38+
39+
/**
40+
* Return the internal name of this field.
41+
* <p>
42+
* Internal method names are in the format ${internal type name}/${field name}.
43+
* </p>
44+
*
45+
* @return the internal name
46+
*/
47+
public String getInternalName() {
48+
return this.declaringType.getInternalName() + "/" + this.name;
49+
}
50+
51+
public FieldData withName(String name) {
52+
return new FieldData(declaringType, name);
53+
}
54+
55+
public boolean hasSameTypes(FieldData other) {
56+
requireNonNull(other, "Null other data!");
57+
return this.declaringType.equals(other.declaringType);
58+
}
59+
60+
public FieldData withDeclaringType(JavaType declaringType) {
61+
return new FieldData(declaringType, name);
62+
}
63+
64+
public FieldData mapTypes(UnaryOperator<JavaType> transformer) {
65+
return new FieldData(transformer.apply(declaringType), name);
66+
}
67+
68+
69+
public static FieldData create(JavaType declaringType, String name) {
70+
return new FieldData(declaringType, name);
71+
}
72+
73+
74+
@Override
75+
public int hashCode() {
76+
return declaringType.hashCode() ^ name.hashCode();
77+
}
78+
79+
@Override
80+
public boolean equals(Object obj) {
81+
return obj == this || obj != null
82+
&& obj.getClass() == FieldData.class
83+
&& ((FieldData) obj).declaringType.equals(this.declaringType)
84+
&& ((FieldData) obj).name.equals(this.name);
85+
}
86+
87+
@Override
88+
public String toString() {
89+
return getInternalName();
90+
}
91+
92+
public static FieldData fromInternalName(String internalName) {
93+
int index = internalName.lastIndexOf('/');
94+
checkArgument(index >= 0 && index < internalName.length() - 1, "Invalid internal name: %s", internalName);
95+
JavaType declaringType = JavaType.fromInternalName(internalName.substring(0, index));
96+
String name = internalName.substring(index + 1);
97+
return create(declaringType, name);
98+
}
99+
}

0 commit comments

Comments
 (0)