-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
668 additions
and
0 deletions.
There are no files selected for viewing
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
51 changes: 51 additions & 0 deletions
51
value/src/main/java/com/google/auto/value/processor/Java8Support.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,51 @@ | ||
package com.google.auto.value.processor; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import javax.lang.model.element.AnnotationMirror; | ||
import javax.lang.model.type.TypeMirror; | ||
|
||
/** | ||
* this class provides access to java 8 type annotations via reflection (to | ||
* still allow running on older java versions). | ||
* | ||
* @author Till Brychcy | ||
*/ | ||
public class Java8Support { | ||
private static Method determineAnnotationsMirrorsMethod() { | ||
try { | ||
return Class.forName("javax.lang.model.AnnotatedConstruct") | ||
.getMethod("getAnnotationMirrors"); | ||
} catch (Exception e) { | ||
// method and type only exist on java 8 and later | ||
return null; | ||
} | ||
} | ||
|
||
static Method getAnnotationsMirrorsMethod = determineAnnotationsMirrorsMethod(); | ||
|
||
/** | ||
* provides access to | ||
* {@link javax.lang.model.AnnotatedConstruct#getAnnotationMirrors()} via | ||
* reflection | ||
* | ||
* @param typeMirror | ||
* @return if possible, the result of typeMirror.getAnnotationMirrors(), | ||
* otherwise an empty list. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
static List<? extends AnnotationMirror> getAnnotationMirrors(TypeMirror typeMirror) { | ||
try { | ||
if (getAnnotationsMirrorsMethod != null) { | ||
return (List<? extends AnnotationMirror>) getAnnotationsMirrorsMethod | ||
.invoke(typeMirror); | ||
} else { | ||
return Collections.emptyList(); | ||
} | ||
} catch (Exception e) { | ||
throw new RuntimeException("exception during invocation of getAnnotationMirrors", e); | ||
} | ||
} | ||
} |
Oops, something went wrong.