org.eclipse.jdt.core.dom.IMemberValuePairBinding Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.IMemberValuePairBinding. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: JdtBasedTypeFactory.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * @since 2.4
 */
protected JvmAnnotationReference createAnnotationReference(/* @NonNull */ IAnnotationBinding annotation) {
	JvmAnnotationReference annotationReference = TypesFactory.eINSTANCE.createJvmAnnotationReference();
	ITypeBinding annotationType = annotation.getAnnotationType();
	annotationReference.setAnnotation(createAnnotationProxy(annotationType));
	InternalEList<JvmAnnotationValue> values = (InternalEList<JvmAnnotationValue>)annotationReference.getExplicitValues();
	IMemberValuePairBinding[] allMemberValuePairs = annotation.getDeclaredMemberValuePairs();
	for (IMemberValuePairBinding memberValuePair : allMemberValuePairs) {
		IMethodBinding methodBinding = memberValuePair.getMethodBinding();
		if (methodBinding != null) {
			try {
				values.addUnique(createAnnotationValue(annotationType, memberValuePair.getValue(), methodBinding));
			} catch(NullPointerException npe) {
				// memberValuePair#getValue may throw an NPE if the methodBinding has no return type
				if (methodBinding.getReturnType() != null) {
					throw npe;
				} else {
					if (log.isDebugEnabled()) {
						log.debug(npe.getMessage(), npe);
					}
				}
			}
		}
	}
	return annotationReference;
}
 
Example #2
Source File: ValidationSuppressionVisitor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 6 votes vote down vote up
private void processAnnotationBinding(
    IAnnotationBinding resolvedAnnotationBinding) {
  if (resolvedAnnotationBinding != null) {
    ITypeBinding annotationType = resolvedAnnotationBinding.getAnnotationType();
    if (annotationType != null) {
      if (annotationType.getQualifiedName().equals(
          SuppressWarnings.class.getName())) {
        IMemberValuePairBinding[] allMemberValuePairs = resolvedAnnotationBinding.getAllMemberValuePairs();
        for (IMemberValuePairBinding iMemberValuePairBinding : allMemberValuePairs) {
          if (computeSuppressWarning(iMemberValuePairBinding)) {
            suppressValidation = true;
            break;
          }
        }
      }
    }
  }
}
 
Example #3
Source File: SharedUtils.java    From txtUML with Eclipse Public License 1.0 6 votes vote down vote up
public static ITypeBinding obtainTypeLiteralAnnotation(BodyDeclaration declaration, Class<?> annotationClass, String name) {
	//TODO NA: need a better solution instead of string literals..
	IAnnotationBinding annot = obtainAnnotationBinding(declaration, annotationClass);
	if (annot != null) {
		for(IMemberValuePairBinding annotValue : annot.getAllMemberValuePairs()) {
			if(annotValue.getName().equals(name)) {
				Object value = annotValue.getValue();
				if(value instanceof ITypeBinding) {
					return (ITypeBinding) value;
				}
				
			}
			
		}
	}		
	return null;
}
 
Example #4
Source File: InJavaImporter.java    From jdt2famix with Eclipse Public License 1.0 6 votes vote down vote up
private void createAnnotationInstanceFromAnnotationInstanceBinding(IAnnotationBinding annotationInstanceBinding,
		ITypeBinding annotationTypeBinding, AnnotationInstance annotationInstance) {
	AnnotationType annotationType = (AnnotationType) ensureTypeFromTypeBinding(annotationTypeBinding);
	annotationInstance.setAnnotationType(annotationType);
	IMemberValuePairBinding[] allMemberValuePairs = annotationInstanceBinding.getAllMemberValuePairs();
	for (IMemberValuePairBinding memberValueBinding : allMemberValuePairs) {
		try {
			Object value = memberValueBinding.getValue();
			AnnotationInstanceAttribute annotationInstanceAttribute = new AnnotationInstanceAttribute();

			annotationInstanceAttribute.setValue(annotationInstanceAttributeValueString(value));
			annotationInstance.addAttributes(annotationInstanceAttribute);
			repository.add(annotationInstanceAttribute);

			annotationInstanceAttribute.setAnnotationTypeAttribute(
					ensureAnnotationTypeAttribute(annotationType, memberValueBinding.getName()));
		} catch (NullPointerException npe) {
			logger.error(
					"Null pointer exception in jdt core when getting the value of annotation instance attribute "
							+ memberValueBinding.getKey());
		}
	}
}
 
Example #5
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static void addAnnotation(StringBuffer buf, IJavaElement element, IAnnotationBinding annotation) throws URISyntaxException {
	IJavaElement javaElement= annotation.getAnnotationType().getJavaElement();
	buf.append('@');
	if (javaElement != null) {
		String uri= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, javaElement);
		addLink(buf, uri, annotation.getName());
	} else {
		buf.append(annotation.getName());
	}
	
	IMemberValuePairBinding[] mvPairs= annotation.getDeclaredMemberValuePairs();
	if (mvPairs.length > 0) {
		buf.append('(');
		for (int j= 0; j < mvPairs.length; j++) {
			if (j > 0) {
				buf.append(JavaElementLabels.COMMA_STRING);
			}
			IMemberValuePairBinding mvPair= mvPairs[j];
			String memberURI= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, mvPair.getMethodBinding().getJavaElement());
			addLink(buf, memberURI, mvPair.getName());
			buf.append('=');
			addValue(buf, element, mvPair.getValue());
		}
		buf.append(')');
	}
}
 
