org.eclipse.jdt.core.IAnnotatable Java Examples

The following examples show how to use org.eclipse.jdt.core.IAnnotatable. 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: JDTUtils.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
public static boolean isHiddenGeneratedElement(IJavaElement element) {
	// generated elements are annotated with @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;
}
 
Example #2
Source File: StubCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
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));
}
 
Example #3
Source File: Jdt2Ecore.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Replies the annotation with the given qualified name.
 *
 * @param element the annoted element.
 * @param qualifiedName the qualified name of the element.
 * @return the annotation, or {@code null} if the element is not annoted.
 */
public IAnnotation getAnnotation(IAnnotatable element, String qualifiedName) {
	if (element != null) {
		try {
			final int separator = qualifiedName.lastIndexOf('.');
			final String simpleName;
			if (separator >= 0 && separator < (qualifiedName.length() - 1)) {
				simpleName = qualifiedName.substring(separator + 1, qualifiedName.length());
			} else {
				simpleName = qualifiedName;
			}
			for (final IAnnotation annotation : element.getAnnotations()) {
				final String name = annotation.getElementName();
				if (name.equals(simpleName) || name.equals(qualifiedName)) {
					return annotation;
				}
			}
		} catch (JavaModelException e) {
			//
		}
	}
	return null;
}
 
Example #4
Source File: PlatformJavaModelUtilities.java    From gwt-eclipse-plugin with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: JavaElementFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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());
}
 
Example #6
Source File: SourceTypeConverter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
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;
}
 
Example #7
Source File: GenericRefactoringHandleTransplanter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
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());
	}

}
 
Example #8
Source File: GenericRefactoringHandleTransplanter.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
protected IAnnotation transplantHandle(IAnnotatable parent, IAnnotation element) {
	return parent.getAnnotation(element.getElementName());
}
 
Example #9
Source File: GenericRefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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());
	}

}
 
Example #10
Source File: GenericRefactoringHandleTransplanter.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
protected IAnnotation transplantHandle(IAnnotatable parent, IAnnotation element) {
	return parent.getAnnotation(element.getElementName());
}
 
Example #11
Source File: JavadocHover.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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();
}
 
Example #12
Source File: AnnotationBinding.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
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());
}