Skip to content
This repository has been archived by the owner on May 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #34 from 2tu/develop
Browse files Browse the repository at this point in the history
fixed clean object fields
  • Loading branch information
zongbintu authored Nov 30, 2017
2 parents 7839910 + e65adde commit 5964d32
Show file tree
Hide file tree
Showing 15 changed files with 294 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android:
- extra-android-m2repository
script:
- chmod +x gradlew
- ./gradlew clean build
- ./gradlew clean build --debug

# Disable Travis container-based infrastructure to correct
# resource consumption in gradle presumably due to dexing.
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### Version 1.0.1(2017-11-30)

* Fix:clear Object property.

### Version 1.0.0(2017-09-25)

* New:save Enum,Array,Serializable.
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ Add the following dependency to your `build.gradle` file:

```
dependencies {
compile 'com.github.2tu.fit:fit:1.0.0'
annotationProcessor 'com.github.2tu.fit:fit-compiler:1.0.0'
compile 'com.github.2tu.fit:fit:1.0.1'
annotationProcessor 'com.github.2tu.fit:fit-compiler:1.0.1'
//use apt
//apt 'com.github.2tu.fit:fit-compiler:1.0.0'
//apt 'com.github.2tu.fit:fit-compiler:1.0.1'
}
```

Expand All @@ -46,14 +46,15 @@ User user = Fit.get(context, "user", User.class);
clear
```java
Fit.clear(context, User.class);
Fit.clear(context, "user");
Fit.clear(context, User.class, "user");
```
other
```java
Fit.get(context, "name").getBoolean("isFirst", false);
SharedPreferences.Editor editor = Fit.edit(context, "name");
editor.putBoolean("isFirst", true);
editor.apply();
Fit.clearObjectField(cotext, "user", "hobbies");
```

## Q&A
Expand Down
105 changes: 83 additions & 22 deletions fit-compiler/src/main/java/fit/compiler/FitProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import fit.SharedPreferenceAble;
import java.beans.PropertyDescriptor;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
Expand Down Expand Up @@ -48,11 +51,11 @@
private static final ClassName STRING = ClassName.get("java.lang", "String");

//Set<String>
TypeName hoverboard = TypeName.get(String.class);
TypeName stringTypeName = TypeName.get(String.class);
ClassName set = ClassName.get("java.util", "Set");
ClassName hashSet = ClassName.get("java.util", "HashSet");
TypeName setOfHoverboards = ParameterizedTypeName.get(set, hoverboard);
TypeName hashSetOfHoverboards = ParameterizedTypeName.get(hashSet, hoverboard);
TypeName setOfHoverboards = ParameterizedTypeName.get(set, stringTypeName);
TypeName hashSetOfHoverboards = ParameterizedTypeName.get(hashSet, stringTypeName);

private static final String METHOD_GET_STRING = "getString";
private static final String METHOD_GET_Int = "getInt";
Expand Down Expand Up @@ -97,7 +100,6 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
if (targetType instanceof ParameterizedTypeName) {
targetType = ((ParameterizedTypeName) targetType).rawType;
}

String packageName = getPackageName(enclosingElement);

try {
Expand All @@ -119,7 +121,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
if (modifiers.contains(Modifier.STATIC)) {
continue;
}

if (kind == ElementKind.FIELD && !modifiers.contains(Modifier.TRANSIENT)) {

if (modifiers.contains(Modifier.PRIVATE)) {
privateFieldElements.add(memberElement);
} else {
Expand Down Expand Up @@ -165,6 +169,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
throw new RuntimeException("Fit can't use no non-parameter constructor");
}

Set<fit.compiler.PropertyDescriptor> getterPropertyDescriptors = new HashSet<>();
Set<fit.compiler.PropertyDescriptor> setterPropertyDescriptors = new HashSet<>();

//过滤getter
Set<Element> getterElements = new HashSet<>();
for (Element method : suspectedGetterElements) {
Expand All @@ -175,6 +182,10 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
for (Element field : privateFieldElements) {
if (field.getSimpleName().toString().equalsIgnoreCase(propertyName)) {
getterElements.add(method);
fit.compiler.PropertyDescriptor propertyDescriptor = new fit.compiler.PropertyDescriptor();
propertyDescriptor.setField(field);
propertyDescriptor.setGetter(method);
getterPropertyDescriptors.add(propertyDescriptor);
break;
}
}
Expand All @@ -188,14 +199,18 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
for (Element field : privateFieldElements) {
if (field.getSimpleName().toString().equalsIgnoreCase(propertyName)) {
setterElements.add(method);
fit.compiler.PropertyDescriptor propertyDescriptor = new fit.compiler.PropertyDescriptor();
propertyDescriptor.setField(field);
propertyDescriptor.setSetter(method);
setterPropertyDescriptors.add(propertyDescriptor);
break;
}
}
}

JavaFile javaFile = JavaFile.builder(preferenceClassName.packageName(),
createPreferenceClass(preferenceClassName, isFinal, targetType, fieldElements,
getterElements, setterElements))
getterPropertyDescriptors, setterPropertyDescriptors))
.addFileComment("Generated code from Fit. Do not modify!")
.build();
javaFile.writeTo(filer);
Expand Down Expand Up @@ -258,8 +273,8 @@ private static String getClassName(TypeElement type, String packageName) {
}

