Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove EntityScanner #1141

Closed
evanchooly opened this issue Jan 24, 2017 · 2 comments
Closed

Remove EntityScanner #1141

evanchooly opened this issue Jan 24, 2017 · 2 comments
Milestone

Comments

@evanchooly
Copy link
Member

No description provided.

@evanchooly evanchooly added this to the 2.0.0 milestone Jan 24, 2017
evanchooly pushed a commit that referenced this issue Jan 24, 2017
trishagee added a commit to trishagee/morphia that referenced this issue Mar 30, 2017
trishagee added a commit to trishagee/morphia that referenced this issue Mar 30, 2017
trishagee added a commit to trishagee/morphia that referenced this issue Mar 30, 2017
@richarddd
Copy link
Contributor

This might be something rather than scan for enties, currently using in my project:

import com.google.auto.service.AutoService
import com.squareup.javapoet.ArrayTypeName
import com.squareup.javapoet.CodeBlock
import com.squareup.javapoet.JavaFile
import com.squareup.javapoet.TypeSpec
import org.mongodb.morphia.annotations.Entity
import java.io.File
import javax.annotation.processing.AbstractProcessor
import javax.annotation.processing.Processor
import javax.annotation.processing.RoundEnvironment
import javax.lang.model.SourceVersion
import javax.lang.model.element.Modifier
import javax.lang.model.element.TypeElement

@AutoService(Processor::class)
class EntityCollectionGenerator : AbstractProcessor() {

    override fun process(annotations: MutableSet<out TypeElement>, roundEnv: RoundEnvironment): Boolean {
        val classes = roundEnv.getElementsAnnotatedWith(Entity::class.java).map {
            "${processingEnv.elementUtils.getPackageOf(it)}.${it.simpleName}"
        }

        if (classes.isNotEmpty()) {

            val generatedClass = TypeSpec.classBuilder("EntityCollection").apply {

                val literal = "{${classes.joinToString { "$it.class" }}};\n"

                val classArray = ArrayTypeName.of(Class::class.java)
                addField(classArray, "COLLECTIONS", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
                addStaticBlock(CodeBlock.builder().add("COLLECTIONS = new $1T $2L", classArray, literal).build())
                addModifiers(Modifier.PUBLIC)
            }.build()

            val file = JavaFile.builder(EntityCollectionGenerator::class.java.`package`.name, generatedClass)
                .build()

            val kaptKotlinGeneratedDir = processingEnv.options[KAPT_KOTLIN_GENERATED_OPTION_NAME]
            file.writeTo(File(kaptKotlinGeneratedDir))
        }

        return true
    }

    override fun getSupportedAnnotationTypes(): MutableSet<String> {
        println("getSupportedAnnotationTypes")
        return mutableSetOf(Entity::class.java.name)
    }

    override fun getSupportedSourceVersion(): SourceVersion {
        println("getSupportedSourceVersion")
        return SourceVersion.latest()
    }

    companion object {
        const val KAPT_KOTLIN_GENERATED_OPTION_NAME = "kapt.kotlin.generated"
    }
}

Its a generator creating a static class containing all the entity classes and will generate a class like this:

public class EntityCollection {
  public static final Class[] COLLECTIONS;

  static {
    COLLECTIONS = new Class[] {...};
  }
}

@evanchooly
Copy link
Member Author

this is done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants