Skip to content

Commit

Permalink
refactor(java): Remove the dependency on third-party libraries. (stel…
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat authored Sep 12, 2023
1 parent 2bb0b55 commit f0c4145
Show file tree
Hide file tree
Showing 67 changed files with 354 additions and 516 deletions.
44 changes: 21 additions & 23 deletions lib/xdrgen/generators/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def render_definitions(node, constants_container)
end

def add_imports_for_definition(defn, imports)
imports.add("com.google.common.io.BaseEncoding")
imports.add("java.util.Base64")
imports.add("java.io.ByteArrayInputStream")
imports.add("java.io.ByteArrayOutputStream")

Expand All @@ -53,15 +53,15 @@ def add_imports_for_definition(defn, imports)
if is_decl_array(m.declaration)
imports.add('java.util.Arrays')
else
imports.add('com.google.common.base.Objects')
imports.add('java.util.Objects')
end
end
# if we have more than one member field then the
# hash code will be computed by
# Objects.hashCode(field1, field2, ..., fieldN)
# therefore, we should always import com.google.common.base.Objects
# Objects.hash(field1, field2, ..., fieldN)
# therefore, we should always import java.util.Objects
if defn.members.length > 1
imports.add("com.google.common.base.Objects")
imports.add("java.util.Objects")
end
when AST::Definitions::Enum ;
# no imports required for enums
Expand All @@ -73,30 +73,30 @@ def add_imports_for_definition(defn, imports)
if is_type_array(defn.discriminant.type)
imports.add('java.util.Arrays')
else
imports.add('com.google.common.base.Objects')
imports.add('java.util.Objects')
end

nonVoidArms.each do |a|
if is_decl_array(a.declaration)
imports.add('java.util.Arrays')
else
imports.add('com.google.common.base.Objects')
imports.add('java.util.Objects')
end
end

# if we have more than one field then the
# hash code will be computed by
# Objects.hashCode(field1, field2, ..., fieldN)
# therefore, we should always import com.google.common.base.Objects
# Objects.hash(field1, field2, ..., fieldN)
# therefore, we should always import java.util.Objects
# if we have more than one field
if totalFields > 1
imports.add("com.google.common.base.Objects")
imports.add("java.util.Objects")
end
when AST::Definitions::Typedef ;
if is_decl_array(defn.declaration)
imports.add('java.util.Arrays')
else
imports.add('com.google.common.base.Objects')
imports.add('java.util.Objects')
end
end

Expand Down Expand Up @@ -300,10 +300,10 @@ def render_struct(struct, out)
if is_decl_array(struct.members[0].declaration)
"Arrays.hashCode(this.#{struct.members[0].name})"
else
"Objects.hashCode(this.#{struct.members[0].name})"
"Objects.hash(this.#{struct.members[0].name})"
end
else
"Objects.hashCode(#{
"Objects.hash(#{
(struct.members.map { |m|
if is_decl_array(m.declaration)
"Arrays.hashCode(this.#{m.name})"
Expand All @@ -324,7 +324,7 @@ def render_struct(struct, out)
if is_decl_array(m.declaration)
"Arrays.equals(this.#{m.name}, other.#{m.name})"
else
"Objects.equal(this.#{m.name}, other.#{m.name})"
"Objects.equals(this.#{m.name}, other.#{m.name})"
end
}
equalExpression = case equalParts.length
Expand Down Expand Up @@ -432,7 +432,7 @@ def render_typedef(typedef, out)
if is_decl_array(typedef.declaration)
"Arrays.hashCode"
else
"Objects.hashCode"
"Objects.hash"
end
out.puts <<-EOS.strip_heredoc
@Override
Expand All @@ -446,7 +446,7 @@ def render_typedef(typedef, out)
if is_decl_array(typedef.declaration)
"Arrays.equals"
else
"Objects.equal"
"Objects.equals"
end
type = name_string typedef.name
out.puts <<-EOS.strip_heredoc
Expand Down Expand Up @@ -639,7 +639,7 @@ def render_union(union, out)
}
parts.append(discriminantPart)

hashCodeExpression = "Objects.hashCode(#{parts.join(", ")})"
hashCodeExpression = "Objects.hash(#{parts.join(", ")})"
out.puts <<-EOS.strip_heredoc
@Override
public int hashCode() {
Expand All @@ -651,14 +651,14 @@ def render_union(union, out)
if is_decl_array(a.declaration)
"Arrays.equals(this.#{a.name}, other.#{a.name})"
else
"Objects.equal(this.#{a.name}, other.#{a.name})"
"Objects.equals(this.#{a.name}, other.#{a.name})"
end
}
equalParts.append(
if is_type_array(union.discriminant.type)
"Arrays.equals(this.#{union.discriminant.name}, other.#{union.discriminant.name})"
else
"Objects.equal(this.#{union.discriminant.name}, other.#{union.discriminant.name})"
"Objects.equals(this.#{union.discriminant.name}, other.#{union.discriminant.name})"
end
)

Expand Down Expand Up @@ -716,8 +716,7 @@ def render_base64(return_type, out)
out.puts <<-EOS.strip_heredoc
@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
return Base64.getEncoder().encodeToString(toXdrByteArray());
}
@Override
Expand All @@ -729,8 +728,7 @@ def render_base64(return_type, out)
}
public static #{return_type} fromXdrBase64(String xdr) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
byte[] bytes = Base64.getDecoder().decode(xdr);
return fromXdrByteArray(bytes);
}
Expand Down
8 changes: 3 additions & 5 deletions lib/xdrgen/generators/java/XdrString.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package <%= @namespace %>;

