public static boolean isHiddenGeneratedElement(IJavaElement element) { // generated elements are tagged with javax.annotation.Generated and // they need to be filtered out if (element instanceof IAnnotatable) { try { IAnnotation[] annotations = ((IAnnotatable) element).getAnnotations(); if (annotations.length != 0) { for (IAnnotation annotation : annotations) { if (isSilencedGeneratedAnnotation(annotation)) { return true; } } } } catch (JavaModelException e) { // ignore } } return false; }
public static boolean isHiddenGeneratedElement(IJavaElement element) { // generated elements are tagged with javax.annotation.Generated and // they need to be filtered out if (element instanceof IAnnotatable) { try { IAnnotation[] annotations = ((IAnnotatable) element).getAnnotations(); if (annotations.length != 0) { for (IAnnotation annotation : annotations) { if (isSilencedGeneratedAnnotation(annotation)) { return true; } } } } catch (JavaModelException e) { //ignore } } return false; }
protected void appendFlags(final IMember member) throws JavaModelException { if (member instanceof IAnnotatable) for (IAnnotation annotation : ((IAnnotatable) member).getAnnotations()) { appendAnnotation(annotation); } int flags = member.getFlags(); final int kind = member.getElementType(); if (kind == IJavaElement.TYPE) { flags &= ~Flags.AccSuper; final IType type = (IType) member; if (!type.isMember()) flags &= ~Flags.AccPrivate; if (Flags.isEnum(flags)) flags &= ~Flags.AccAbstract; } if (Flags.isEnum(flags)) flags &= ~Flags.AccFinal; if (kind == IJavaElement.METHOD) { flags &= ~Flags.AccVarargs; flags &= ~Flags.AccBridge; } if (flags != 0) fBuffer.append(Flags.toString(flags)); }
private boolean shouldWriteMarkers(IJavaElement currentElement) { IJavaElement parent = currentElement; while (parent != null) { if (parent instanceof IAnnotatable) { IAnnotatable p = (IAnnotatable) parent; try { for (IAnnotation a : p.getAnnotations()) { if (a.getElementName().equalsIgnoreCase("EvoIgnore")) { return false; } } } catch (JavaModelException e) { e.printStackTrace(); } } parent = parent.getParent(); } return true; }
protected void appendFlags(final IMember member) throws JavaModelException { if (member instanceof IAnnotatable) for (IAnnotation annotation : ((IAnnotatable) member).getAnnotations()) { appendAnnotation(annotation); } int flags= member.getFlags(); final int kind= member.getElementType(); if (kind == IJavaElement.TYPE) { flags&= ~Flags.AccSuper; final IType type= (IType) member; if (!type.isMember()) flags&= ~Flags.AccPrivate; if (Flags.isEnum(flags)) flags&= ~Flags.AccAbstract; } if (Flags.isEnum(flags)) flags&= ~Flags.AccFinal; if (kind == IJavaElement.METHOD) { flags&= ~Flags.AccVarargs; flags&= ~Flags.AccBridge; } if (flags != 0) fBuffer.append(Flags.toString(flags)); }
public List<String> readMarkerAnnotation(IAnnotatable annotatable) throws JavaModelException { List<String> markers = new ArrayList<String>(); IAnnotation annotation = TapestryUtils.findAnnotation(annotatable.getAnnotations(), TapestryUtils.ORG_APACHE_TAPESTRY5_IOC_ANNOTATIONS_MARKER); if (annotation != null) { String[] typeLiterals = EclipseUtils.readValuesFromAnnotation(getEclipseProject(), annotation, "value"); for (String typeLiteral : typeLiterals) { String typeName = EclipseUtils.resolveTypeName(moduleClass, typeLiteral); markers.add(typeName); } } return markers; }
private List<String> extractMarkers(IAnnotatable annotatable, Set<String> skipAnnotations) throws JavaModelException { List<String> markers = new ArrayList<String>(); for (IAnnotation annotation : annotatable.getAnnotations()) { String typeName = EclipseUtils.resolveTypeName( tapestryModule.getModuleClass(), annotation.getElementName()); if (skipAnnotations.contains(typeName)) { continue; } markers.add(typeName); } return markers; }
/** * Obtient une annotation d'un nom donné sur un objet donné. * * @param annotable Objet à inspecter. * @param name Nom de l'annotation. * @return Annotation, <code>null</code> sinon. */ public static IAnnotation getAnnotation(IAnnotatable annotable, String name) { try { for (IAnnotation annotation : annotable.getAnnotations()) { String annotationName = StringUtils.getLastNameFragment(annotation.getElementName()); if (name.equals(annotationName)) { return annotation; } } } catch (JavaModelException e) { ErrorUtils.handle(e); } return null; }
private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException { IAnnotation[] annotations = element.getAnnotations(); int length = annotations.length; Annotation[] astAnnotations = new Annotation[length]; if (length > 0) { char[] cuSource = getSource(); int recordedAnnotations = 0; for (int i = 0; i < length; i++) { ISourceRange positions = annotations[i].getSourceRange(); int start = positions.getOffset(); int end = start + positions.getLength(); char[] annotationSource = CharOperation.subarray(cuSource, start, end); if (annotationSource != null) { Expression expression = parseMemberValue(annotationSource); /* * expression can be null or not an annotation if the source has changed between * the moment where the annotation source positions have been retrieved and the moment were * this parsing occurred. * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916 */ if (expression instanceof Annotation) { astAnnotations[recordedAnnotations++] = (Annotation) expression; } } } if (length != recordedAnnotations) { // resize to remove null annotations System.arraycopy( astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations); } } return astAnnotations; }
/** * Gets the corresponding IAnnotation. * * @param qualifiedAnnotationName the fully qualified annotation type name * @param annotatable the IAnnotatable java element which contains the * annotation * @param contextType the type which is used to lookup imports * @return the IAnnotation or null * @throws JavaModelException */ public static Object getAnnotation(String qualifiedAnnotationName, Object annotatable, IType contextType) throws JavaModelException { for (IAnnotation annotation : ((IAnnotatable) annotatable).getAnnotations()) { if (qualifiedAnnotationName.equals(resolveTypeName(contextType, annotation.getElementName()))) { return annotation; } } return null; }
public void consumeAnnotation() { if (!(this.element instanceof IAnnotatable)) return; int size = this.types.size(); if (size == 0) return; IJavaElement annotationType = ((JavaElementFinder) this.types.get(size-1)).element; this.element = ((IAnnotatable) this.element).getAnnotation(annotationType.getElementName()); }
private Annotation[] convertAnnotations(IAnnotatable element) throws JavaModelException { IAnnotation[] annotations = element.getAnnotations(); int length = annotations.length; Annotation[] astAnnotations = new Annotation[length]; if (length > 0) { char[] cuSource = getSource(); int recordedAnnotations = 0; for (int i = 0; i < length; i++) { ISourceRange positions = annotations[i].getSourceRange(); int start = positions.getOffset(); int end = start + positions.getLength(); char[] annotationSource = CharOperation.subarray(cuSource, start, end); if (annotationSource != null) { Expression expression = parseMemberValue(annotationSource); /* * expression can be null or not an annotation if the source has changed between * the moment where the annotation source positions have been retrieved and the moment were * this parsing occurred. * See https://bugs.eclipse.org/bugs/show_bug.cgi?id=90916 */ if (expression instanceof Annotation) { astAnnotations[recordedAnnotations++] = (Annotation) expression; } } } if (length != recordedAnnotations) { // resize to remove null annotations System.arraycopy(astAnnotations, 0, (astAnnotations = new Annotation[recordedAnnotations]), 0, recordedAnnotations); } } return astAnnotations; }
public static IAnnotation getAnnotationAt(IAnnotatable annotatable, int offset) throws JavaModelException { IAnnotation[] annotations = annotatable.getAnnotations(); for (IAnnotation annotation : annotations) { ISourceRange sourceRange = annotation.getSourceRange(); if (isInRange(sourceRange, offset)) { return annotation; } } return null; }
private static String getAnnotations(IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) throws URISyntaxException, JavaModelException { if (!(element instanceof IAnnotatable)) return null; if (((IAnnotatable)element).getAnnotations().length == 0) return null; IBinding binding; ASTNode node= getHoveredASTNode(editorInputElement, hoverRegion); if (node == null) { ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); p.setProject(element.getJavaProject()); try { binding= p.createBindings(new IJavaElement[] { element }, null)[0]; } catch (OperationCanceledException e) { return null; } } else { binding= resolveBinding(node); } if (binding == null) return null; IAnnotationBinding[] annotations= binding.getAnnotations(); if (annotations.length == 0) return null; StringBuffer buf= new StringBuffer(); for (int i= 0; i < annotations.length; i++) { //TODO: skip annotations that don't have an @Documented annotation? addAnnotation(buf, element, annotations[i]); buf.append("<br>"); //$NON-NLS-1$ } return buf.toString(); }
private RefactoringStatus checkAnnotations(IAnnotatable source, IType sourceType, IAnnotatable target, IType targetType) throws JavaModelException { // a set of annotations from the source method. Set<IAnnotation> sourceAnnotationSet = new HashSet<>(Arrays.asList(source.getAnnotations())); // remove any annotations to not consider. removeSpecialAnnotations(sourceAnnotationSet, sourceType); // a set of source method annotation names. Set<String> sourceMethodAnnotationElementNames = getAnnotationElementNames(sourceAnnotationSet); // a set of annotations from the target method. Set<IAnnotation> targetAnnotationSet = new HashSet<>(Arrays.asList(target.getAnnotations())); // remove any annotations to not consider. removeSpecialAnnotations(targetAnnotationSet, targetType); // a set of target method annotation names. Set<String> targetAnnotationElementNames = getAnnotationElementNames(targetAnnotationSet); // if the source method annotation names don't match the target method // annotation names. if (!sourceMethodAnnotationElementNames.equals(targetAnnotationElementNames)) return RefactoringStatus.createErrorStatus(PreconditionFailure.AnnotationNameMismatch.getMessage(), new RefactoringStatusContext() { @Override public Object getCorrespondingElement() { return source; } }); else // otherwise, we have the same annotations names. Check the values. for (IAnnotation sourceAnnotation : sourceAnnotationSet) { IMemberValuePair[] sourcePairs = sourceAnnotation.getMemberValuePairs(); IAnnotation targetAnnotation = target.getAnnotation(sourceAnnotation.getElementName()); IMemberValuePair[] targetPairs = targetAnnotation.getMemberValuePairs(); if (sourcePairs.length != targetPairs.length) { // TODO: Can perhaps analyze this situation further by // looking up default field values. logWarning( "There may be differences in the length of the value vectors as some annotations may use default field values."); return new RefactoringStatus(); } Arrays.parallelSort(sourcePairs, Comparator.comparing(IMemberValuePair::getMemberName)); Arrays.parallelSort(targetPairs, Comparator.comparing(IMemberValuePair::getMemberName)); for (int i = 0; i < sourcePairs.length; i++) if (!sourcePairs[i].getMemberName().equals(targetPairs[i].getMemberName()) || sourcePairs[i].getValueKind() != targetPairs[i].getValueKind() || !(sourcePairs[i].getValue().equals(targetPairs[i].getValue()))) return RefactoringStatus.createErrorStatus( formatMessage(PreconditionFailure.AnnotationValueMismatch.getMessage(), sourceAnnotation, targetAnnotation), JavaStatusContext.create(findEnclosingMember(sourceAnnotation))); } return new RefactoringStatus(); // OK. }
public final IJavaElement transplantHandle(IJavaElement element) { IJavaElement parent = element.getParent(); if (parent != null) parent = transplantHandle(parent); // recursive switch (element.getElementType()) { case IJavaElement.JAVA_MODEL: return transplantHandle((IJavaModel) element); case IJavaElement.JAVA_PROJECT: return transplantHandle((IJavaProject) element); case IJavaElement.PACKAGE_FRAGMENT_ROOT: return transplantHandle((IJavaProject) parent, (IPackageFragmentRoot) element); case IJavaElement.PACKAGE_FRAGMENT: return transplantHandle((IPackageFragmentRoot) parent, (IPackageFragment) element); case IJavaElement.COMPILATION_UNIT: return transplantHandle((IPackageFragment) parent, (ICompilationUnit) element); case IJavaElement.CLASS_FILE: return transplantHandle((IPackageFragment) parent, (IClassFile) element); case IJavaElement.TYPE: return transplantHandle(parent, (IType) element); case IJavaElement.FIELD: return transplantHandle((IType) parent, (IField) element); case IJavaElement.METHOD: return transplantHandle((IType) parent, (IMethod) element); case IJavaElement.INITIALIZER: return transplantHandle((IType) parent, (IInitializer) element); case IJavaElement.PACKAGE_DECLARATION: return transplantHandle((ICompilationUnit) parent, (IPackageDeclaration) element); case IJavaElement.IMPORT_CONTAINER: return transplantHandle((ICompilationUnit) parent, (IImportContainer) element); case IJavaElement.IMPORT_DECLARATION: return transplantHandle((IImportContainer) parent, (IImportDeclaration) element); case IJavaElement.LOCAL_VARIABLE: return transplantHandle((ILocalVariable) element); case IJavaElement.TYPE_PARAMETER: return transplantHandle((IMember) parent, (ITypeParameter) element); case IJavaElement.ANNOTATION: return transplantHandle((IAnnotatable) parent, (IAnnotation) element); default: throw new IllegalArgumentException(element.toString()); } }
protected IAnnotation transplantHandle(IAnnotatable parent, IAnnotation element) { return parent.getAnnotation(element.getElementName()); }
private String getAnnotations( IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) throws URISyntaxException, JavaModelException { if (!(element instanceof IPackageFragment)) { if (!(element instanceof IAnnotatable)) return null; if (((IAnnotatable) element).getAnnotations().length == 0) return null; } IBinding binding = null; // TODO ASTNode node = null; // getHoveredASTNode(editorInputElement, hoverRegion); if (node == null) { // todo use ast ported parser,that uses our java model // ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); // p.setProject(element.getJavaProject()); // p.setBindingsRecovery(true); // try { // binding = p.createBindings(new IJavaElement[]{element}, null)[0]; // } catch (OperationCanceledException e) { // return null; // } } else { binding = resolveBinding(node); } if (binding == null) return null; IAnnotationBinding[] annotations = binding.getAnnotations(); if (annotations.length == 0) return null; StringBuffer buf = new StringBuffer(); for (int i = 0; i < annotations.length; i++) { // TODO: skip annotations that don't have an @Documented annotation? addAnnotation(buf, element, annotations[i]); buf.append("<br>"); // $NON-NLS-1$ } return buf.toString(); }
private List<IAnnotation> fetchActionAnnotationValue( IAnnotatable annotatable, boolean actionsImportExist, boolean actionImportExist) throws JavaModelException { List<IAnnotation> result = new ArrayList<IAnnotation>(); IAnnotation[] annotations = annotatable.getAnnotations(); for (IAnnotation annotation : annotations) { if (annotation.exists()) { boolean fetchValue = ACTIONS_ANNOTATION_FQN.equals(annotation .getElementName()) || (ACTIONS_ANNOTATION.equals(annotation .getElementName()) && actionsImportExist); if (fetchValue) { IMemberValuePair[] valuePairs = annotation .getMemberValuePairs(); for (IMemberValuePair valuePair : valuePairs) { if (valuePair.getValueKind() == IMemberValuePair.K_ANNOTATION && ANNOTATION_VALUE.equals(valuePair .getMemberName())) { // single value if (valuePair.getValue() instanceof IAnnotation) { result.add((IAnnotation) valuePair.getValue()); } else { // array Object[] objs = (Object[]) valuePair.getValue(); for (Object o : objs) { result.add((IAnnotation) o); } } } } } else { fetchValue = ACTION_ANNOTATION_FQN.equals(annotation .getElementName()) || (ACTION_ANNOTATION.equals(annotation .getElementName()) && actionImportExist); if (fetchValue) { result.add(annotation); } } } } return result; }
public final IJavaElement transplantHandle(IJavaElement element) { IJavaElement parent= element.getParent(); if (parent != null) parent= transplantHandle(parent); // recursive switch (element.getElementType()) { case IJavaElement.JAVA_MODEL: return transplantHandle((IJavaModel) element); case IJavaElement.JAVA_PROJECT: return transplantHandle((IJavaProject) element); case IJavaElement.PACKAGE_FRAGMENT_ROOT: return transplantHandle((IJavaProject) parent, (IPackageFragmentRoot) element); case IJavaElement.PACKAGE_FRAGMENT: return transplantHandle((IPackageFragmentRoot) parent, (IPackageFragment) element); case IJavaElement.COMPILATION_UNIT: return transplantHandle((IPackageFragment) parent, (ICompilationUnit) element); case IJavaElement.CLASS_FILE: return transplantHandle((IPackageFragment) parent, (IClassFile) element); case IJavaElement.TYPE: return transplantHandle(parent, (IType) element); case IJavaElement.FIELD: return transplantHandle((IType) parent, (IField) element); case IJavaElement.METHOD: return transplantHandle((IType) parent, (IMethod) element); case IJavaElement.INITIALIZER: return transplantHandle((IType) parent, (IInitializer) element); case IJavaElement.PACKAGE_DECLARATION: return transplantHandle((ICompilationUnit) parent, (IPackageDeclaration) element); case IJavaElement.IMPORT_CONTAINER: return transplantHandle((ICompilationUnit) parent, (IImportContainer) element); case IJavaElement.IMPORT_DECLARATION: return transplantHandle((IImportContainer) parent, (IImportDeclaration) element); case IJavaElement.LOCAL_VARIABLE: return transplantHandle((ILocalVariable) element); case IJavaElement.TYPE_PARAMETER: return transplantHandle((IMember) parent, (ITypeParameter) element); case IJavaElement.ANNOTATION: return transplantHandle((IAnnotatable) parent, (IAnnotation) element); default: throw new IllegalArgumentException(element.toString()); } }
private static String getAnnotations(IJavaElement element, ITypeRoot editorInputElement, IRegion hoverRegion) throws URISyntaxException, JavaModelException { if (!(element instanceof IPackageFragment)) { if (!(element instanceof IAnnotatable)) return null; if (((IAnnotatable)element).getAnnotations().length == 0) return null; } IBinding binding; ASTNode node= getHoveredASTNode(editorInputElement, hoverRegion); if (node == null) { ASTParser p= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL); p.setProject(element.getJavaProject()); p.setBindingsRecovery(true); try { binding= p.createBindings(new IJavaElement[] { element }, null)[0]; } catch (OperationCanceledException e) { return null; } } else { binding= resolveBinding(node); } if (binding == null) return null; IAnnotationBinding[] annotations= binding.getAnnotations(); if (annotations.length == 0) return null; StringBuffer buf= new StringBuffer(); for (int i= 0; i < annotations.length; i++) { //TODO: skip annotations that don't have an @Documented annotation? addAnnotation(buf, element, annotations[i]); buf.append("<br>"); //$NON-NLS-1$ } return buf.toString(); }
public IJavaElement getJavaElement() { if (!(this.bindingResolver instanceof DefaultBindingResolver)) return null; ASTNode node = (ASTNode) ((DefaultBindingResolver) this.bindingResolver).bindingsToAstNodes.get(this); if (!(node instanceof Annotation)) return null; ASTNode parent = node.getParent(); IJavaElement parentElement = null; switch (parent.getNodeType()) { case ASTNode.PACKAGE_DECLARATION: IJavaElement cu = ((CompilationUnit) parent.getParent()).getJavaElement(); if (cu instanceof ICompilationUnit) { String pkgName = ((PackageDeclaration) parent).getName().getFullyQualifiedName(); parentElement = ((ICompilationUnit) cu).getPackageDeclaration(pkgName); } break; case ASTNode.ENUM_DECLARATION: case ASTNode.TYPE_DECLARATION: case ASTNode.ANNOTATION_TYPE_DECLARATION: parentElement = ((AbstractTypeDeclaration) parent).resolveBinding().getJavaElement(); break; case ASTNode.FIELD_DECLARATION: VariableDeclarationFragment fragment = (VariableDeclarationFragment) ((FieldDeclaration) parent).fragments().get(0); IVariableBinding variableBinding = fragment.resolveBinding(); if (variableBinding == null) { return null; } parentElement = variableBinding.getJavaElement(); break; case ASTNode.METHOD_DECLARATION: IMethodBinding methodBinding = ((MethodDeclaration) parent).resolveBinding(); if (methodBinding == null) return null; parentElement = methodBinding.getJavaElement(); break; case ASTNode.VARIABLE_DECLARATION_STATEMENT: fragment = (VariableDeclarationFragment) ((VariableDeclarationStatement) parent).fragments().get(0); variableBinding = fragment.resolveBinding(); if (variableBinding == null) { return null; } parentElement = variableBinding.getJavaElement(); break; default: return null; } if (! (parentElement instanceof IAnnotatable)) return null; if ((parentElement instanceof IMember) && ((IMember) parentElement).isBinary()) { return ((IAnnotatable) parentElement).getAnnotation(getAnnotationType().getQualifiedName()); } return ((IAnnotatable) parentElement).getAnnotation(getName()); }
public List<ICompletionProposal> computeCompletionProposals( ContentAssistInvocationContext context, IProgressMonitor monitor) { if (context instanceof JavaContentAssistInvocationContext) { JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext)context; ICompilationUnit unit = javaContext.getCompilationUnit(); try { if (unit == null || !unit.isStructureKnown()) return Collections.emptyList(); IType primaryType = unit.findPrimaryType(); if (primaryType == null || !primaryType.isInterface()) return Collections.emptyList(); int offset = javaContext.getInvocationOffset(); IJavaElement element = unit.getElementAt(offset); if (element == null || !(element instanceof IMethod)) return Collections.emptyList(); IAnnotation annotation = JavaMapperUtil.getAnnotationAt((IAnnotatable)element, offset); if (annotation == null) return Collections.emptyList(); final IJavaProject project = javaContext.getProject(); final IMethod method = (IMethod)element; final String mapperFqn = method.getDeclaringType().getFullyQualifiedName(); if (isInlineStatementAnnotation(annotation)) { return proposeStatementText(project, unit, offset, annotation, method); } else { String elementName = annotation.getElementName(); if ("ResultMap".equals(elementName)) { return proposeResultMap(project, mapperFqn, offset, annotation); } else if ("Results".equals(elementName)) { return proposeResults(project, mapperFqn, offset, annotation, method); } else if ("ConstructorArgs".equals(elementName)) { return proposeConstructorArgs(project, mapperFqn, offset, annotation, method); } else if ("Options".equals(elementName) || "SelectKey".equals(elementName)) { return proposeKeyProperty(project, mapperFqn, offset, annotation, method); } } } catch (JavaModelException e) { Activator.log(Status.ERROR, "Something went wrong.", e); } } return Collections.emptyList(); }