下面列出了javax.lang.model.element.AnnotationMirror#getElementValues ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private <T> T getAnnotation(Element element, Class<? extends Annotation> type, String name) {
String canonicalName = type.getCanonicalName();
List<? extends AnnotationMirror> annotationMirrors = elements.getAllAnnotationMirrors(element);
if (annotationMirrors != null && annotationMirrors.size() > 0) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
if (canonicalName.equals(annotationMirror.getAnnotationType().toString())) {
if (annotationMirror.getElementValues() != null) {
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
annotationMirror.getElementValues().entrySet()) {
ExecutableElement annotationName = entry.getKey();
AnnotationValue annotationValue = entry.getValue();
if (annotationName.getSimpleName().toString().equals(name)) {
return (T) annotationValue.getValue();
}
}
}
}
}
}
return null;
}
private boolean isSame( AnnotationMirror first,
AnnotationMirror second , CompilationController controller )
{
Element firstElement = first.getAnnotationType().asElement();
Element secondElement = second.getAnnotationType().asElement();
if ( !firstElement.equals(secondElement)){
return false;
}
Map<? extends ExecutableElement, ? extends AnnotationValue> firstValues = first.getElementValues();
Map<? extends ExecutableElement, ? extends AnnotationValue> secondValues = second.getElementValues();
if ( firstValues.size() != secondValues.size() ){
return false;
}
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry :
firstValues.entrySet())
{
AnnotationValue secondValue = secondValues.get(entry.getKey());
AnnotationValue firstValue = entry.getValue();
if ( !isSame( firstValue, secondValue, controller )){
return false;
}
}
return true;
}
private String getEventType(Element element1) {
List<? extends AnnotationMirror> annotationMirrors = elements.getAllAnnotationMirrors(element1);
if (annotationMirrors != null && annotationMirrors.size() > 0) {
for (AnnotationMirror annotationMirror : annotationMirrors) {
if (ANNOTATION_NAME.equals(annotationMirror.getAnnotationType().toString())) {
System.out.println(TAG + "annotationMirror: " + annotationMirror.getAnnotationType().toString());
if (annotationMirror.getElementValues() != null) {
for (AnnotationValue annotationValue : annotationMirror.getElementValues().values()) {
return annotationValue.getValue().toString();
}
}
}
}
}
return null;
}
private boolean isJaxWsImplementationClass(TypeElement classEl, CompilationController controller) {
TypeElement wsElement = controller.getElements().getTypeElement("javax.jws.WebService"); //NOI18N
if (wsElement != null) {
List<? extends AnnotationMirror> annotations = classEl.getAnnotationMirrors();
for (AnnotationMirror anMirror : annotations) {
if (controller.getTypes().isSameType(wsElement.asType(), anMirror.getAnnotationType())) {
Map<? extends ExecutableElement, ? extends AnnotationValue> expressions = anMirror.getElementValues();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : expressions.entrySet()) {
if (entry.getKey().getSimpleName().contentEquals("wsdlLocation")) { //NOI18N
return false;
}
}
return true;
}
}
}
return false;
}
/**
* Attempts to find the {@code @DefaultProperty} annotation on the type, and returns
* the default property name. Returns null, if @DefaultProperty is not defined
*
* @param te type to inspect
* @return default property name, or {@code null} if default property is not defined for the type.
*/
public static String getDefaultProperty(TypeElement te) {
for (AnnotationMirror an : te.getAnnotationMirrors()) {
if (!((TypeElement)an.getAnnotationType().asElement()).getQualifiedName().contentEquals(DEFAULT_PROPERTY_TYPE_NAME)) {
continue;
}
Map<? extends ExecutableElement, ? extends AnnotationValue> m = an.getElementValues();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> en : m.entrySet()) {
if (en.getKey().getSimpleName().contentEquals(DEFAULT_PROPERTY_VALUE_NAME)) {
Object v = en.getValue().getValue();
return v == null ? null : v.toString();
}
}
}
return null;
}
protected boolean isPassivatingScope( TypeElement scope, WebBeansModel model ) {
AnnotationMirror normalScope = AnnotationUtil.getAnnotationMirror(
scope, model.getCompilationController(), AnnotationUtil.NORMAL_SCOPE_FQN);
if ( normalScope==null){
return false;
}
Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues =
normalScope.getElementValues();
boolean isPassivating = false;
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry:
elementValues.entrySet())
{
ExecutableElement key = entry.getKey();
if ( key.getSimpleName().contentEquals(AnnotationUtil.PASSIVATING)){
isPassivating = Boolean.TRUE.toString().equals(entry.getValue().toString());
}
}
return isPassivating;
}
private String getServiceName(CompilationController controller, TypeElement classElement) {
// check service name
AnnotationMirror anMirror = _RetoucheUtil.getAnnotation(controller,
classElement, "javax.jws.WebService"); // NOI18N
if ( anMirror == null ){
return null;
}
java.util.Map<? extends ExecutableElement, ? extends AnnotationValue> expressions =
anMirror.getElementValues();
for (java.util.Map.Entry<? extends ExecutableElement,
? extends AnnotationValue> entry : expressions.entrySet())
{
if (entry.getKey().getSimpleName().contentEquals("serviceName")) { // NOI18N
return (String) expressions.get(entry.getKey()).getValue();
}
}
return null;
}
/**
* Returns the type converters from the annotations present on the specified element.
* @param element the method or field annotated with {@code @Option} or {@code @Parameters}
* @return the type converters or an empty array if not found
*/
@SuppressWarnings("unchecked")
public static ParameterConsumerMetaData extract(Element element) {
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for (AnnotationMirror mirror : annotationMirrors) {
DeclaredType annotationType = mirror.getAnnotationType();
if (TypeUtil.isOption(annotationType) || TypeUtil.isParameter(annotationType)) {
Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = mirror.getElementValues();
for (ExecutableElement attribute : elementValues.keySet()) {
if ("parameterConsumer".equals(attribute.getSimpleName().toString())) {
AnnotationValue parameterConsumer = elementValues.get(attribute);
TypeMirror typeMirror = (TypeMirror) parameterConsumer.getValue();
if (typeMirror != null) {
return new ParameterConsumerMetaData(typeMirror);
}
}
}
}
}
return null;
}
/**
* Returns the completion candidates from the annotations present on the specified element.
* @param element the method or field annotated with {@code @Option} or {@code @Parameters}
* @return the completion candidates or {@code null} if not found
*/
public static Iterable<String> extract(Element element) {
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors();
for (AnnotationMirror mirror : annotationMirrors) {
DeclaredType annotationType = mirror.getAnnotationType();
if (TypeUtil.isOption(annotationType) || TypeUtil.isParameter(annotationType)) {
Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = mirror.getElementValues();
for (ExecutableElement attribute : elementValues.keySet()) {
if ("completionCandidates".equals(attribute.getSimpleName().toString())) {
AnnotationValue typeMirror = elementValues.get(attribute);
return new CompletionCandidatesMetaData((TypeMirror) typeMirror);
}
}
}
}
return null;
}
private void checkName( TypeElement element, WebBeansModel model,
Result result )
{
AnnotationMirror named = AnnotationUtil.getAnnotationMirror(element,
AnnotationUtil.NAMED , model.getCompilationController());
if ( named == null ){
return;
}
Map<? extends ExecutableElement, ? extends AnnotationValue> members =
named.getElementValues();
for (Entry<? extends ExecutableElement, ? extends AnnotationValue> entry:
members.entrySet())
{
ExecutableElement member = entry.getKey();
if ( member.getSimpleName().contentEquals(AnnotationUtil.VALUE)){
result.addError( element, model,
NbBundle.getMessage(StereotypeAnalyzer.class,
"ERR_NonEmptyNamedStereotype")); // NOI18N
}
}
}
/**
* Return true if the given Element is deprecated for removal.
*
* @param e the Element to check.
* @return true if the given Element is deprecated for removal.
*/
public boolean isDeprecatedForRemoval(Element e) {
List<? extends AnnotationMirror> annotationList = e.getAnnotationMirrors();
JavacTypes jctypes = ((DocEnvImpl) configuration.docEnv).toolEnv.typeutils;
for (AnnotationMirror anno : annotationList) {
if (jctypes.isSameType(anno.getAnnotationType().asElement().asType(), getDeprecatedType())) {
Map<? extends ExecutableElement, ? extends AnnotationValue> pairs = anno.getElementValues();
if (!pairs.isEmpty()) {
for (ExecutableElement element : pairs.keySet()) {
if (element.getSimpleName().contentEquals("forRemoval")) {
return Boolean.parseBoolean((pairs.get(element)).toString());
}
}
}
}
}
return false;
}
boolean refresh(TypeElement typeElement) {
class2 = typeElement.getQualifiedName().toString();
AnnotationModelHelper helper = getHelper();
Map<String, ? extends AnnotationMirror> annByType = helper.getAnnotationsByType(typeElement.getAnnotationMirrors());
AnnotationMirror embeddableAnn = annByType.get("javax.persistence.MappedSuperclass"); // NOI18N
AnnotationMirror entityAcc = annByType.get("javax.persistence.Access"); // NOI18N
if (entityAcc != null) {
entityAcc.getElementValues();
AnnotationParser parser = AnnotationParser.create(helper);
parser.expect("value", new ValueProvider() {
@Override
public Object getValue(AnnotationValue elementValue) {
return elementValue.toString();
}
@Override
public Object getDefaultValue() {
return null;
}
});//NOI18N
ParseResult parseResult = parser.parse(entityAcc);
accessType = parseResult.get("value", String.class);
}
return embeddableAnn != null;
}
private void appendAnnotation(StringBuilder sb, AnnotationMirror annotationDesc, boolean topLevel) {
DeclaredType annotationType = annotationDesc.getAnnotationType();
if (annotationType != null && (!topLevel || isDocumented(annotationType))) {
appendType(sb, annotationType, false, false, true);
Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotationDesc.getElementValues();
if (!values.isEmpty()) {
sb.append('('); //NOI18N
for (Iterator<? extends Map.Entry<? extends ExecutableElement, ? extends AnnotationValue>> it = values.entrySet().iterator(); it.hasNext();) {
Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> value = it.next();
createLink(sb, value.getKey(), value.getKey().getSimpleName());
sb.append('='); //NOI18N
appendAnnotationValue(sb, value.getValue());
if (it.hasNext())
sb.append(","); //NOI18N
}
sb.append(')'); //NOI18N
}
if (topLevel)
sb.append("<br>"); //NOI18N
}
}
private ApiContract<ClassName> getApiClassNameContract(TypeElement apiImplElement) {
String apiClassSymbol = null;
List<? extends AnnotationMirror> annotationMirrors = apiImplElement.getAnnotationMirrors();
for (AnnotationMirror annotationMirror : annotationMirrors) {
Map<? extends ExecutableElement, ? extends AnnotationValue> elementValues = annotationMirror.getElementValues();
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : elementValues.entrySet()) {
apiClassSymbol = entry.getValue().accept(annotationValueVisitor, null);
}
}
ClassName apiName = ClassName.get(elements.getTypeElement(apiClassSymbol));
ClassName apiImplName = ClassName.get(apiImplElement);
return new ApiContract<>(apiName, apiImplName);
}
private String getEndpointInterface(TypeElement classEl,
CompilationController controller)
{
TypeElement wsElement = controller.getElements().
getTypeElement("javax.jws.WebService"); //NOI18N
if ( wsElement == null ){
isIncomplete = true;
return null;
}
List<? extends AnnotationMirror> annotations = classEl.getAnnotationMirrors();
for (AnnotationMirror anMirror : annotations) {
if (controller.getTypes().isSameType(wsElement.asType(),
anMirror.getAnnotationType()))
{
Map<? extends ExecutableElement, ? extends AnnotationValue> expressions =
anMirror.getElementValues();
for (Map.Entry<? extends ExecutableElement,
? extends AnnotationValue> entry : expressions.entrySet())
{
if (entry.getKey().getSimpleName()
.contentEquals("endpointInterface")) // NOI18N
{
String value = (String) expressions.get(entry.getKey())
.getValue();
if (value != null) {
TypeElement seiEl = controller.getElements()
.getTypeElement(value);
if (seiEl != null) {
return seiEl.getQualifiedName().toString();
}
}
}
}
}
}
return null;
}
/**
* Checks that the given annotation is of type
* <code>{@value #ANN_SUITE}.{@value #ANN_SUITE_MEMBERS}</code>
* and contains the given list of classes as (the only) argument,
* in the same order.
*
* @param annMirror annotation to be checked
* @param suiteMembers list of fully qualified class names denoting
* content of the test suite
* @return {@code true} if the annotation meets the described criteria,
* {@code false} otherwise
*/
private boolean checkSuiteMembersAnnotation(AnnotationMirror annMirror,
List<String> suiteMembers,
WorkingCopy workingCopy) {
Map<? extends ExecutableElement,? extends AnnotationValue> annParams
= annMirror.getElementValues();
if (annParams.size() != 1) {
return false;
}
AnnotationValue annValue = annParams.values().iterator().next();
Object value = annValue.getValue();
if (value instanceof java.util.List) {
List<? extends AnnotationValue> items
= (List<? extends AnnotationValue>) value;
if (items.size() != suiteMembers.size()) {
return false;
}
Types types = workingCopy.getTypes();
Iterator<String> suiteMembersIt = suiteMembers.iterator();
for (AnnotationValue item : items) {
Name suiteMemberName = getAnnotationValueClassName(item, types);
if (suiteMemberName == null) {
return false;
}
if (!suiteMemberName.contentEquals(suiteMembersIt.next())) {
return false;
}
}
return true;
}
return false;
}
public List<TypeElement> getTypesFromAnnotation(AnnotationMirror annotationMirror,
String fieldname) {
List<TypeElement> collected = new ArrayList<>();
if (annotationMirror != null) {
Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotationMirror
.getElementValues();
for (ExecutableElement element : values.keySet()) {
if (element.getSimpleName().toString().equals(fieldname)) {
values.get(element).accept(typeCollector, collected);
}
}
}
return collected;
}
public List<String> getStringsFromAnnotation(AnnotationMirror annotationMirror,
String fieldname) {
List<String> collected = new ArrayList<>();
if (annotationMirror != null) {
Map<? extends ExecutableElement, ? extends AnnotationValue> values = annotationMirror
.getElementValues();
for (ExecutableElement element : values.keySet()) {
if (element.getSimpleName().toString().equals(fieldname)) {
values.get(element).accept(stringCollector, collected);
}
}
}
return collected;
}
/**
* Determines if the WSDL operation is the corresponding Java method
*/
private boolean isMethodFor(ExecutableElement method, String operationName){
if(method.getSimpleName().toString().equals(operationName)){
return true;
}
//if method name is not the same as the operation name, look at WebMethod annotation
List<? extends AnnotationMirror> methodAnnotations =
method.getAnnotationMirrors();
for (AnnotationMirror anMirror : methodAnnotations) {
if (JaxWsUtils.hasFqn(anMirror, "javax.jws.WebMethod")) { // NOI18N
Map<? extends ExecutableElement,
? extends AnnotationValue> expressions =
anMirror.getElementValues();
for(Entry<? extends ExecutableElement,
? extends AnnotationValue> entry: expressions.entrySet())
{
if (entry.getKey().getSimpleName().
contentEquals("operationName")) //NOI18N
{
String name = (String)expressions.get(entry.getKey()).
getValue();
if (operationName.equals(name)){
return true;
}
}
}
}
}
return false;
}
private void appendBindingParamters( AnnotationMirror mirror,
StringBuilder builder )
{
Map<? extends ExecutableElement, ? extends AnnotationValue>
elementValues = mirror.getElementValues();
StringBuilder params = new StringBuilder();
for ( Entry<? extends ExecutableElement, ? extends AnnotationValue>
entry : elementValues.entrySet())
{
ExecutableElement key = entry.getKey();
AnnotationValue value = entry.getValue();
List<? extends AnnotationMirror> annotationMirrors =
key.getAnnotationMirrors();
boolean nonBinding = false;
for (AnnotationMirror annotationMirror : annotationMirrors) {
DeclaredType annotationType = annotationMirror.getAnnotationType();
Element element = annotationType.asElement();
if ( ( element instanceof TypeElement ) &&
((TypeElement)element).getQualifiedName().
contentEquals(NON_BINDING_MEMBER_ANNOTATION))
{
nonBinding = true;
break;
}
}
if ( !nonBinding ){
params.append( key.getSimpleName().toString() );
params.append( "=" ); // NOI18N
if ( value.getValue() instanceof String ){
params.append('"');
params.append( value.getValue().toString());
params.append('"');
}
else {
params.append( value.getValue().toString());
}
params.append(", "); // NOI18N
}
}
if ( params.length() >0 ){
builder.append( "(" ); // NOI18N
builder.append( params.substring(0 , params.length() -2 ));
builder.append( ")" ); // NOI18N
}
}