下面列出了怎么用org.eclipse.jdt.core.dom.IMemberValuePairBinding的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* @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;
}
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;
}
}
}
}
}
}
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;
}
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());
}
}
}
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(')');
}
}
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;
}
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;
}
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;
}
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;
}
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;
}
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(')');
}
}
private boolean computeSuppressWarning(
IMemberValuePairBinding iMemberValuePairBinding) {
if (!"value".equals(iMemberValuePairBinding.getName())) {
return false;
}
Object value = iMemberValuePairBinding.getValue();
return findString(value, "rpc-validation");
}
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;
}
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;
}