Skip to content

Commit

Permalink
retain & findview
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg Khalidov committed Jan 13, 2016
1 parent 9202eb6 commit fc11771
Show file tree
Hide file tree
Showing 13 changed files with 413 additions and 35 deletions.
16 changes: 1 addition & 15 deletions androjeta-apt/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@
* limitations under the License.
*/

buildscript {
repositories {
jcenter()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.4'
classpath 'net.ltgt.gradle:gradle-apt-plugin:0.4'
}
}

apply plugin: 'java'

repositories{
Expand All @@ -40,5 +27,4 @@ repositories{
dependencies {
compile project(':androjeta')
compile 'org.brooth.jeta:jeta-apt:0.3-alpha'
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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])
*/
Expand All @@ -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";
}
}
}
12 changes: 0 additions & 12 deletions androjeta/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,7 @@
def androidBuildVersion = 21
def androidHome = System.getenv('ANDROID_HOME')

buildscript {
repositories {
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
classpath 'net.ltgt.gradle:gradle-apt-plugin:0.4'
}
}

apply plugin: 'java'
apply plugin: 'net.ltgt.apt'

repositories {
mavenLocal()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public class RetainController extends MasterController<Object, RetainMetacode<Object>> {

protected RetainController(Metasitory metasitory, Object master) {
public RetainController(Metasitory metasitory, Object master) {
super(metasitory, master, Retain.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
@Target(ElementType.FIELD)
public @interface FindView {

int value() default -1;
String id() default "";

String name() default "";
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.brooth.androjeta.ui;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import org.brooth.jeta.MasterController;
import org.brooth.jeta.metasitory.Metasitory;
Expand All @@ -24,12 +26,21 @@
*/
public class FindViewController extends MasterController<Object, FindViewMetacode<Object>> {

protected FindViewController(Metasitory metasitory, Object master) {
public FindViewController(Metasitory metasitory, Object master) {
super(metasitory, master, FindView.class);
}

public void findViews(Context context) {
public void findViews() {
if (master instanceof Activity) {
findViews((Activity) master);
return;
}

throw new IllegalStateException("Master is not an activity, use findViews(Activity activity) instead");
}

public void findViews(Activity activity) {
for (FindViewMetacode<Object> metacode : metacodes)
metacode.applyFindView(master, context);
metacode.applyFindViews(master, activity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 added gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
6 changes: 6 additions & 0 deletions gradle/wrapper/gradle-wrapper.properties
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
Loading

0 comments on commit fc11771

Please sign in to comment.