boolean found = false; for (IAnnotation i : in) { String name = i.getElementName(); int li = name.lastIndexOf('.'); if (li > -1) name = name.substring(li + 1);
private void appendAnnotation(IAnnotation annotation) throws JavaModelException { String name= annotation.getElementName(); if (!fStubInvisible && name.startsWith("sun.") || name.startsWith("jdk.internal.")) //$NON-NLS-1$ //$NON-NLS-2$ return; // skip Sun-internal annotations fBuffer.append('@'); fBuffer.append(name.replace('$', '.')); fBuffer.append('('); IMemberValuePair[] memberValuePairs= annotation.getMemberValuePairs(); for (IMemberValuePair pair : memberValuePairs) { fBuffer.append(pair.getMemberName()); fBuffer.append('='); appendAnnotationValue(pair.getValue(), pair.getValueKind()); fBuffer.append(','); } if (memberValuePairs.length > 0) fBuffer.deleteCharAt(fBuffer.length() - 1); fBuffer.append(')').append('\n'); }
public static Optional<IAnnotation> getAnnotation(IMethod method, String methodAnnotation) { try { final Optional<IAnnotation> matched = Arrays.stream(method.getAnnotations()) .filter(annotation -> methodAnnotation.endsWith(annotation.getElementName())).findAny(); if (!matched.isPresent()) { return Optional.empty(); } final IAnnotation annotation = matched.get(); if (!annotation.exists()) { return Optional.empty(); } final String name = annotation.getElementName(); final String[][] fullNameArr = method.getDeclaringType().resolveType(name); if (fullNameArr == null) { final ICompilationUnit cu = method.getCompilationUnit(); if (cu != null && cu.getImport(methodAnnotation).exists()) { return Optional.of(annotation); } else { return Optional.empty(); } } final String fullName = Arrays.stream(fullNameArr[0]).collect(Collectors.joining(".")); return fullName.equals(methodAnnotation) ? Optional.of(annotation) : Optional.empty(); } catch (final JavaModelException e) { return Optional.empty(); } }
private static String getAnnotationSourceFixed(IMember member, IAnnotation annotation, String startSimple) { try { ISourceRange annotSourceRange = annotation.getSourceRange(); ISourceRange ownerSourceRange = member.getSourceRange(); if (SourceRange.isAvailable(ownerSourceRange) && SourceRange.isAvailable(annotSourceRange) && ownerSourceRange.getOffset() > annotSourceRange.getOffset()) { String icuSource = member.getCompilationUnit().getSource(); if (icuSource != null && icuSource.length() >= ownerSourceRange.getOffset()) { String diff = icuSource.substring(annotSourceRange.getOffset(), ownerSourceRange.getOffset()); int offset = diff.lastIndexOf(startSimple); if (offset >= 0) { offset += annotSourceRange.getOffset(); int end = offset + annotSourceRange.getLength(); if (icuSource.length() >= end) { return icuSource.substring(offset, end); } } } } } catch (JavaModelException e) { SdkLog.warning("Unable to find source for annotation '{}' in '{}'.", annotation.getElementName(), member.getElementName(), e); } return null; }
@Override protected Position getSourceRange(IType type, IDocument doc, IReference reference) throws CoreException, BadLocationException { IApiMember member = reference.getMember(); if (member != null) { IAnnotatable annotable = findAnnotableElement(type, member, reference, doc); if (annotable != null) { IAnnotation[] annotations = annotable.getAnnotations(); String typename = Signatures.getSimpleTypeName(reference.getResolvedReference().getName()); for (IAnnotation annotation : annotations) { if (annotation.getElementName().equals(typename)) { ISourceRange range = annotation.getNameRange(); if (range != null) { // select the '@' as well - augment offset and // length accordingly return new Position(range.getOffset() - 1, range.getLength() + 1); } else { break; } } } } } return defaultSourcePosition(type, reference); }
private static boolean methodHasDependencies(IMethod method) throws JavaModelException { IAnnotation annotation = method.getAnnotation("Test"); return annotation != null && (contains(annotation.getMemberValuePairs(), "dependsOnGroups") || contains(annotation.getMemberValuePairs(), "dependsOnMethods")); }
/** * @param element the element * @return <code>true</code> if the element is a test class annotated with * <code>@RunWith(JUnitPlatform.class)</code> */ public static boolean isRunWithJUnitPlatform(IJavaElement element) { if (element instanceof ICompilationUnit) { element= ((ICompilationUnit) element).findPrimaryType(); } if (element instanceof IType) { IType type= (IType) element; try { IAnnotation runWithAnnotation= type.getAnnotation("RunWith"); //$NON-NLS-1$ if (!runWithAnnotation.exists()) { runWithAnnotation= type.getAnnotation("org.junit.runner.RunWith"); //$NON-NLS-1$ } if (runWithAnnotation.exists()) { IMemberValuePair[] memberValuePairs= runWithAnnotation.getMemberValuePairs(); for (IMemberValuePair memberValuePair : memberValuePairs) { if (memberValuePair.getMemberName().equals("value") && memberValuePair.getValue().equals("JUnitPlatform")) { //$NON-NLS-1$ //$NON-NLS-2$ return true; } } } } catch (JavaModelException e) { // ignore } } return false; }
int recordedAnnotations = 0; for (int i = 0; i < length; i++) { ISourceRange positions = annotations[i].getSourceRange(); int start = positions.getOffset(); int end = start + positions.getLength();
for (final IAnnotation duplicate : matchesById) { final IAnnotation other = getVisibleDuplicate(duplicate, matchesById); final IType parent = (IType) duplicate.getAncestor(IJavaElement.TYPE); if (S2eUtils.exists(parent) && S2eUtils.exists(other)) { @SuppressWarnings("squid:S2259") final IType otherParent = (IType) other.getAncestor(IJavaElement.TYPE); final ISourceRange sourceRange = duplicate.getSourceRange(); if (S2eUtils.exists(otherParent) && SourceRange.isAvailable(sourceRange)) { final IMarker marker = duplicate.getResource().createMarker(CLASS_ID_DUPLICATE_MARKER_ID); marker.setAttribute(IMarker.MESSAGE, "Duplicate @ClassId. Value '" + matches.getKey() + "' of type '" + parent.getFullyQualifiedName() + "' is the same as of type '" + otherParent.getFullyQualifiedName() + "'.");
public static boolean isPolymorphicSignature(IMethod method) { return method.getAnnotation("java.lang.invoke.MethodHandle$PolymorphicSignature").exists(); //$NON-NLS-1$ }
private void appendAnnotation(IAnnotation annotation) throws JavaModelException { String name= annotation.getElementName(); if (!fStubInvisible && name.startsWith("sun.")) //$NON-NLS-1$ return; // skip Sun-internal annotations fBuffer.append('@'); fBuffer.append(name); fBuffer.append('('); IMemberValuePair[] memberValuePairs= annotation.getMemberValuePairs(); for (IMemberValuePair pair : memberValuePairs) { fBuffer.append(pair.getMemberName()); fBuffer.append('='); appendAnnotationValue(pair.getValue(), pair.getValueKind()); fBuffer.append(','); } if (memberValuePairs.length > 0) fBuffer.deleteCharAt(fBuffer.length() - 1); fBuffer.append(')').append('\n'); }
private String extractDefaultValue(IMethod operation, IAnnotation annot) throws JavaModelException, IllegalArgumentException { IAnnotation annotation = annot; final Object value = annotation.getMemberValuePairs()[0].getValue(); final String fieldId = (value == null) ? null : value.toString(); if (!Strings.isNullOrEmpty(fieldId)) { final String fieldName = Utils.createNameForHiddenDefaultValueAttribute(fieldId); final IField field = operation.getDeclaringType().getField(fieldName); if (field != null) { annotation = getAnnotation(field, SarlSourceCode.class.getName()); if (annotation != null) { return annotation.getMemberValuePairs()[0].getValue().toString(); } } } return null; }
int recordedAnnotations = 0; for (int i = 0; i < length; i++) { ISourceRange positions = annotations[i].getSourceRange(); int start = positions.getOffset(); int end = start + positions.getLength();
public static boolean isPolymorphicSignature(IMethod method) { return method.getAnnotation("java.lang.invoke.MethodHandle$PolymorphicSignature").exists(); //$NON-NLS-1$ }
IAnnotation ruleAnnotation = field.getAnnotation("Rule"); IType declaringType = field.getDeclaringType(); String elementName = ruleAnnotation.getElementName(); String[][] fullyQualifiedName = declaringType.resolveType(elementName);
private static boolean isSilencedGeneratedAnnotation(IAnnotation annotation) throws JavaModelException { if ("javax.annotation.Generated".equals(annotation.getElementName()) || "javax.annotation.processing.Generated".equals(annotation.getElementName())) { IMemberValuePair[] memberValuePairs = annotation.getMemberValuePairs(); for (IMemberValuePair m : memberValuePairs) { if ("value".equals(m.getMemberName()) && IMemberValuePair.K_STRING == m.getValueKind()) { if (m.getValue() instanceof String) { return SILENCED_CODEGENS.contains(m.getValue()); } else if (m.getValue() instanceof Object[]) { for (Object val : (Object[])m.getValue()) { if(SILENCED_CODEGENS.contains(val)) { return true; } } } } } } return false; }
/** * Gets teh value of the given annotation attribute as {@link String}. * * @param annotation * The {@link IAnnotation} for which the attribute should be converted. * @param name * The name of attribute. * @return A {@link String} with the value of the given attribute or <code>null</code> if the value is <code>null</code> * or could not be found. * @throws JavaModelException */ public static String getAnnotationValueString(IAnnotation annotation, String name) throws JavaModelException { if (!exists(annotation)) { return null; } IMemberValuePair[] memberValues = annotation.getMemberValuePairs(); for (IMemberValuePair p : memberValues) { if (Objects.equals(name, p.getMemberName())) { Object val = p.getValue(); if (val != null) { return val.toString(); } } } return null; }
int recordedAnnotations = 0; for (int i = 0; i < length; i++) { ISourceRange positions = annotations[i].getSourceRange(); int start = positions.getOffset(); int end = start + positions.getLength();
public static boolean isTestMethod(IMethod method, boolean onlyPublicMethod, String[] annotations) { if (isMethod(method, onlyPublicMethod)) { for (String annotation : annotations) { if (method.getAnnotation(annotation).exists()) { return true; } } } return false; }
@Override public IAnnotation getAnnotation(String annotationName) { for (int i = 0, length = this.annotations.length; i < length; i++) { IAnnotation annotation = this.annotations[i]; if (annotation.getElementName().equals(annotationName)) return annotation; } return super.getAnnotation(annotationName); }