-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Maowcraft
authored and
Maowcraft
committed
Mar 30, 2021
1 parent
6c8239d
commit 861dba5
Showing
15 changed files
with
274 additions
and
25 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
example-test/src/main/java/org/transparent/eureka/example/test/EndlessTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() {} | ||
} |
6 changes: 3 additions & 3 deletions
6
...transparent/eureka/example/test/Test.java → ...rent/eureka/example/test/ExampleTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
example/src/main/java/org/transparent/eureka/example/endless/Endless.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
24 changes: 24 additions & 0 deletions
24
example/src/main/java/org/transparent/eureka/example/endless/EndlessProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
example/src/main/java/org/transparent/eureka/example/endless/EndlessTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...g/transparent/eureka/example/Example.java → ...arent/eureka/example/example/Example.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...rent/eureka/example/ExampleProcessor.java → ...eka/example/example/ExampleProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ent/eureka/example/ExampleTranslator.java → ...ka/example/example/ExampleTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
example/src/main/resources/META-INF/services/javax.annotation.processing.Processor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/main/java/org/transparent/eureka/util/Annotations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |