Advertisement
Guest User

Untitled

a guest
Jul 6th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.40 KB | None | 0 0
  1. package annotations
  2.  
  3. import java.beans.Introspector
  4. import java.io.IOException
  5. import java.util.*
  6. import javax.annotation.processing.*
  7. import javax.lang.model.SourceVersion
  8. import javax.lang.model.element.*
  9. import javax.tools.Diagnostic
  10.  
  11. /**
  12.  * Created by JCD on 06.07.2017.
  13.  */
  14.  
  15. //open class SimpleBeanInfo {
  16. //    open fun getPropertyDescriptions() {
  17. //        //
  18. //    }
  19. //}
  20. //class BeanClassBeanInfo : SimpleBeanInfo()
  21.  
  22. @MustBeDocumented
  23. @Target(AnnotationTarget.FUNCTION)
  24. @Retention(AnnotationRetention.SOURCE)
  25. annotation class Property(val editor: String = "")
  26.  
  27. class BeanInfoAnnotationFactory : Processor {
  28.  
  29.     override fun getSupportedAnnotationTypes(): MutableSet<String> {
  30.         return mutableSetOf("Property")
  31.     }
  32.  
  33.     override fun getSupportedOptions(): MutableSet<String> {
  34.         return mutableSetOf()
  35.     }
  36.  
  37.     override fun init(env: ProcessingEnvironment) {
  38.         getAnnotationFor(env)
  39.     }
  40.  
  41.     override fun process(annotations: MutableSet<out TypeElement>, env: RoundEnvironment): Boolean {
  42.         env.rootElements
  43.     }
  44.  
  45.     override fun getCompletions(element: Element?, annotation: AnnotationMirror?, member: ExecutableElement?, userText: String?): MutableIterable<Completion> {
  46.         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
  47.     }
  48.  
  49.     override fun getSupportedSourceVersion(): SourceVersion {
  50.         TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
  51.     }
  52.  
  53.     private fun getAnnotationFor(env: ProcessingEnvironment) {
  54.         val typeElement = env.elementUtils.getTypeElement("Property")
  55.         for (t in env.elementUtils.getAllMembers(typeElement)) {
  56.             if (t.modifiers.contains(Modifier.PUBLIC)) {
  57.                 println(t)
  58.                 val props = TreeMap<String, Property>()
  59.                 val enclosing = t.enclosingElement
  60.                 println("ENCLOSING $enclosing")
  61.                 for(m in t.javaClass.methods) {
  62.                     val p = m.getAnnotation(Property::class.java)
  63.                     if(p != null) {
  64.                         val mname = m.name
  65.                         val prefixes = listOf("get", "set", "is")
  66.                         var found = false
  67.                         for(i in 0 until prefixes.size) {
  68.                             if(mname.startsWith(prefixes[i])) {
  69.                                 found = true
  70.                                 val start = prefixes[i].length
  71.                                 val name = Introspector.decapitalize(mname.substring(start))
  72.                                 props.put(name, p)
  73.                             }
  74.                             if(!found) {
  75.                                 env.messager.printMessage(Diagnostic.Kind.ERROR, "@Property must be applied to getXxx, setXxx or isXxx method")
  76.                             }
  77.                         }
  78.                         try {
  79.                             if(props.isNotEmpty()) {
  80.                                 writeBeanInfoFile(typeElement.qualifiedName, props)
  81.                             }
  82.                         } catch (e: IOException) {
  83.                             e.printStackTrace()
  84.                         }
  85.                     }
  86.                 }
  87.  
  88.             }
  89.         }
  90.     }
  91.  
  92.     fun writeBeanInfoFile(beanClassName: String, props: Map<String, Property>) {
  93.         // TODO
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement