package com.cj.apifinder
import java.lang.reflect.Executable
import com.cj.apifinder.SpecFinder.FileWithApi
import io.github.lukehutch.fastclasspathscanner.FastClasspathScanner
import io.github.lukehutch.fastclasspathscanner.matchprocessor.MethodAnnotationMatchProcessor
import org.springframework.web.bind.annotation.RequestMapping
import scala.collection.mutable
import scala.collection.mutable.ListBuffer
object AnnotationApiFinder {
def main(args: Array[String]): Unit = {
findApisViaAnnotations().foreach( println(_) )
}
def findApisViaAnnotations(): Seq[FileWithApi] = {
val results = mutable.ListBuffer[FileWithApi]()
val scanner = new FastClasspathScanner("cj","com.cj")
.matchClassesWithMethodAnnotation(classOf[RequestMapping], new RequestMappingAnnotationMatchProcessor(results))
.scan()
results
}
}
class RequestMappingAnnotationMatchProcessor(filesWithApiUrls: ListBuffer[FileWithApi]) extends MethodAnnotationMatchProcessor {
override def processMatch(aClass: Class[_], executable: Executable): Unit = {
val requestMapping : RequestMapping = executable.getAnnotation(classOf[RequestMapping])
if ( requestMapping != null )
filesWithApiUrls ++= requestMapping.value.map( apiUrl => FileWithApi(aClass.getCanonicalName, Option(apiUrl)) )
else
println( aClass.getCanonicalName + " : Why no RequestMapping?" )
}
}
package com.cj.apifinder
import com.cj.apifinder.SpecFinder.FileWithApi
import io.github.classgraph.AnnotationInfoList.AnnotationInfoFilter
import io.github.classgraph.{AnnotationInfo, AnnotationParameterValue, ClassGraph, ClassInfoList}
import scala.collection.JavaConverters._
object RequestMappingFinder {
def main(args: Array[String]): Unit = {
findApisViaAnnotations().foreach( println(_) )
}
def findApisViaAnnotations(): Seq[FileWithApi] = {
val scanResult = new ClassGraph()
.whitelistPackages("cj","com.cj")
.enableAllInfo()
.scan()
apisIn(scanResult.getClassesWithAnnotation("org.springframework.web.bind.annotation.RequestMapping"))
.union(apisIn(scanResult.getClassesWithMethodAnnotation("org.springframework.web.bind.annotation.RequestMapping")))
}
def apisIn(annotatedClasses: ClassInfoList): Seq[FileWithApi] = {
annotatedClasses.getNames.asScala
.flatMap(className => {
annotatedClasses.get(className)
.getAnnotationInfo
.filter(new AnnotationInfoFilter {
override def accept(annotationInfo: AnnotationInfo): Boolean = {
return annotationInfo.getName.equals("org.springframework.web.bind.annotation.RequestMapping")
}
})
.asScala.map(annotationInfo => {
val annotationValueHolder = annotationInfo.getParameterValues.asScala
.find({ paramValue: AnnotationParameterValue => paramValue.getName.equals("value") })
val annotationValue = if (annotationValueHolder.isEmpty) None
else Option(convertValue(annotationValueHolder.get.getValue))
new FileWithApi(className, annotationValue)
})
})
}
def convertValue(value: Object ): String = {
if ( value.isInstanceOf[Array[Object]] ) {
value.asInstanceOf[Array[Object]](0).toString
} else {
value.toString
}
}
}
--
You received this message because you are subscribed to the Google Groups "ClassGraph-Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to classgraph-use...@googlegroups.com.
To post to this group, send email to classgra...@googlegroups.com.
Visit this group at https://groups.google.com/group/classgraph-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/classgraph-users/26c8443a-db96-4575-9c2d-f8b1fc43d262%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
apisIn(scanResult.getClassesWithAnnotation("org.springframework.web.bind.annotation.RequestMapping"))
.union(apisIn(scanResult.getClassesWithMethodAnnotation("org.springframework.web.bind.annotation.RequestMapping")))
annotatedClasses.getNames.asScala
.flatMap(className => {
annotatedClasses.get(className)
.getAnnotationInfo
.filter(new AnnotationInfoFilter {
override def accept(annotationInfo: AnnotationInfo): Boolean = {
return annotationInfo.getName.equals("org.springframework.web.bind.annotation.RequestMapping")
}
})
def findApisViaAnnotations(): Seq[FileWithApi] = {
val scanResult = new ClassGraph()
.whitelistPackages("cj","com.cj")
.enableAllInfo()
.scan()
apisDeclaredAtClassLevel(scanResult.getClassesWithAnnotation("org.springframework.web.bind.annotation.RequestMapping"))
.union(apisDeclaredAtMethodLevel(scanResult.getClassesWithMethodAnnotation("org.springframework.web.bind.annotation.RequestMapping")))
}
def apisDeclaredAtClassLevel(annotatedClasses: ClassInfoList): Seq[FileWithApi] = {
annotatedClasses.asScala
.flatMap(classInfo => {
classInfo.getAnnotationInfo
.filter(new AnnotationInfoFilter {
override def accept(annotationInfo: AnnotationInfo): Boolean = {
return annotationInfo.getName.equals("org.springframework.web.bind.annotation.RequestMapping")
}
})
.asScala.map(annotationInfo => {
val annotationValueHolder = annotationInfo.getParameterValues.asScala
.find({ paramValue: AnnotationParameterValue => paramValue.getName.equals("value") })
val annotationValue = if (annotationValueHolder.isEmpty) None
else Option(convertValue(annotationValueHolder.get.getValue))
new FileWithApi(classInfo.getName, annotationValue)
})
})
}
def apisDeclaredAtMethodLevel(annotatedClasses: ClassInfoList): Seq[FileWithApi] = {
annotatedClasses.asScala
.flatMap(classInfo => {
classInfo.getMethodInfo.asScala
.flatMap( methodInfo => methodInfo.getAnnotationInfo
.filter(new AnnotationInfoFilter {
override def accept(annotationInfo: AnnotationInfo): Boolean = {
return annotationInfo.getName.equals("org.springframework.web.bind.annotation.RequestMapping")
}
})
.asScala.map(annotationInfo => {
val annotationValueHolder = annotationInfo.getParameterValues.asScala
.find({ paramValue: AnnotationParameterValue => paramValue.getName.equals("value") })
val annotationValue = if (annotationValueHolder.isEmpty) None
else Option(convertValue(annotationValueHolder.get.getValue))
new FileWithApi(classInfo.getName, annotationValue)
}))
})
}
--
You received this message because you are subscribed to the Google Groups "ClassGraph-Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to classgraph-use...@googlegroups.com.
To post to this group, send email to classgra...@googlegroups.com.
Visit this group at https://groups.google.com/group/classgraph-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/classgraph-users/8a339feb-c064-497d-b9fa-217351dc097f%40googlegroups.com.
Looks right to me. Do I need to add any specific clarifications to the docs that would have made it easier for you to have figured this out?
--
You received this message because you are subscribed to the Google Groups "ClassGraph-Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to classgraph-use...@googlegroups.com.
To post to this group, send email to classgra...@googlegroups.com.
Visit this group at https://groups.google.com/group/classgraph-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/classgraph-users/702ba9ee-a474-4dc8-a2e7-42803d4c7f19%40googlegroups.com.