Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): render constants. #160

Merged
merged 3 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions lib/xdrgen/generators/java.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ module Generators
class Java < Xdrgen::Generators::Base

def generate
constants_container = Set[]
render_lib
render_definitions(@top)
render_definitions(@top, constants_container)
render_constants constants_container
end

def render_lib
Expand Down Expand Up @@ -36,9 +38,9 @@ def render_lib
@output.write "XdrUnsignedInteger.java", result
end

def render_definitions(node)
node.namespaces.each{|n| render_definitions n }
node.definitions.each(&method(:render_definition))
def render_definitions(node, constants_container)
node.namespaces.each{|n| render_definitions n, constants_container }
node.definitions.each { |defn| render_definition(defn, constants_container) }
end

def add_imports_for_definition(defn, imports)
Expand Down Expand Up @@ -100,7 +102,7 @@ def add_imports_for_definition(defn, imports)
end
end

def render_definition(defn)
def render_definition(defn, constants_container)
imports = Set[]
add_imports_for_definition(defn, imports)

Expand All @@ -123,6 +125,10 @@ def render_definition(defn)
render_element "public class", imports, defn do |out|
render_typedef defn, out
end
when AST::Definitions::Const ;
const_name = defn.name
const_value = defn.value
constants_container.add([const_name, const_value])
end
end

Expand Down Expand Up @@ -169,6 +175,7 @@ def render_element(type, imports, element, post_name="implements XdrElement")
name = name_string element.name
out = @output.open(path)
render_top_matter out
out.puts "import static #{@namespace}.Constants.*;"
imports.each do |import|
out.puts "import #{import};"
end
Expand All @@ -182,6 +189,19 @@ def render_element(type, imports, element, post_name="implements XdrElement")
out.puts "}"
end

def render_constants(constants_container)
out = @output.open("Constants.java")
render_top_matter out
out.puts "public final class Constants {"
out.indent do
out.puts "private Constants() {}"
constants_container.each do |const_name, const_value|
out.puts "public static final int #{const_name} = #{const_value};"
end
end
out.puts "}"
end

def render_enum(enum, out)
out.balance_after /,[\s]*/ do
enum.members.each do |em|
Expand Down Expand Up @@ -664,7 +684,6 @@ def render_top_matter(out)

package #{@namespace};


import java.io.IOException;
EOS
out.break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;

// === xdr source ============================================================

Expand Down
10 changes: 10 additions & 0 deletions spec/output/generator_spec_java/block_comments.x/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

package MyXDR;

import java.io.IOException;

public final class Constants {
private Constants() {}
}
11 changes: 11 additions & 0 deletions spec/output/generator_spec_java/const.x/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

package MyXDR;

import java.io.IOException;

public final class Constants {
private Constants() {}
public static final int FOO = 1;
}
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/const.x/TestArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import java.util.Arrays;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/const.x/TestArray2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import java.util.Arrays;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/enum.x/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;

// === xdr source ============================================================

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/enum.x/Color2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;

// === xdr source ============================================================

Expand Down
10 changes: 10 additions & 0 deletions spec/output/generator_spec_java/enum.x/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

package MyXDR;

import java.io.IOException;

public final class Constants {
private Constants() {}
}
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/enum.x/MessageType.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;

// === xdr source ============================================================

Expand Down
10 changes: 10 additions & 0 deletions spec/output/generator_spec_java/nesting.x/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

package MyXDR;

import java.io.IOException;

public final class Constants {
private Constants() {}
}
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/nesting.x/Foo.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.base.Objects;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/nesting.x/MyUnion.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.base.Objects;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/nesting.x/UnionKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;

// === xdr source ============================================================

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/optional.x/Arr.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import java.util.Arrays;

// === xdr source ============================================================
Expand Down
10 changes: 10 additions & 0 deletions spec/output/generator_spec_java/optional.x/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

package MyXDR;

import java.io.IOException;

public final class Constants {
private Constants() {}
}
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/optional.x/HasOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.base.Objects;

// === xdr source ============================================================
Expand Down
10 changes: 10 additions & 0 deletions spec/output/generator_spec_java/struct.x/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

package MyXDR;

import java.io.IOException;

public final class Constants {
private Constants() {}
}
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/struct.x/Int64.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.base.Objects;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/struct.x/MyStruct.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.base.Objects;
import java.util.Arrays;

Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;

// === xdr source ============================================================

Expand Down
12 changes: 12 additions & 0 deletions spec/output/generator_spec_java/test.x/Constants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Automatically generated by xdrgen
// DO NOT EDIT or your changes may be overwritten

package MyXDR;

import java.io.IOException;

public final class Constants {
private Constants() {}
public static final int FOO = 1244;
public static final int BAR = FOO;
}
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/HasStuff.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.base.Objects;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import java.util.Arrays;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Hashes1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import java.util.Arrays;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Hashes2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import java.util.Arrays;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Hashes3.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import java.util.Arrays;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Int1.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.base.Objects;

// === xdr source ============================================================
Expand Down
2 changes: 1 addition & 1 deletion spec/output/generator_spec_java/test.x/Int2.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

package MyXDR;


import java.io.IOException;

import static MyXDR.Constants.*;
import com.google.common.base.Objects;

// === xdr source ============================================================
Expand Down
Loading