Skip to content

Commit

Permalink
Merge pull request #59 from zeoflow/feature/hilt
Browse files Browse the repository at this point in the history
AutoGenerate class-ready for `@Hilt`
  • Loading branch information
teogor authored Jun 30, 2022
2 parents e5f0cbc + 45d03d5 commit fdf756d
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion app-java/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,20 @@ android {
}

dependencies {
/**
* Memo
*/
implementation(project(":memo"))
implementation(project(":memo-runtime"))
annotationProcessor annotationProcessor(project(':memo-compiler',))
annotationProcessor annotationProcessor(project(':memo-compiler'))

/**
* DI
*/
implementation "com.google.dagger:hilt-android:2.40.5"
annotationProcessor "com.google.dagger:hilt-android-compiler:2.40.5"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
annotationProcessor "androidx.hilt:hilt-compiler:1.0.0"

implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

import android.database.Observable;

import com.zeoflow.memo.common.Hilt;
import com.zeoflow.memo.common.KeyName;
import com.zeoflow.memo.common.MemoEntity;
import com.zeoflow.memo.common.MemoFunction;

@Hilt
@MemoEntity("Country")
public class Country<T>
{
Expand Down
11 changes: 11 additions & 0 deletions app-kotlin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ android {
}

dependencies {
/**
* Memo
*/
implementation(project(":memo"))
implementation(project(":memo-runtime"))
kapt(project(":memo-compiler-ktx"))

/**
* DI
*/
implementation "com.google.dagger:hilt-android:2.40.5"
kapt "com.google.dagger:hilt-android-compiler:2.40.5"
implementation "androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha03"
kapt "androidx.hilt:hilt-compiler:1.0.0"

implementation 'androidx.core:core-ktx:1.8.0'
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation 'com.google.android.material:material:1.6.1'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import com.zeoflow.memo.common.*
* it was annotated with @EncryptEntity("G15y3aV9M8d")
*/
@MemoEntity
//@EncryptEntity("G15y3aV9M8d")
@EncryptEntity("G15y3aV9M8d")
@Hilt
data class City(
@KeyName("name")
@Default(NameDefault::class)
Expand Down
3 changes: 3 additions & 0 deletions memo-common/src/main/java/com/zeoflow/memo/common/Hilt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.zeoflow.memo.common

public annotation class Hilt()
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ class JavaClassTypes : ClassTypes() {
)
}

override fun Inject(): Any {
return ClassName(
"javax.inject",
"Inject"
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,11 @@ class JavaClassTypes : ClassTypes() {
)
}

override fun Inject(): Any {
return ClassName.get(
"javax.inject",
"Inject"
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ abstract class ClassTypes {
abstract fun Observer(): Any
abstract fun LifecycleOwner(): Any

abstract fun Inject(): Any

}

enum class Language {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ class EntityBuilder(
.addKdoc("Code generated by Memo's Compiler. @see [link](https://github.com/zeoflow/memo)\n\n")
.addKdoc("Code generated based on the [${entityData.entityName}]")

if (entityData.isHilt) {
val primaryConstructor = FunSpec.constructorBuilder()
.addAnnotation(types.Inject() as ClassName)
.addKdoc(CodeBlock.of("Constructor ready for @Hilt"))
.build()
typeSpec.primaryConstructor(primaryConstructor)
}

addInitializer(typeSpec)

for (field in entityData.fields) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.zeoflow.memo.processor.entity

import androidx.annotation.NonNull
import com.squareup.javapoet.*
import com.squareup.kotlinpoet.FunSpec
import com.zeoflow.memo.processor.ClassTypes
import com.zeoflow.memo.processor.poet.getCustomType
import javax.lang.model.element.Modifier
Expand Down Expand Up @@ -32,19 +33,13 @@ class EntityBuilderJava(
"Preference class for \$T\n",
ClassName.get(
packageName(),
simpleName()
getClassName()
)
)
builder.addJavadoc(
"Generated by Memo's Injector (https://github.com/zeoflow/memo).\n"
)
builder.addModifiers(Modifier.PUBLIC)
builder.superclass(
ClassName.get(
packageName(),
simpleName()
)
)
builder.addFields(getFieldSpecs())
.addMethods(addInstancesSpec())
.addTypes(getOnChangedTypeSpecs())
Expand Down Expand Up @@ -94,7 +89,7 @@ class EntityBuilderJava(
FieldSpec.builder(
getCustomType(
packageName(),
simpleName()
getClassName()
),
FIELD_INSTANCE,
Modifier.PRIVATE,
Expand Down Expand Up @@ -135,7 +130,7 @@ class EntityBuilderJava(
.returns(
getCustomType(
packageName(),
simpleName()
getClassName()
)
)
.build()
Expand Down Expand Up @@ -190,18 +185,11 @@ class EntityBuilderJava(

private fun addConstructorsSpec(): List<MethodSpec> {
val methods: MutableList<MethodSpec> = java.util.ArrayList()
// val autoConstructor = MethodSpec.constructorBuilder()
// .addModifiers(Modifier.PRIVATE)
// .addStatement("this()")
// .build()
// methods.add(autoConstructor)
val constructor = MethodSpec.constructorBuilder()
.addModifiers(Modifier.PRIVATE)
// .addParameter(
// ParameterSpec.builder(AndroidTypeNames.CONTEXT, CONSTRUCTOR_CONTEXT)
// .addAnnotation(NonNull::class.java)
// .build()
// )
.addModifiers(Modifier.PUBLIC)
if (entityData.isHilt) {
constructor.addAnnotation(types.Inject() as ClassName)
}
if (entityData.isEncryption) {
constructor.addStatement(
"\$T.Companion.init().withEncryption(new \$T(\$N)).build()",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class EntityData(
val getterCompoundFunctionsList: MutableMap<Array<String>, ExecutableElement>
var isDefaultPreference = false
var isEncryption = false
var isHilt = false
var encryptionKey: String? = null

init {
Expand All @@ -38,6 +39,9 @@ class EntityData(
val encryptEntity = annotatedType.getAnnotation(
com.zeoflow.memo.common.EncryptEntity::class
)
val hiltEntity = annotatedType.getAnnotation(
com.zeoflow.memo.common.Hilt::class
)

packageName = annotatedType.packageName
typeElement = annotatedType
Expand All @@ -64,6 +68,10 @@ class EntityData(
isEncryption = true
encryptionKey = encryptEntity.value.value
}

if (hiltEntity != null) {
isHilt = true
}
//
// if (Strings.isNullOrEmpty(entityName)) {
// throw VerifyException("You should entity MemoStorage class value.")
Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencyResolutionManagement {
mavenCentral()
}
}
rootProject.name = "Memo-KTX"
rootProject.name = "Memo"

/**
* APP MODULES (DEMOS)
Expand Down

0 comments on commit fdf756d

Please sign in to comment.