Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package annotations
- import java.beans.Introspector
- import java.io.IOException
- import java.util.*
- import javax.annotation.processing.*
- import javax.lang.model.SourceVersion
- import javax.lang.model.element.*
- import javax.tools.Diagnostic
- /**
- * Created by JCD on 06.07.2017.
- */
- //open class SimpleBeanInfo {
- // open fun getPropertyDescriptions() {
- // //
- // }
- //}
- //class BeanClassBeanInfo : SimpleBeanInfo()
- @MustBeDocumented
- @Target(AnnotationTarget.FUNCTION)
- @Retention(AnnotationRetention.SOURCE)
- annotation class Property(val editor: String = "")
- class BeanInfoAnnotationFactory : Processor {
- override fun getSupportedAnnotationTypes(): MutableSet<String> {
- return mutableSetOf("Property")
- }
- override fun getSupportedOptions(): MutableSet<String> {
- return mutableSetOf()
- }
- override fun init(env: ProcessingEnvironment) {
- getAnnotationFor(env)
- }
- override fun process(annotations: MutableSet<out TypeElement>, env: RoundEnvironment): Boolean {
- env.rootElements
- }
- override fun getCompletions(element: Element?, annotation: AnnotationMirror?, member: ExecutableElement?, userText: String?): MutableIterable<Completion> {
- TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
- }
- override fun getSupportedSourceVersion(): SourceVersion {
- TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
- }
- private fun getAnnotationFor(env: ProcessingEnvironment) {
- val typeElement = env.elementUtils.getTypeElement("Property")
- for (t in env.elementUtils.getAllMembers(typeElement)) {
- if (t.modifiers.contains(Modifier.PUBLIC)) {
- println(t)
- val props = TreeMap<String, Property>()
- val enclosing = t.enclosingElement
- println("ENCLOSING $enclosing")
- for(m in t.javaClass.methods) {
- val p = m.getAnnotation(Property::class.java)
- if(p != null) {
- val mname = m.name
- val prefixes = listOf("get", "set", "is")
- var found = false
- for(i in 0 until prefixes.size) {
- if(mname.startsWith(prefixes[i])) {
- found = true
- val start = prefixes[i].length
- val name = Introspector.decapitalize(mname.substring(start))
- props.put(name, p)
- }
- if(!found) {
- env.messager.printMessage(Diagnostic.Kind.ERROR, "@Property must be applied to getXxx, setXxx or isXxx method")
- }
- }
- try {
- if(props.isNotEmpty()) {
- writeBeanInfoFile(typeElement.qualifiedName, props)
- }
- } catch (e: IOException) {
- e.printStackTrace()
- }
- }
- }
- }
- }
- }
- fun writeBeanInfoFile(beanClassName: String, props: Map<String, Property>) {
- // TODO
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement