Java Code Examples for org.eclipse.jdt.core.IField#getNameRange()

The following examples show how to use org.eclipse.jdt.core.IField#getNameRange() . 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: JavadocContentAccess2.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private boolean handleConstantValue(IField field, boolean link) throws JavaModelException {
	String text = null;

	ISourceRange nameRange = field.getNameRange();
	if (SourceRange.isAvailable(nameRange)) {
		CompilationUnit cuNode = CoreASTProvider.getInstance().getAST(field.getTypeRoot(), CoreASTProvider.WAIT_YES, new NullProgressMonitor());
		if (cuNode != null) {
			ASTNode nameNode = NodeFinder.perform(cuNode, nameRange);
			if (nameNode instanceof SimpleName) {
				IBinding binding = ((SimpleName) nameNode).resolveBinding();
				if (binding instanceof IVariableBinding) {
					IVariableBinding variableBinding = (IVariableBinding) binding;
					Object constantValue = variableBinding.getConstantValue();
					if (constantValue != null) {
						if (constantValue instanceof String) {
							text = ASTNodes.getEscapedStringLiteral((String) constantValue);
						} else {
							text = constantValue.toString(); // Javadoc tool is even worse for chars...
						}
					}
				}
			}
		}
	}

	if (text == null) {
		Object constant = field.getConstant();
		if (constant != null) {
			text = constant.toString();
		}
	}

	if (text != null) {
		text = convertToHTMLContentWithWhitespace(text);
		if (link) {
			String uri;
			try {
				uri = JavaElementLinks.createURI("eclipse-javadoc", field);
				fBuf.append(JavaElementLinks.createLink(uri, text));
			} catch (URISyntaxException e) {
				return false;
			}
		} else {
			handleText(text);
		}
		return true;
	}
	return false;
}
 
Example 2
Source File: InlineConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
	final String selection= arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
	if (selection != null) {
		int offset= -1;
		int length= -1;
		final StringTokenizer tokenizer= new StringTokenizer(selection);
		if (tokenizer.hasMoreTokens())
			offset= Integer.valueOf(tokenizer.nextToken()).intValue();
		if (tokenizer.hasMoreTokens())
			length= Integer.valueOf(tokenizer.nextToken()).intValue();
		if (offset >= 0 && length >= 0) {
			fSelectionStart= offset;
			fSelectionLength= length;
		} else
			return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION}));
	}
	final String handle= arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
	if (handle != null) {
		final IJavaElement element= JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
		if (element == null || !element.exists())
			return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
		else {
			if (element instanceof ICompilationUnit) {
				fSelectionCu= (ICompilationUnit) element;
				if (selection == null)
					return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
			} else if (element instanceof IField) {
				final IField field= (IField) element;
				try {
					final ISourceRange range= field.getNameRange();
					if (range != null) {
						fSelectionStart= range.getOffset();
						fSelectionLength= range.getLength();
					} else
						return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, IJavaRefactorings.INLINE_CONSTANT));
				} catch (JavaModelException exception) {
					return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
				}
				fSelectionCu= field.getCompilationUnit();
			} else
				return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT}));
			final ASTParser parser= ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
			parser.setResolveBindings(true);
			parser.setSource(fSelectionCu);
			final CompilationUnit unit= (CompilationUnit) parser.createAST(null);
			initialize(fSelectionCu, unit);
			if (checkStaticFinalConstantNameSelected().hasFatalError())
				return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
		}
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
	final String replace= arguments.getAttribute(ATTRIBUTE_REPLACE);
	if (replace != null) {
		fReplaceAllReferences= Boolean.valueOf(replace).booleanValue();
	} else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REPLACE));
	final String remove= arguments.getAttribute(ATTRIBUTE_REMOVE);
	if (remove != null)
		fRemoveDeclaration= Boolean.valueOf(remove).booleanValue();
	else
		return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REMOVE));
	return new RefactoringStatus();
}
 
Example 3
Source File: JavadocContentAccess2.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean handleConstantValue(IField field, boolean link) throws JavaModelException {
	String text= null;
	
	ISourceRange nameRange= field.getNameRange();
	if (SourceRange.isAvailable(nameRange)) {
		CompilationUnit cuNode= SharedASTProvider.getAST(field.getTypeRoot(), SharedASTProvider.WAIT_ACTIVE_ONLY, null);
		if (cuNode != null) {
			ASTNode nameNode= NodeFinder.perform(cuNode, nameRange);
			if (nameNode instanceof SimpleName) {
				IBinding binding= ((SimpleName) nameNode).resolveBinding();
				if (binding instanceof IVariableBinding) {
					IVariableBinding variableBinding= (IVariableBinding) binding;
					Object constantValue= variableBinding.getConstantValue();
					if (constantValue != null) {
						if (constantValue instanceof String) {
							text= ASTNodes.getEscapedStringLiteral((String) constantValue);
						} else {
							text= constantValue.toString(); // Javadoc tool is even worse for chars...
						}
					}
				}
			}
		}
	}
	
	if (text == null) {
		Object constant= field.getConstant();
		if (constant != null) {
			text= constant.toString();
		}
	}
	
	if (text != null) {
		text= HTMLPrinter.convertToHTMLContentWithWhitespace(text);
		if (link) {
			String uri;
			try {
				uri= JavaElementLinks.createURI(JavaElementLinks.JAVADOC_SCHEME, field);
				fBuf.append(JavaElementLinks.createLink(uri, text));
			} catch (URISyntaxException e) {
				JavaPlugin.log(e);
				return false;
			}
		} else {
			handleText(text);
		}
		return true;
	}
	return false;
}