Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
Maowcraft authored and Maowcraft committed Mar 31, 2021
1 parent 701c2d6 commit 539b1d9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.transparent.eureka.example.example;

import com.sun.tools.javac.code.TypeTag;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.tree.TreeMaker;
Expand All @@ -10,6 +11,7 @@
import javax.lang.model.element.Element;

import static org.transparent.eureka.util.Modifiers.PRIVATE_FINAL;
import static org.transparent.eureka.util.Modifiers.PUBLIC_STATIC;

public final class ExampleTranslator extends LucentTranslator {
private final EurekaFactory factory;
Expand Down Expand Up @@ -39,6 +41,15 @@ public void visitClassDef(JCClassDecl tree) {
.name("myField")
.value("This is my field.")
.build());
tree.defs = tree.defs.append(factory.method()
.mods(PUBLIC_STATIC)
.type(TypeTag.VOID)
.name("myMethod")
.body(factory.block()
.ifStat(factory.bin("Clearly not null", Tag.NE, null),
factory.call("System.out.println", "If statement test."))
.call("System.out.println", "Hello, world!"))
.build());
result = tree;
}
}
6 changes: 4 additions & 2 deletions src/main/java/org/transparent/eureka/EurekaFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public JCExpressionStatement exprStat(JCExpression expr) {
}

@Override
public JCBinary bin(Tag operator, Object lhs, Object rhs) {
public JCBinary bin(Object lhs, Tag operator, Object rhs) {
return factory.Binary(operator, expr(lhs), expr(rhs));
}

Expand All @@ -202,9 +202,11 @@ public JCExpressionStatement call(String name, Object... args) {
}

public JCExpression expr(Object value) {
if (value == null)
return literal();
return (value instanceof JCExpression)
? (JCExpression) value
: factory.Literal(value);
: literal(value);
}

public JCStatement stat(Object value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ JCEnhancedForLoop forEach(JCVariableDecl variable, JCExpression condition,

JCExpressionStatement exprStat(JCExpression expr);

JCBinary bin(Tag operator, Object lhs, Object rhs);
JCBinary bin(Object lhs, Tag operator, Object rhs);

JCAssignOp assign(Tag operator, Object lhs, Object rhs);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sun.tools.javac.util.List;
import org.transparent.eureka.EurekaFactory;
import org.transparent.eureka.api.builder.TreeBuilder;
import org.transparent.eureka.util.Modifiers;

public class MethodBuilder extends TreeBuilder<JCMethodDecl> {
private JCModifiers mods;
Expand All @@ -24,6 +25,11 @@ public MethodBuilder(EurekaFactory factory) {
body = factory.maker().Block(0L, List.nil());
}

public MethodBuilder mods(Modifiers mods) {
this.mods = factory.mods(mods.getFlags());
return this;
}

public MethodBuilder mods(long flags) {
this.mods = factory.mods(flags);
return this;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/transparent/eureka/util/Modifiers.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ public enum Modifiers {
PUBLIC_STATIC_FINAL (PUBLIC | STATIC | FINAL),
PRIVATE_FINAL (PRIVATE | FINAL),
PUBLIC_FINAL (PUBLIC | FINAL),
PRIVATE_STATIC (PRIVATE | STATIC),
PUBLIC_STATIC (PUBLIC | STATIC),
;

private final long flags;
Expand Down

0 comments on commit 539b1d9

Please sign in to comment.