import com.google.common.io.BaseEncoding;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Base64;

public class XdrString implements XdrElement {
private byte[] bytes;
Expand Down Expand Up @@ -41,8 +41,7 @@ public class XdrString implements XdrElement {

@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
return Base64.getEncoder().encodeToString(toXdrByteArray());
}

@Override
Expand All @@ -54,8 +53,7 @@ public class XdrString implements XdrElement {
}

public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
byte[] bytes = Base64.getDecoder().decode(xdr);
return fromXdrByteArray(bytes, maxSize);
}

Expand Down
15 changes: 6 additions & 9 deletions lib/xdrgen/generators/java/XdrUnsignedHyperInteger.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package <%= @namespace %>;

import com.google.common.base.Objects;
import com.google.common.io.BaseEncoding;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Base64;
import java.util.Objects;

/**
* Represents XDR Unsigned Hyper Integer.
Expand Down Expand Up @@ -62,8 +61,7 @@ public class XdrUnsignedHyperInteger implements XdrElement {

@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
return Base64.getEncoder().encodeToString(toXdrByteArray());
}

@Override
Expand All @@ -75,8 +73,7 @@ public class XdrUnsignedHyperInteger implements XdrElement {
}

public static XdrUnsignedHyperInteger fromXdrBase64(String xdr) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
byte[] bytes = Base64.getDecoder().decode(xdr);
return fromXdrByteArray(bytes);
}

Expand All @@ -88,7 +85,7 @@ public class XdrUnsignedHyperInteger implements XdrElement {

@Override
public int hashCode() {
return Objects.hashCode(this.number);
return Objects.hash(this.number);
}

@Override
Expand All @@ -98,7 +95,7 @@ public class XdrUnsignedHyperInteger implements XdrElement {
}

XdrUnsignedHyperInteger other = (XdrUnsignedHyperInteger) object;
return Objects.equal(this.number, other.number);
return Objects.equals(this.number, other.number);
}

public String toString() {
Expand Down
15 changes: 6 additions & 9 deletions lib/xdrgen/generators/java/XdrUnsignedInteger.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package <%= @namespace %>;

import com.google.common.base.Objects;
import com.google.common.io.BaseEncoding;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.Objects;

/**
* Represents XDR Unsigned Integer.
Expand Down Expand Up @@ -50,8 +49,7 @@ public class XdrUnsignedInteger implements XdrElement {

@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
return Base64.getEncoder().encodeToString(toXdrByteArray());
}

@Override
Expand All @@ -63,8 +61,7 @@ public class XdrUnsignedInteger implements XdrElement {
}

public static XdrUnsignedInteger fromXdrBase64(String xdr) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
byte[] bytes = Base64.getDecoder().decode(xdr);
return fromXdrByteArray(bytes);
}

Expand All @@ -76,7 +73,7 @@ public class XdrUnsignedInteger implements XdrElement {

@Override
public int hashCode() {
return Objects.hashCode(this.number);
return Objects.hash(this.number);
}

@Override
Expand All @@ -86,7 +83,7 @@ public class XdrUnsignedInteger implements XdrElement {
}

XdrUnsignedInteger other = (XdrUnsignedInteger) object;
return Objects.equal(this.number, other.number);
return Objects.equals(this.number, other.number);
}

public String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.io.BaseEncoding;
import java.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

Expand Down Expand Up @@ -49,8 +49,7 @@ public void encode(XdrDataOutputStream stream) throws IOException {
}
@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
return Base64.getEncoder().encodeToString(toXdrByteArray());
}

@Override
Expand All @@ -62,8 +61,7 @@ public byte[] toXdrByteArray() throws IOException {
}

public static AccountFlags fromXdrBase64(String xdr) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
byte[] bytes = Base64.getDecoder().decode(xdr);
return fromXdrByteArray(bytes);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package MyXDR;

import com.google.common.io.BaseEncoding;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Base64;

public class XdrString implements XdrElement {
private byte[] bytes;
Expand Down Expand Up @@ -41,8 +41,7 @@ public byte[] getBytes() {

@Override
public String toXdrBase64() throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
return base64Encoding.encode(toXdrByteArray());
return Base64.getEncoder().encodeToString(toXdrByteArray());
}

@Override
Expand All @@ -54,8 +53,7 @@ public byte[] toXdrByteArray() throws IOException {
}

public static XdrString fromXdrBase64(String xdr, int maxSize) throws IOException {
BaseEncoding base64Encoding = BaseEncoding.base64();
byte[] bytes = base64Encoding.decode(xdr);
byte[] bytes = Base64.getDecoder().decode(xdr);
return fromXdrByteArray(bytes, maxSize);
}

Expand Down
Loading

0 comments on commit f0c4145

Please sign in to comment.