Example #6
Source File: StructCache.java    From junion with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static IMemberValuePairBinding find(IMemberValuePairBinding[] anno, String propertyName) {
	if(anno == null) return null;
	for(int i = 0; i < anno.length; i++)
		if(anno[i].getName().equals(propertyName))
			return anno[i];
	return null;
}
 
Example #7
Source File: StructCache.java    From junion with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static Object getValue(IMemberValuePairBinding[] anno, String propertyName) {
	if(anno == null) return null;
	for(int i = 0; i < anno.length; i++)
		if(anno[i].getName().equals(propertyName))
			return anno[i].getValue();
	return null;
}
 
Example #8
Source File: JdtAnnotationUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
public static String getAnnotationParameterString(
    IAnnotationBinding annotationBinding, String paramName) {
  if (annotationBinding == null) {
    return null;
  }
  for (IMemberValuePairBinding member : annotationBinding.getDeclaredMemberValuePairs()) {
    if (paramName.equals(member.getName())) {
      return (String) member.getValue();
    }
  }
  return null;
}
 
Example #9
Source File: JdtAnnotationUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
public static Object[] getAnnotationParameterArray(
    IAnnotationBinding annotationBinding, String paramName) {
  if (annotationBinding == null) {
    return null;
  }
  for (IMemberValuePairBinding member : annotationBinding.getDeclaredMemberValuePairs()) {
    if (paramName.equals(member.getName())) {
      if (member.getValue() instanceof Object[]) {
        return (Object[]) member.getValue();
      }
    }
  }
  return null;
}
 
Example #10
Source File: JdtAnnotationUtils.java    From j2cl with Apache License 2.0 5 votes vote down vote up
public static boolean getAnnotationParameterBoolean(
    IAnnotationBinding annotationBinding, String paramName, boolean defaultValue) {
  if (annotationBinding == null) {
    return defaultValue;
  }
  for (IMemberValuePairBinding member : annotationBinding.getDeclaredMemberValuePairs()) {
    if (paramName.equals(member.getName())) {
      return (Boolean) member.getValue();
    }
  }
  return defaultValue;
}
 
Example #11
Source File: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void addAnnotation(StringBuilder buf, IAnnotationBinding annotation, boolean addLinks) throws URISyntaxException {
	IJavaElement javaElement = annotation.getAnnotationType().getJavaElement();
	buf.append('@');
	if (javaElement == null || !addLinks) {
		buf.append(annotation.getName());
	} else {
		String uri = JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, javaElement);
		addLink(buf, uri, annotation.getName());
	}

	IMemberValuePairBinding[] mvPairs = annotation.getDeclaredMemberValuePairs();
	if (mvPairs.length > 0) {
		buf.append('(');
		for (int j = 0; j < mvPairs.length; j++) {
			if (j > 0) {
				buf.append(JavaElementLabels.COMMA_STRING);
			}
			IMemberValuePairBinding mvPair = mvPairs[j];
			if (addLinks) {
				String memberURI = JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, mvPair.getMethodBinding().getJavaElement());
				addLink(buf, memberURI, mvPair.getName());
			} else {
				buf.append(mvPair.getName());
			}
			buf.append('=');
			addValue(buf, mvPair.getValue(), addLinks);
		}
		buf.append(')');
	}
}
 
Example #12
Source File: ValidationSuppressionVisitor.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
private boolean computeSuppressWarning(
    IMemberValuePairBinding iMemberValuePairBinding) {
  if (!"value".equals(iMemberValuePairBinding.getName())) {
    return false;
  }

  Object value = iMemberValuePairBinding.getValue();
  return findString(value, "rpc-validation");
}
 
Example #13
Source File: MultiplicityProvider.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
private static Integer getExplicitMultiplicity(ITypeBinding typeDeclaration, String annotationName) {
	for (IAnnotationBinding annot : typeDeclaration.getAnnotations()) {
		if (annot.getName().equals(annotationName)) {
			for (IMemberValuePairBinding pair : annot.getAllMemberValuePairs()) {
				if (pair.getName().equals("value")) {
					return (Integer) pair.getValue();
				}
			}
		}
	}
	return null;
}
 
Example #14
Source File: NullAnnotationsRewriteOperations.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
boolean hasNonNullDefault(IBinding enclosingElement) {
	if (!fRemoveIfNonNullByDefault) return false;
	IAnnotationBinding[] annotations = enclosingElement.getAnnotations();
	for (int i= 0; i < annotations.length; i++) {
		IAnnotationBinding annot = annotations[i];
		if (annot.getAnnotationType().getQualifiedName().equals(fNonNullByDefaultName)) {
			IMemberValuePairBinding[] pairs= annot.getDeclaredMemberValuePairs();
			if (pairs.length > 0) {
				// is default cancelled by "false" or "value=false" ?
				for (int j= 0; j < pairs.length; j++)
					if (pairs[j].getKey() == null || pairs[j].getKey().equals("value")) //$NON-NLS-1$
						return (pairs[j].getValue() != Boolean.FALSE);
			}
			return true;
		}
	}
	if (enclosingElement instanceof IMethodBinding) {
		return hasNonNullDefault(((IMethodBinding)enclosingElement).getDeclaringClass());
	} else if (enclosingElement instanceof ITypeBinding) {
		ITypeBinding typeBinding= (ITypeBinding)enclosingElement;
		if (typeBinding.isLocal())
			return hasNonNullDefault(typeBinding.getDeclaringMethod());
		else if (typeBinding.isMember())
			return hasNonNullDefault(typeBinding.getDeclaringClass());
		else
			return hasNonNullDefault(typeBinding.getPackage());
	}
	return false;
}