Skip to content

Commit

Permalink
endless test finished
Browse files Browse the repository at this point in the history
  • Loading branch information
Maowcraft authored and Maowcraft committed Mar 30, 2021
1 parent 6c8239d commit 861dba5
Show file tree
Hide file tree
Showing 15 changed files with 274 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.transparent.eureka.example.test;

import org.transparent.eureka.example.endless.Endless;

public class EndlessTest {
public static void main(String[] args) {
endless();
}

@Endless
private static void endless() {}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.transparent.eureka.example.test;

import org.transparent.eureka.example.Example;
import org.transparent.eureka.example.example.Example;

@Example
public final class Test {
public final class ExampleTest {
public static void main(String[] args) {
final Test test = new Test();
final ExampleTest test = new ExampleTest();
System.out.println(test.generated);
System.out.println(test.generated());
}
Expand Down
2 changes: 1 addition & 1 deletion example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ dependencies {
This will install Lucent and its sources to your local Maven repo.
*/
implementation 'org.transparent:lucent:1.0.0'
implementation 'org.transparent:lucent:1.0.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.transparent.eureka.example.endless;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

// Does the following things in a method:
// Creates a local variable --
// final String[] array = {"first", "second", "third"}
// Adds an endless while loop
// while(true)
// Adds a foreach loop inside the while loop
// for (String s : array)
// Prints s
// System.out.println(s);
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.SOURCE)
public @interface Endless {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.transparent.eureka.example.endless;

import org.transparent.lucent.processor.LucentProcessor;
import org.transparent.lucent.transform.LucentTranslator;
import org.transparent.lucent.util.TypeKind;

import java.lang.annotation.Annotation;

public final class EndlessProcessor extends LucentProcessor {
@Override
public LucentTranslator getTranslator() {
return translator(EndlessTranslator::new);
}

@Override
public TypeKind getSupportedTypeKind() {
return TypeKind.CLASS;
}

@Override
public Class<? extends Annotation> getSupportedAnnotation() {
return Endless.class;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package org.transparent.eureka.example.endless;

import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.List;
import com.sun.tools.javac.util.Names;
import org.transparent.eureka.tree.factory.EurekaFactory;
import org.transparent.eureka.util.Annotations;
import org.transparent.eureka.util.Lists;
import org.transparent.lucent.transform.LucentTranslator;

import javax.lang.model.element.Element;

public final class EndlessTranslator extends LucentTranslator {
private final EurekaFactory factory;

public EndlessTranslator(Names names, TreeMaker factory) {
super(names, factory);
this.factory = new EurekaFactory(names, factory);
}

@Override
public void translate(JCTree tree, Element element) {
tree.accept(this);
}

@Override
public void visitMethodDef(JCMethodDecl tree) {
super.visitMethodDef(tree);
if (Annotations.annotated(tree, Endless.class)) {
result = factory.inject(tree)
.add(factory.field()
.mods(Flags.FINAL)
.type(factory.array(factory.id("String")))
.name("array")
.init(factory.array(
factory.id("String"),
Lists.literals(factory,
"first",
"second",
"third"
))))
.add(factory.whileStat(
factory.literal(true),
factory.forEachStat(
factory.field("s", factory.id("String")),
factory.id("array"),
factory.call(
"System.out.println",
factory.id("s")))
))
.tree();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.transparent.eureka.example;
package org.transparent.eureka.example.example;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.transparent.eureka.example;
package org.transparent.eureka.example.example;

import org.transparent.lucent.processor.LucentProcessor;
import org.transparent.lucent.transform.LucentTranslator;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.transparent.eureka.example;
package org.transparent.eureka.example.example;

import com.sun.tools.javac.code.Flags;
import com.sun.tools.javac.code.TypeTag;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
org.transparent.eureka.example.ExampleProcessor
org.transparent.eureka.example.example.ExampleProcessor
org.transparent.eureka.example.endless.EndlessProcessor
14 changes: 14 additions & 0 deletions src/main/java/org/transparent/eureka/api/factory/TreeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ JCVariableDecl field(JCModifiers mods, String name,

JCMethodDecl method(String name, JCBlock body);

JCNewArray array(JCExpression type, List<JCExpression> dimensions, List<JCExpression> elements);

JCNewArray array(JCExpression type, List<JCExpression> elements);

JCArrayTypeTree array(JCExpression type);

JCNewArray array(List<JCExpression> elements);

JCNewArray array();

JCBlock block(long flags);

JCReturn returnStat(JCExpression value);
Expand Down Expand Up @@ -126,5 +136,9 @@ JCEnhancedForLoop forEachStat(JCVariableDecl variable, JCExpression condition,

JCAssign assign(JCExpression lhs, Object rhs);

JCExpressionStatement call(String name, JCExpression... args);

JCExpressionStatement call(String name, Object... args);

BlockBuilder block();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import org.transparent.eureka.tree.builder.BlockBuilder;
import org.transparent.eureka.tree.builder.FieldBuilder;
import org.transparent.eureka.tree.builder.MethodBuilder;
import org.transparent.eureka.util.Injector;
import org.transparent.eureka.util.Injector.*;

public class EurekaFactory extends AbstractTreeFactory {
public EurekaFactory(Names names, TreeMaker factory) {
Expand Down Expand Up @@ -115,10 +115,6 @@ public JCVariableDecl field(String name, JCExpression type) {
return factory.VarDef(mods(), name(name), type, null);
}

public FieldBuilder field() {
return new FieldBuilder(names, factory);
}

@Override
public JCMethodDecl method(JCModifiers mods, String name, JCExpression returnType, JCBlock body) {
return factory.MethodDef(
Expand Down Expand Up @@ -146,6 +142,31 @@ public JCMethodDecl method(String name, JCBlock body) {
body, null);
}

@Override
public JCNewArray array(JCExpression type, List<JCExpression> dimensions, List<JCExpression> elements) {
return factory.NewArray(type, dimensions, elements);
}

@Override
public JCNewArray array(JCExpression type, List<JCExpression> elements) {
return factory.NewArray(type, List.nil(), elements);
}

@Override
public JCArrayTypeTree array(JCExpression type) {
return factory.TypeArray(type);
}

@Override
public JCNewArray array(List<JCExpression> elements) {
return factory.NewArray(null, List.nil(), elements);
}

@Override
public JCNewArray array() {
return factory.NewArray(null, List.nil(), List.nil());
}

@Override
public JCBlock block(long flags) {
return factory.Block(flags, List.nil());
Expand Down Expand Up @@ -312,11 +333,41 @@ public JCAssign assign(JCExpression lhs, Object rhs) {
return factory.Assign(lhs, literal(rhs));
}

@Override
public JCExpressionStatement call(String name, JCExpression... args) {
JCExpression expr = id(name);
if (args.length > 0)
expr = factory.Apply(
List.nil(), expr, List.from(args)
);
return factory.Exec(expr);
}

@Override
public JCExpressionStatement call(String name, Object... args) {
JCExpression expr = id(name);
if (args.length > 0) {
List<JCExpression> exprs = List.nil();
for (Object arg : args)
exprs = exprs.append(literal(arg));
expr = factory.Apply(List.nil(), expr, exprs);
}
return factory.Exec(expr);
}

public FieldBuilder field() {
return new FieldBuilder(names, factory);
}

public MethodBuilder method() {
return new MethodBuilder(names, factory);
}

public Injector inject(JCClassDecl tree) {
return new Injector(tree);
public ClassInjector inject(JCClassDecl tree) {
return new ClassInjector(tree);
}

public MethodInjector inject(JCMethodDecl tree) {
return new MethodInjector(tree, factory);
}
}
19 changes: 19 additions & 0 deletions src/main/java/org/transparent/eureka/util/Annotations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.transparent.eureka.util;

import com.sun.tools.javac.tree.JCTree.JCMethodDecl;

import java.lang.annotation.Annotation;

public final class Annotations {
private Annotations() {}

public static boolean annotated(JCMethodDecl tree, Class<? extends Annotation> annotation) {
return tree.mods.annotations
.stream()
.anyMatch(anno -> anno.type.tsym
.getQualifiedName()
.contentEquals(
annotation.getCanonicalName()
));
}
}
53 changes: 43 additions & 10 deletions src/main/java/org/transparent/eureka/util/Injector.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
package org.transparent.eureka.util;

import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.*;
import com.sun.tools.javac.tree.TreeMaker;
import com.sun.tools.javac.util.List;
import org.transparent.eureka.api.builder.Builder;
import org.transparent.eureka.tree.builder.BlockBuilder;
import org.transparent.eureka.tree.builder.FieldBuilder;

public final class Injector {
private final JCClassDecl tree;
public abstract class Injector<T extends JCTree> {
protected final T tree;

public Injector(JCClassDecl tree) {
public Injector(T tree) {
this.tree = tree;
}

public Injector add(Builder<? extends JCTree> builder) {
tree.defs = tree.defs
.append(builder.build());
return this;
public T tree() {
return tree;
}

public JCClassDecl tree() {
return tree;
public static class ClassInjector extends Injector<JCClassDecl> {
public ClassInjector(JCClassDecl tree) {
super(tree);
}

public ClassInjector add(Builder<? extends JCTree> builder) {
return add(builder.build());
}

public ClassInjector add(JCTree tree) {
this.tree.defs = this.tree.defs
.append(tree);
return this;
}
}

public static class MethodInjector extends Injector<JCMethodDecl> {
private final TreeMaker maker;

public MethodInjector(JCMethodDecl tree, TreeMaker maker) {
super(tree);
this.maker = maker;
}

public MethodInjector add(FieldBuilder builder) {
return add(builder.build());
}

public MethodInjector add(JCStatement statement) {
tree.body.stats = tree.body.stats
.append(statement);
return this;
}
}
}
18 changes: 18 additions & 0 deletions src/main/java/org/transparent/eureka/util/Lists.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.transparent.eureka.util;

import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.util.List;
import org.transparent.eureka.api.factory.TreeFactory;

public final class Lists {
private Lists() {}

public static List<JCExpression> literals(TreeFactory factory, Object... values) {
List<JCExpression> literals = List.nil();
for (Object value : values) {
literals = literals.append(
factory.literal(value));
}
return literals;
}
}

0 comments on commit 861dba5

Please sign in to comment.