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
import android.util.Log; | |
public final class User_Log { | |
public static void log(User args) { | |
Log.d("User", String.format("firstName - %s lastName - %s city - %s ", args.firstName, args.lastName, args.city)); | |
} | |
} |
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
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
User g = new User("Gandalf", "The White", "Maia"); | |
User_Log.log(g); | |
} |
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
private void writeSourceFile(TypeElement originatingType) { | |
//get Log class from android.util package | |
//This will make sure the Log class is properly imported into our class | |
ClassName logClassName = ClassName.get("android.util", "Log"); | |
//get the current annotated class name | |
TypeVariableName typeVariableName = TypeVariableName.get(originatingType.getSimpleName().toString()); | |
//create static void method named log | |
MethodSpec log = MethodSpec.methodBuilder(METHOD_LOG) |
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
private boolean isValidClass(TypeElement type){ | |
if(type.getKind() != ElementKind.CLASS){ | |
messager.printMessage(Diagnostic.Kind.ERROR,type.getSimpleName()+" only classes can be annotated with AwesomeLogger"); | |
return false; | |
} | |
if(type.getModifiers().contains(Modifier.PRIVATE)){ | |
messager.printMessage(Diagnostic.Kind.ERROR,type.getSimpleName()+" only public classes can be annotated with AwesomeLogger"); | |
return false; | |
} |
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
package com.example; // PackageElement | |
public class User { // TypeElement | |
private String firstName;// VariableElement | |
private String lastName; // VariableElement | |
public User () {} // ExecuteableElement | |
} |
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
@Override | |
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { | |
//get all elements annotated with AwesomeLogger | |
Collection<? extends Element> annotatedElements = roundEnvironment.getElementsAnnotatedWith(AwesomeLogger.class); | |
//filter out elements we don't need | |
List<TypeElement> types = new ImmutableList.Builder<TypeElement>() | |
.addAll(ElementFilter.typesIn(annotatedElements)) | |
.build(); |
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
@Override | |
public boolean process(Set<? extends TypeElement> set, RoundEnvironment roundEnvironment) { | |
//get all elements annotated with AwesomeLogger | |
Collection<? extends Element> annotatedElements = roundEnvironment.getElementsAnnotatedWith(AwesomeLogger.class); | |
for (Element type : annotatedElements) { | |
... | |
} | |
return true; |
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
ElementType.METHOD | |
ElementType.PACKAGE | |
ElementType.PARAMETER | |
ElementType.TYPE | |
ElementType.ANNOTATION_TYPE | |
ElementType.CONSTRUCTOR | |
ElementType.LOCAL_VARIABLE | |
ElementType.FIELD |
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
@AutoService(Processor.class) | |
public class AwesomeLoggerProcessor extends AbstractProcessor { | |
private static final String KEY_PARAM_NAME = "args"; | |
private static final String METHOD_LOG = "log"; | |
private static final String CLASS_SUFFIX = "_Log"; | |
private Messager messager; | |
private Filer filer; | |
@Override | |
public synchronized void init(ProcessingEnvironment processingEnvironment) { |
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
//processor module | |
dependencies { | |
compile fileTree(dir: 'libs', include: ['*.jar']) | |
compile project(':annotation') | |
compile 'com.google.auto.service:auto-service:1.0-rc2' | |
} |