private TypeSpec createPreferenceClass(ClassName preferenceClassName, boolean isFinal,
TypeName targetTypeName, Set<Element> fieldElements, Set<Element> getterElements,
Set<Element> setterElement) {
TypeName targetTypeName, Set<Element> fieldElements, Set<fit.compiler.PropertyDescriptor> getterPropertyDescriptors,
Set<fit.compiler.PropertyDescriptor> setterPropertyDescriptors) {
TypeSpec.Builder result =
TypeSpec.classBuilder(preferenceClassName.simpleName()).addModifiers(PUBLIC);

Expand All @@ -270,15 +285,17 @@ private TypeSpec createPreferenceClass(ClassName preferenceClassName, boolean is
ParameterizedTypeName parameterizedTypeName = ParameterizedTypeName.get(MM, targetTypeName);
result.addSuperinterface(parameterizedTypeName);

result.addMethod(createPreferenceSaveMethod(targetTypeName, fieldElements, getterElements));
result.addMethod(createPreferenceSaveMethod(targetTypeName, fieldElements, getterPropertyDescriptors));

result.addMethod(createPreferenceGetMethod(targetTypeName, fieldElements, setterElement));
result.addMethod(createPreferenceGetMethod(targetTypeName, fieldElements, setterPropertyDescriptors));

result.addMethod(createPreferenceClearFieldsMethod(fieldElements, getterPropertyDescriptors));

return result.build();
}

private MethodSpec createPreferenceSaveMethod(TypeName targetType, Set<Element> fieldElements,
Set<Element> getterElements) {
Set<fit.compiler.PropertyDescriptor> getterPropertyDescriptors) {
MethodSpec.Builder result = MethodSpec.methodBuilder("save")
.returns(SHARED_PREFERENCES_EDITOR)
.addAnnotation(Override.class)
Expand All @@ -297,15 +314,15 @@ private MethodSpec createPreferenceSaveMethod(TypeName targetType, Set<Element>
}

//getter
for (Element method : getterElements) {
for (fit.compiler.PropertyDescriptor propertyDescriptor : getterPropertyDescriptors) {
Element method = propertyDescriptor.getGetter();
TypeMirror typeMirror = ((ExecutableType) method.asType()).getReturnType();
TypeName typeName = TypeName.get(typeMirror);
String putMethod = genPutMethod(typeName);
String valueL = method.toString();

result = genSaveCode(result, typeName, putMethod, valueL,
valueL.startsWith("get") ? valueL.substring(3, valueL.length() - 2)
: valueL.substring(2, valueL.length() - 2));
propertyDescriptor.getField().getSimpleName().toString());
}

result = result.addStatement("return editor");
Expand All @@ -317,7 +334,7 @@ private String genPutMethod(TypeName fieldTypeName) {
TypeName unboxFieldTypeName = unbox(fieldTypeName);
String putMethod = "";

if (TypeName.get(String.class).equals(unboxFieldTypeName)) {
if (stringTypeName.equals(unboxFieldTypeName)) {
putMethod = "putString";
} else if (TypeName.BOOLEAN.equals(unboxFieldTypeName)) {
putMethod = "putBoolean";
Expand Down Expand Up @@ -370,7 +387,7 @@ private MethodSpec.Builder genSaveCode(MethodSpec.Builder builder, TypeName type
propertyName, UTILS, propertyName, propertyName);
}
} else {
if (TypeName.get(String.class).equals(unboxFieldTypeName) || typeName.isPrimitive()) {
if (stringTypeName.equals(unboxFieldTypeName) || typeName.isPrimitive()) {
builder.addStatement("editor.$L($S, obj." + valueL + ")", putMethod, propertyName);
} else {
builder.addStatement("$T.writeObject(context, name + $S, obj.$L)", FILE_OBJECT_UTIL,
Expand All @@ -381,7 +398,7 @@ private MethodSpec.Builder genSaveCode(MethodSpec.Builder builder, TypeName type
}

private MethodSpec createPreferenceGetMethod(TypeName targetType, Set<Element> fieldElements,
Set<Element> setterElement) {
Set<fit.compiler.PropertyDescriptor> setterPropertyDescriptors) {
MethodSpec.Builder result = MethodSpec.methodBuilder("get")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
Expand All @@ -398,9 +415,10 @@ private MethodSpec createPreferenceGetMethod(TypeName targetType, Set<Element> f
}

//setter
for (Element method : setterElement) {
for (fit.compiler.PropertyDescriptor propertyDescriptor : setterPropertyDescriptors) {
Element method =propertyDescriptor.getSetter();
TypeMirror typeMirror = ((ExecutableType) method.asType()).getParameterTypes().get(0);
genGetCode(true, result, typeMirror, method.getSimpleName().toString().substring(3),
genGetCode(true, result, typeMirror, propertyDescriptor.getField().getSimpleName().toString(),
"obj." + method.getSimpleName() + "(");
}
result.addStatement("return obj").returns(targetType);
Expand All @@ -414,7 +432,7 @@ private MethodSpec.Builder genGetCode(boolean isSetter, MethodSpec.Builder build
String defaultValue = "0";
String cast = "";
String value = "$L sharedPreferences.$L($S, $L)";
if (TypeName.get(String.class).equals(fieldTypeName)) {
if (stringTypeName.equals(fieldTypeName)) {
method = METHOD_GET_STRING;
defaultValue = null;
} else if (TypeName.BOOLEAN.equals(fieldTypeName)) {
Expand Down Expand Up @@ -469,6 +487,52 @@ private MethodSpec.Builder genGetCode(boolean isSetter, MethodSpec.Builder build
propertyName, defaultValue);
}

private MethodSpec createPreferenceClearFieldsMethod(Set<Element> fieldElements,
Set<fit.compiler.PropertyDescriptor> getterPropertyDescriptors) {
MethodSpec.Builder result = MethodSpec.methodBuilder("clearFields")
.addAnnotation(Override.class)
.addModifiers(PUBLIC)
.addParameter(CONTEXT, "context")
.addParameter(STRING, "name");

//属性
for (Element element : fieldElements) {
TypeName typeName = TypeName.get(element.asType());
if (isObject(typeName)) {
result.addStatement("$T.deleteFile(context, name + $S)", FILE_OBJECT_UTIL,
"." + element.getSimpleName().toString());
}
}

//getter
for (fit.compiler.PropertyDescriptor propertyDescriptor :getterPropertyDescriptors){

Element element = propertyDescriptor.getGetter();
TypeMirror typeMirror = ((ExecutableType) element.asType()).getReturnType();
TypeName typeName = TypeName.get(typeMirror);
String valueL = element.toString();

if (isObject(typeName)) {
result.addStatement("$T.deleteFile(context, name + $S)", FILE_OBJECT_UTIL,
"." + propertyDescriptor.getField().getSimpleName().toString());
}
}
return result.build();
}

/**
* @param typeName {@link Type}
* @return object is true,otherwise false
* @since 1.0.1
*/
private boolean isObject(TypeName typeName) {
typeName = typeName.box();
return !(typeName.isBoxedPrimitive()
|| stringTypeName.equals(typeName)
|| setOfHoverboards.equals(typeName)
|| hashSetOfHoverboards.equals(typeName));
}

private TypeName unbox(TypeName typeName) {
if (typeName.isBoxedPrimitive()) {
return typeName.unbox();
Expand Down Expand Up @@ -500,9 +564,6 @@ private void printMessage(Diagnostic.Kind kind, Element element, String message,
}

@Override public Set<String> getSupportedAnnotationTypes() {
//Set<Class<? extends Annotation>> annotations = new LinkedHashSet<>();
//annotations.add(SharedPreferenceAble.class);
//return annotations;
return Collections.singleton(SharedPreferenceAble.class.getCanonicalName());
}

Expand Down
38 changes: 38 additions & 0 deletions fit-compiler/src/main/java/fit/compiler/PropertyDescriptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package fit.compiler;

import javax.lang.model.element.Element;

/**
* @author Tu enum@foxmail.com.
* @since 1.0.1
*/

public class PropertyDescriptor {
private Element getter;
private Element setter;
private Element field;

public Element getGetter() {
return getter;
}

public void setGetter(Element getter) {
this.getter = getter;
}

public Element getSetter() {
return setter;
}

public void setSetter(Element setter) {
this.setter = setter;
}

public Element getField() {
return field;
}

public void setField(Element field) {
this.field = field;
}
}
9 changes: 9 additions & 0 deletions fit-compiler/src/test/java/fit/MM.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ public interface MM<T> {
SharedPreferences.Editor save(Context context, String name, T obj);

T get(Context context, String name);

/**
* clear Object fields
*
* @param context {@link Context}
* @param name SharedPreference's name
* @since 1.0.1
*/
void clearFields(Context context, String name);
}
Loading

0 comments on commit 5964d32

Please sign in to comment.