-
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
Oleg Khalidov
committed
Jan 13, 2016
1 parent
9202eb6
commit fc11771
Showing
13 changed files
with
413 additions
and
35 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,15 @@ | |
import org.brooth.androjeta.apt.processors.RetainProcessor; | ||
import org.brooth.jeta.apt.JetaProcessor; | ||
|
||
import javax.annotation.processing.SupportedAnnotationTypes; | ||
import javax.annotation.processing.SupportedSourceVersion; | ||
import javax.lang.model.SourceVersion; | ||
|
||
/** | ||
* @author Oleg Khalidov ([email protected]) | ||
*/ | ||
@SupportedAnnotationTypes("*") | ||
@SupportedSourceVersion(SourceVersion.RELEASE_5) | ||
public class AndrojetaProcessor extends JetaProcessor { | ||
|
||
@Override | ||
|
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 |
---|---|---|
|
@@ -15,22 +15,75 @@ | |
*/ | ||
package org.brooth.androjeta.apt.processors; | ||
|
||
import com.squareup.javapoet.TypeSpec; | ||
import com.google.common.base.CaseFormat; | ||
import com.squareup.javapoet.*; | ||
import org.brooth.androjeta.ui.FindView; | ||
import org.brooth.androjeta.ui.FindViewMetacode; | ||
import org.brooth.jeta.apt.ProcessingContext; | ||
import org.brooth.jeta.apt.RoundContext; | ||
import org.brooth.jeta.apt.processors.AbstractProcessor; | ||
|
||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.Modifier; | ||
import java.util.Locale; | ||
|
||
/** | ||
* @author Oleg Khalidov ([email protected]) | ||
*/ | ||
public class FindViewProcessor extends AbstractProcessor { | ||
|
||
private String appPackage; | ||
|
||
public FindViewProcessor() { | ||
super(FindView.class); | ||
} | ||
|
||
@Override | ||
public void init(ProcessingContext processingContext) { | ||
super.init(processingContext); | ||
|
||
if (!processingContext.processingProperties().containsKey("application.package")) | ||
throw new IllegalStateException("Option 'application.package' not presented, set it in jeta.properties " + | ||
"in order to use @FindView"); | ||
|
||
appPackage = processingContext.processingProperties().getProperty("application.package"); | ||
} | ||
|
||
@Override | ||
public boolean process(TypeSpec.Builder builder, RoundContext roundContext) { | ||
ClassName masterClassName = ClassName.get(roundContext.metacodeContext().masterElement()); | ||
builder.addSuperinterface(ParameterizedTypeName.get( | ||
ClassName.get(FindViewMetacode.class), masterClassName)); | ||
|
||
MethodSpec.Builder methodBuilder = MethodSpec. | ||
methodBuilder("applyFindViews") | ||
.addAnnotation(Override.class) | ||
.addModifiers(Modifier.PUBLIC) | ||
.returns(void.class) | ||
.addParameter(masterClassName, "master") | ||
.addParameter(ClassName.bestGuess("android.app.Activity"), "activity"); | ||
|
||
for (Element element : roundContext.elements()) { | ||
String fieldName = element.getSimpleName().toString(); | ||
|
||
methodBuilder.addStatement("master.$L = ($T) activity.findViewById($L)", | ||
fieldName, TypeName.get(element.asType()), getResName(element, roundContext)); | ||
} | ||
|
||
builder.addMethod(methodBuilder.build()); | ||
|
||
return false; | ||
} | ||
|
||
private String getResName(Element element, RoundContext roundContext) { | ||
FindView annotation = element.getAnnotation(FindView.class); | ||
if (annotation.value() != -1) | ||
return String.valueOf(annotation.value()); | ||
|
||
String resName = !annotation.name().isEmpty() ? annotation.name() : | ||
CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_CAMEL, roundContext.metacodeContext().masterElement() | ||
.getSimpleName().toString() + '_' + element.getSimpleName().toString()); | ||
|
||
return String.format(Locale.ENGLISH, "%s.R.id.%s", appPackage, resName); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -15,11 +15,22 @@ | |
*/ | ||
package org.brooth.androjeta.apt.processors; | ||
|
||
import com.google.common.base.CaseFormat; | ||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.ParameterizedTypeName; | ||
import com.squareup.javapoet.TypeSpec; | ||
import org.brooth.androjeta.retain.Retain; | ||
import org.brooth.androjeta.retain.RetainMetacode; | ||
import org.brooth.jeta.apt.RoundContext; | ||
import org.brooth.jeta.apt.processors.AbstractProcessor; | ||
|
||
import javax.lang.model.element.Element; | ||
import javax.lang.model.element.Modifier; | ||
import javax.lang.model.type.TypeKind; | ||
import javax.lang.model.util.Elements; | ||
import javax.lang.model.util.Types; | ||
|
||
/** | ||
* @author Oleg Khalidov ([email protected]) | ||
*/ | ||
|
@@ -31,6 +42,67 @@ public RetainProcessor() { | |
|
||
@Override | ||
public boolean process(TypeSpec.Builder builder, RoundContext roundContext) { | ||
ClassName masterClassName = ClassName.get(roundContext.metacodeContext().masterElement()); | ||
builder.addSuperinterface(ParameterizedTypeName.get( | ||
ClassName.get(RetainMetacode.class), masterClassName)); | ||
|
||
MethodSpec.Builder saveMethodBuilder = MethodSpec. | ||
methodBuilder("applySaveRetains") | ||
.addAnnotation(Override.class) | ||
.addModifiers(Modifier.PUBLIC) | ||
.returns(void.class) | ||
.addParameter(masterClassName, "master") | ||
.addParameter(ClassName.bestGuess("android.os.Bundle"), "bundle"); | ||
|
||
MethodSpec.Builder restoreMethodBuilder = MethodSpec. | ||
methodBuilder("applyRestoreRetains") | ||
.addAnnotation(Override.class) | ||
.addModifiers(Modifier.PUBLIC) | ||
.returns(void.class) | ||
.addParameter(masterClassName, "master") | ||
.addParameter(ClassName.bestGuess("android.os.Bundle"), "bundle"); | ||
|
||
for (Element element : roundContext.elements()) { | ||
String fieldName = element.getSimpleName().toString(); | ||
String methodPostfix = getMethodPostfix(element); | ||
String key = roundContext.metacodeContext().masterElement().getSimpleName().toString() + "_" + fieldName; | ||
|
||
saveMethodBuilder.addStatement("bundle.put$L($S, master.$L)", | ||
methodPostfix, key, fieldName); | ||
|
||
restoreMethodBuilder.addStatement("master.$L = ($T) bundle.get$L($S)", | ||
fieldName, ClassName.get(element.asType()), methodPostfix, key); | ||
} | ||
|
||
builder.addMethod(saveMethodBuilder.build()); | ||
builder.addMethod(restoreMethodBuilder.build()); | ||
|
||
return false; | ||
} | ||
|
||
private String getMethodPostfix(Element element) { | ||
Elements elements = processingContext.processingEnv().getElementUtils(); | ||
Types types = processingContext.processingEnv().getTypeUtils(); | ||
|
||
TypeKind typeKind = element.asType().getKind(); | ||
if (typeKind.isPrimitive()) { | ||
switch (typeKind) { | ||
case INT: | ||
return "Int"; | ||
|
||
default: | ||
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, typeKind.name()); | ||
} | ||
} | ||
|
||
if (types.isAssignable(element.asType(), elements.getTypeElement("android.os.Parcelable").asType())) { | ||
return "Parcelable"; | ||
|
||
} else if (types.isAssignable(element.asType(), elements.getTypeElement("java.lang.String").asType())) { | ||
return "String"; | ||
|
||
} else { | ||
return "Serializable"; | ||
} | ||
} | ||
} |
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
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
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 |
---|---|---|
|
@@ -15,11 +15,11 @@ | |
*/ | ||
package org.brooth.androjeta.ui; | ||
|
||
import android.content.Context; | ||
import android.app.Activity; | ||
|
||
/** | ||
* @author Oleg Khalidov ([email protected]) | ||
*/ | ||
public interface FindViewMetacode<M> { | ||
void applyFindView(M master, Context context); | ||
void applyFindViews(M master, Activity activity); | ||
} |
Binary file not shown.
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,6 @@ | ||
#Wed Jan 13 07:29:48 BRT 2016 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-2.6-all.zip |
Oops, something went wrong.