/** * @param project * @param itype * @return * @throws JavaModelException */ private static IPath findPathInGeneratedAnnotation(IProject project, IType itype) throws JavaModelException { ICompilationUnit cu = itype.getCompilationUnit(); List<IAnnotationBinding> annotations = resolveAnnotation(cu, Generated.class).getAnnotations(); if ((annotations != null) && (annotations.size() > 0)) { IAnnotationBinding ab = annotations.get(0); IMemberValuePairBinding[] attributes = ab.getAllMemberValuePairs(); for (int i = 0; i < attributes.length; i++) { IMemberValuePairBinding attribut = attributes[i]; if (attribut.getName().equalsIgnoreCase("value")) { Object[] o = (Object[]) attribut.getValue(); if (o != null && o.length > 0 && String.valueOf(o[0]).trim().length() > 0) { try { IPath p = ResourceManager.find(project, String.valueOf(o[0]).trim()); return p; } catch (Exception e) { ResourceManager.logException(e); return null; } } } } } return null; }
/** * @param project * @param itype * @return * @throws JavaModelException */ public static IPath findPathInModelAnnotation(IProject project, IType itype) throws JavaModelException { ICompilationUnit cu = itype.getCompilationUnit(); List<IAnnotationBinding> annotations = resolveAnnotation(cu, Model.class).getAnnotations(); if ((annotations != null) && (annotations.size() > 0)) { IAnnotationBinding ab = annotations.get(0); IMemberValuePairBinding[] attributes = ab.getAllMemberValuePairs(); for (int i = 0; i < attributes.length; i++) { IMemberValuePairBinding attribut = attributes[i]; if (attribut.getName().equalsIgnoreCase("value")) { Object[] o = (Object[]) attribut.getValue(); if (o != null && o.length > 0 && String.valueOf(o[0]).trim().length() > 0) { try { IPath p = ResourceManager.find(project, String.valueOf(o[0]).trim()); return p; } catch (Exception e) { ResourceManager.logException(e); return null; } } } } } return null; }
private static IType getClassesWithAnnotation(ICompilationUnit compilationUnit, Class annotationClass, String attributName, boolean valued) throws JavaModelException { List<IAnnotationBinding> annotations = resolveAnnotation(compilationUnit, annotationClass).getAnnotations(); if ((annotations != null) && (annotations.size() > 0)) { IAnnotationBinding ab = annotations.get(0); IMemberValuePairBinding[] attributes = ab.getAllMemberValuePairs(); for (int i = 0; i < attributes.length; i++) { IMemberValuePairBinding attribut = attributes[i]; if (attribut.getName().equalsIgnoreCase(attributName)) { if (valued) { if (String.valueOf(attribut.getValue()).trim().length() > 0) { return compilationUnit.findPrimaryType(); } } else { if (String.valueOf(attribut.getValue()).trim().length() == 0) { return compilationUnit.findPrimaryType(); } } } } } return null; }
/** * @param index * @return */ public String getValue (int index) { if ((annotations != null) && annotations.size() > 0) { IAnnotationBinding generated = annotations.get(index); IMemberValuePairBinding[] vpb = generated.getAllMemberValuePairs(); for (int i = 0; i < vpb.length; i++) { IMemberValuePairBinding pair = vpb[i]; if ("value".equals(pair.getName())) { Object object = pair.getValue(); String value = null; if (object!=null && object.getClass().isArray()) { Object [] values = (Object []) object; value = (String) values[0]; } else { value = (String) object; } return value; } } } return null; }
public String getValue (String attribute, int index) { if ((annotations != null) && annotations.size() > 0) { IAnnotationBinding generated = annotations.get(index); IMemberValuePairBinding[] vpb = generated.getAllMemberValuePairs(); for (int i = 0; i < vpb.length; i++) { IMemberValuePairBinding pair = vpb[i]; if (attribute.equals(pair.getName())) { Object object = pair.getValue(); String value = null; if (object!=null && object.getClass().isArray()) { Object [] values = (Object []) object; value = (String) values[0]; } else { value = (String) object; } return value; } } } return null; }
public static boolean isCopyOnInheritAnnotation(ITypeBinding annotationType, IJavaProject project, IAnnotationBinding defaultNullness) { if (JavaCore.ENABLED.equals(project.getOption(JavaCore.COMPILER_INHERIT_NULL_ANNOTATIONS, true))) { return false; } if (defaultNullness != null && Bindings.isNonNullAnnotation(annotationType, project)) { IMemberValuePairBinding[] memberValuePairs= defaultNullness.getDeclaredMemberValuePairs(); if (memberValuePairs.length == 1) { Object value= memberValuePairs[0].getValue(); if (value instanceof Boolean) { return Boolean.FALSE.equals(value); // do copy within @NonNullByDefault(false) } } // missing or unrecognized value return false; // nonnull within the scope of @NonNullByDefault: don't copy } return Bindings.isAnyNullAnnotation(annotationType, project); }
/** * @param domain * @param ann finds a domain in annotation binding */ private boolean hasDomain(DomainParams domain, IAnnotationBinding ann) { IMemberValuePairBinding[] pairs = ann.getDeclaredMemberValuePairs(); for (int i = 0; i < pairs.length; i++) { if (pairs[i].getValue() instanceof String) { String dname = (String)pairs[i].getValue(); if (dname.equals(domain.getDomainName())) return true; } if (pairs[i].getValue() instanceof Object[]){ Object[] objs = (Object[])pairs[i].getValue(); for (int j = 0; j < objs.length; j++) { String dName = objs[j].toString(); if (dName.equals(domain.getDomainName())) return true; } } } return false; }
private boolean visitAnnotation(Annotation node) { IAnnotationBinding binding = node.getAnnotationBinding(); boolean needsReflection = false; AbstractTypeDeclaration owningType = TreeUtil.getOwningType(node); if (owningType != null) { needsReflection = TranslationUtil.needsReflection(owningType); } else { needsReflection = TranslationUtil.needsReflection( TreeUtil.getNearestAncestorWithType(PackageDeclaration.class, node)); } if (!BindingUtil.isRuntimeAnnotation(binding) || !needsReflection) { return false; } for (IMemberValuePairBinding memberValuePair : binding.getAllMemberValuePairs()) { if (memberValuePair.isDefault()) { Object value = memberValuePair.getValue(); if (value instanceof IVariableBinding) { addImports(((IVariableBinding) value).getType()); } else if (value instanceof ITypeBinding) { addImports((ITypeBinding) value); } } } return true; }
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; }
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; } } } } } }
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(')'); } }
@Override public boolean matches(IMethodBinding method) throws JavaModelException { for (IAnnotationBinding annotation : method.getAnnotations()) { String annotationName = annotation.getAnnotationType().getQualifiedName(); if (MybatipseConstants.ANNOTATION_RESULTS.equals(annotationName)) { IMemberValuePairBinding[] valuePairs = annotation.getAllMemberValuePairs(); for (IMemberValuePairBinding valuePair : valuePairs) { if ("id".equals(valuePair.getName())) { String resultsId = (String)valuePair.getValue(); return nameMatches(resultsId, matchString, exactMatch); } } } } return false; }
public static boolean isClassOrRuntimeAnnotation(ITypeBinding annotationType) { IAnnotationBinding[] metaAnnotations= annotationType.getAnnotations(); for (IAnnotationBinding metaAnnotation : metaAnnotations) { if ("java.lang.annotation.Retention".equals(metaAnnotation.getAnnotationType().getQualifiedName())) { //$NON-NLS-1$ IMemberValuePairBinding[] mvps= metaAnnotation.getDeclaredMemberValuePairs(); if (mvps.length == 1) { Object value= mvps[0].getValue(); if (value instanceof IVariableBinding) { String name= ((IVariableBinding) value).getName(); if ("RUNTIME".equals(name) || "CLASS".equals(name)) //$NON-NLS-1$ //$NON-NLS-2$ return true; } } } } return false; }
private static void addAnnotation(StringBuffer buf, IJavaElement element, IAnnotationBinding annotation) throws URISyntaxException { String uri= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, annotation.getAnnotationType().getJavaElement()); buf.append('@'); 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]; 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 void printAnnotationParameters(IAnnotationBinding annotation) { IMemberValuePairBinding[] valueBindings = BindingUtil.getSortedMemberValuePairs(annotation); for (int i = 0; i < valueBindings.length; i++) { if (i > 0) { print(' '); } IMemberValuePairBinding valueBinding = valueBindings[i]; print(i == 0 ? "With" : "with"); printf("%s:", NameTable.capitalize( NameTable.getAnnotationPropertyName(valueBinding.getMethodBinding()))); Object value = valueBinding.getValue(); printAnnotationValue(value); } }
public static Object getAnnotationValue(IAnnotationBinding annotation, String name) { for (IMemberValuePairBinding pair : annotation.getAllMemberValuePairs()) { if (name.equals(pair.getName())) { return pair.getValue(); } } return null; }
/** * Returns an alphabetically sorted list of an annotation's member values. * This is necessary since an annotation's values can be specified in any * order, but the annotation's constructor needs to be invoked using its * declaration order. */ public static IMemberValuePairBinding[] getSortedMemberValuePairs( IAnnotationBinding annotation) { IMemberValuePairBinding[] valuePairs = annotation.getAllMemberValuePairs(); Arrays.sort(valuePairs, new Comparator<IMemberValuePairBinding>() { @Override public int compare(IMemberValuePairBinding o1, IMemberValuePairBinding o2) { return o1.getName().compareTo(o2.getName()); } }); return valuePairs; }
private void addAnnotation(StringBuffer buf, IJavaElement element, IAnnotationBinding annotation) throws URISyntaxException { IJavaElement javaElement = annotation.getAnnotationType().getJavaElement(); buf.append('@'); if (javaElement != null) { String uri = JavaElementLinks.createURI(baseHref, 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(baseHref, mvPair.getMethodBinding().getJavaElement()); addLink(buf, memberURI, mvPair.getName()); buf.append('='); addValue(buf, element, mvPair.getValue()); } buf.append(')'); } }
private boolean computeSuppressWarning( IMemberValuePairBinding iMemberValuePairBinding) { if (!"value".equals(iMemberValuePairBinding.getName())) { return false; } Object value = iMemberValuePairBinding.getValue(); return findString(value, "rpc-validation"); }
/** * Checks the {@link @To} {@link @From} and {@link @Trigger} annotations. * Only reports error on {@link @Trigger} if the others are OK. */ private void checkDirections() { ITypeBinding triggerValue = null; Annotation triggerAnnot = null; ITypeBinding fromValue = null; ITypeBinding toValue = null; for (Object mod : transition.modifiers()) { if (mod instanceof Annotation) { Annotation annot = (Annotation) mod; IAnnotationBinding annBinding = annot.resolveAnnotationBinding(); ITypeBinding value = null; for (IMemberValuePairBinding binding : annBinding.getAllMemberValuePairs()) { if (binding.getKey() == null && binding.getValue() instanceof ITypeBinding) { value = (ITypeBinding) binding.getValue(); } } String bindedAnnotationName = annBinding.getAnnotationType().getQualifiedName(); if (From.class.getCanonicalName().equals(bindedAnnotationName)) { fromValue = value; } else if (To.class.getCanonicalName().equals(bindedAnnotationName)) { toValue = value; } else if (Trigger.class.getCanonicalName().equals(bindedAnnotationName)) { triggerValue = value; triggerAnnot = annot; } } } if (fromValue == null) { collector.report(new MissingTransitionSource(collector.getSourceInfo(), transition)); } if (toValue == null) { collector.report(new MissingTransitionTarget(collector.getSourceInfo(), transition)); } if (fromValue != null && toValue != null) { checkTrigger(triggerAnnot, triggerValue, fromValue); } }
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) { AnnotationInstanceAttribute annotationInstanceAttribute = new AnnotationInstanceAttribute(); annotationInstanceAttribute.setValue(annotationInstanceAttributeValueString(memberValueBinding.getValue())); annotationInstance.addAttributes(annotationInstanceAttribute); repository.add(annotationInstanceAttribute); annotationInstanceAttribute.setAnnotationTypeAttribute(ensureAnnotationTypeAttribute(annotationType, memberValueBinding.getName())); } }
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; }
@Override public void add(IMethodBinding method, List<SingleVariableDeclaration> params) { found = true; ITypeBinding[] paramTypes = method.getParameterTypes(); boolean foundParamAnnotation = false; for (int i = 0; i < paramTypes.length; i++) { String paramFqn = paramTypes[i].getQualifiedName(); foundParamAnnotation = false; if (MybatipseConstants.TYPE_ROW_BOUNDS.equals(paramFqn)) continue; IAnnotationBinding[] paramAnnotations = method.getParameterAnnotations(i); for (IAnnotationBinding annotation : paramAnnotations) { if (MybatipseConstants.ANNOTATION_PARAM .equals(annotation.getAnnotationType().getQualifiedName())) { IMemberValuePairBinding[] valuePairs = annotation.getAllMemberValuePairs(); if (valuePairs.length == 1) { IMemberValuePairBinding valuePairBinding = valuePairs[0]; String paramValue = (String)valuePairBinding.getValue(); paramMap.put(paramValue, paramFqn); foundParamAnnotation = true; } } } if (!foundParamAnnotation && isParamAttrGenerated()) { SingleVariableDeclaration param = params.get(i); paramMap.put(param.getName().toString(), paramFqn); } paramMap.put("param" + (i + 1), paramFqn); } if (paramTypes.length == 1 && !foundParamAnnotation) { // Statement has a sole unnamed parameter paramMap.clear(); putSoleParam(paramTypes[0].getQualifiedName()); } }
/** * Helper to extract value of annotation attribute from {@link IMemberValuePairBinding} * * @param annotationContent * {@link IMemberValuePairBinding} with key/value annotation attribute bindings * @param attribute * name of the attribute we want to extract * * @return attribute value */ protected String extractAttributeValueFromAnnotation(IMemberValuePairBinding[] annotationContent, String attribute) { for (IMemberValuePairBinding binding : annotationContent) { if (binding.getName().equals(attribute)) { return binding.getValue().toString(); } } return null; }