How to get Class from Element in Java

** During Annotation Processing, the target class is not loaded in the ClassLoader on the processor side, so you cannot get the Class by this method **

~~ Get Class from # Element ~~

~~-Type Element If (Element of class), Class of that Element ~~ ~~-Variable Element (variables such as fields / enum constants) The variable type Class ~~

Get ~~. There are about 3 other types of Elements, but I haven't written them because they haven't been verified and there seems to be no need to get Class from them. ~~

~~ I don't care about the mechanism, just move it for the time being! If you are a person, please use this code. Please copy and paste it in a suitable place. ~~

fun Element.getDeclaredClass(): Class<*> = ((asType() as DeclaredType).asElement() as TypeElement).qualifiedName.let { name -> ClassLoader.getSystemClassLoader().loadClass(name.toString())}

Since it is an extension function of ~~ Element, getDeclaredClass () is called and used as it is from the Element fetched from around roundEnv.getElementsAnnotatedWith (). ~~

~~ ## Process for getting Class from Element ~~

~~ 1. Call ʻElement # asType ()to get the TypeMirror of the type. 2. Cast the obtained value to DeclaredType. 3. CallDeclaredType # asElement ()` to get the Element of type. 4. Cast the obtained value to TypeElement 5. Get the binary name (fully qualified name) of the class from TypeElement 6. Get Class from ClassLoader using binary name ~~

~~ After all, you can't convert Element directly to Class. You will get the Class through the ClassLoader. ~~

~~ ʻAbout Element # asType`. JavaDoc is ~~

~~> Returns the type defined by this element. ~~

It says ~~. This returns the type of the variable if it's a Variable Element, or the type that the Element itself represents if it's a Type Element. By the way, the Element acquired in 3 is the same as the first Element, so in the case of Type Element, skipping 1 to 4 and 5 and 6 will be enough. How 1 ~ 4 get the Type Element of type from Variable Element How 5 ~ 6 get Class from Type Element It will be. ~~

Try to code

~~ Here is the code I actually wrote ~~

fun Element.getDeclaredClass(): Class<*> {
    return ((asType() as DeclaredType).asElement() as TypeElement).qualifiedName.let { name ->
        ClassLoader.getSystemClassLoader().loadClass(name.toString())
    }
}

~~ Would it be like this if written in Java? ~~

public Class<?> getDEclaredClass(Element element) {
    String name = ((TypeElement)((DeclaredType)element.asType()).asElement()).getQualifiedName().toString()
    return ClassLoader.getSystemClassLoader().loadClass(name)
}

Recommended Posts

How to get Class from Element in Java
How to get the class name / method name running in Java
How to get the date in java
How to get keycloak credentials in interceptor class
How to use java class
How to use Java HttpClient (Get)
How to disassemble Java class files
How to learn JAVA in 7 days
How to get parameters in Spark
How to decompile java class files
[Java] How to use LinkedHashMap class
How to use class methods [Java]
How to use classes in Java?
How to name variables in Java
[Java] How to use Math class
How to concatenate strings in java
How to store a string from ArrayList to String in Java (Personal)
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
How to get date data in Ruby
[Java] How to use the File class
[Kotlin] 3 ways to get Class from KClass
[Java] How to use the HashMap class
How to do base conversion in Java
[IOS] How to get data from DynamoDB
[Java] How to get the current directory
How to implement coding conventions in Java
How to embed Janus Graph in Java
[Processing × Java] How to use the class
Java: How to send values from Servlet to Servlet
How to use Java Scanner class (Note)
Get history from Zabbix server in Java
[Java] How to use the Calendar class
How to get and add data from Firebase Firestore in Ruby
[Java] How to convert from String to Path type and get the path
How to get the length of an audio file in java
GetInstance () from a @Singleton class in Groovy from Java
[Java] How to use FileReader class and BufferedReader class
Changes from Java 8 to Java 11
Sum from Java_1 to 100
How to get a heapdump from a Docker container
[Java] How to get and output standard input
How to display a web page in Java
Java method call from RPG (method call in own class)
How to convert a solidity contract to a Java contract class
Get unixtime (seconds) from ZonedDateTime in Scala / Java
How to hide null fields in response in Java
How to conditionally add html.erb class in Rails
Library "OSHI" to get system information in Java
From Java to Ruby !!
[Java] How to get random numbers excluding specific numbers
[Java] How to switch from open jdk to oracle jdk
How to get and study java SE8 Gold
[Java] How to get to the front of a specific string using the String class
How to get the value after "_" in Windows batch like Java -version
How to get the setting value (property value) from the database in Spring Framework
How to get SIMD optimization from HotSpot JavaVM
How to get the absolute path of a directory running in Java
[Java] How to substitute Model Mapper in Jackson