-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
validation - Fixing some ClassNotFoundException when retrieving @Vali…
…datedFor.value() since in most of cases, plain Class instance won't be available yet at annotation processing time Note that current impl has a drawback by relying on com.sun.tools.javac.code hidden package
- Loading branch information
Showing
3 changed files
with
42 additions
and
1 deletion.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
restx-core-annotation-processor/src/main/java/restx/annotations/Annotations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package restx.annotations; | ||
|
||
import com.sun.tools.javac.code.Attribute; | ||
|
||
import javax.lang.model.element.AnnotationMirror; | ||
import javax.lang.model.element.AnnotationValue; | ||
import javax.lang.model.element.ExecutableElement; | ||
import javax.lang.model.element.VariableElement; | ||
import javax.lang.model.type.DeclaredType; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author fcamblor | ||
*/ | ||
public class Annotations { | ||
public static String[] getAnnotationClassValuesAsFQCN(VariableElement p, Class annotationClazz, String methodName) { | ||
List<? extends AnnotationMirror> annotationMirrors = p.getAnnotationMirrors(); | ||
for(AnnotationMirror annotationMirror : annotationMirrors){ | ||
if(annotationMirror.getAnnotationType().toString().equals(annotationClazz.getCanonicalName())){ | ||
for(Map.Entry<? extends ExecutableElement,? extends AnnotationValue> entry : annotationMirror.getElementValues().entrySet()){ | ||
if(entry.getKey().getSimpleName().contentEquals(methodName)){ | ||
Attribute.Array array = (Attribute.Array) entry.getValue(); | ||
|
||
List<String> fqcns = new ArrayList<>(); | ||
for(Attribute attribute : array.getValue()) { | ||
DeclaredType type = (DeclaredType) attribute.getValue(); | ||
fqcns.add(type.toString()); | ||
} | ||
return fqcns.toArray(new String[fqcns.size()]); | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters