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

The following examples show how to use org.eclipse.jdt.core.IField#getJavaProject() . 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: JavadocView.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Compute the textual representation of a 'static' 'final' field's constant initializer value.
 *
 * @param activePart the part that triggered the computation, or <code>null</code>
 * @param selection the selection that references the field, or <code>null</code>
 * @param resolvedField the filed whose constant value will be computed
 * @param monitor the progress monitor
 *
 * @return the textual representation of the constant, or <code>null</code> if the
 *   field is not a constant field, the initializer value could not be computed, or
 *   the progress monitor was cancelled
 * @since 3.4
 */
private String computeFieldConstant(IWorkbenchPart activePart, ISelection selection, IField resolvedField, IProgressMonitor monitor) {

	if (!JavadocHover.isStaticFinal(resolvedField))
		return null;

	Object constantValue;
	IJavaProject preferenceProject;

	if (selection instanceof ITextSelection && activePart instanceof JavaEditor) {
		IEditorPart editor= (IEditorPart) activePart;
		ITypeRoot activeType= JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
		preferenceProject= activeType.getJavaProject();
		constantValue= getConstantValueFromActiveEditor(activeType, resolvedField, (ITextSelection) selection, monitor);
		if (constantValue == null) // fall back - e.g. when selection is inside Javadoc of the element
			constantValue= computeFieldConstantFromTypeAST(resolvedField, monitor);
	} else {
		constantValue= computeFieldConstantFromTypeAST(resolvedField, monitor);
		preferenceProject= resolvedField.getJavaProject();
	}

	if (constantValue != null)
		return JavadocHover.getFormattedAssignmentOperator(preferenceProject) + formatCompilerConstantValue(constantValue);

	return null;
}
 
Example 2
Source File: RenameEnumConstProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
protected RenameJavaElementDescriptor createRefactoringDescriptor() {
	final IField field= getField();
	String project= null;
	IJavaProject javaProject= field.getJavaProject();
	if (javaProject != null) {
		project= javaProject.getElementName();
	}
	int flags= JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
	final IType declaring= field.getDeclaringType();
	try {
		if (!Flags.isPrivate(declaring.getFlags())) {
			flags|= RefactoringDescriptor.MULTI_CHANGE;
		}
		if (declaring.isAnonymous() || declaring.isLocal()) {
			flags|= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
		}
	} catch (JavaModelException exception) {
		JavaLanguageServerPlugin.log(exception);
	}
	final String description= Messages.format(RefactoringCoreMessages.RenameEnumConstProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fField.getElementName()));
	final String header= Messages.format(RefactoringCoreMessages.RenameEnumConstProcessor_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(field.getElementName()), JavaElementLabels.getElementLabel(field.getParent(), JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(getNewElementName())});
	final String comment= new JDTRefactoringDescriptorComment(project, this, header).asString();
	final RenameJavaElementDescriptor descriptor= RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_ENUM_CONSTANT);
	descriptor.setProject(project);
	descriptor.setDescription(description);
	descriptor.setComment(comment);
	descriptor.setFlags(flags);
	descriptor.setJavaElement(field);
	descriptor.setNewName(getNewElementName());
	descriptor.setUpdateReferences(fUpdateReferences);
	descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
	return descriptor;
}
 
Example 3
Source File: RenameEnumConstProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
protected RenameJavaElementDescriptor createRefactoringDescriptor() {
	final IField field= getField();
	String project= null;
	IJavaProject javaProject= field.getJavaProject();
	if (javaProject != null)
		project= javaProject.getElementName();
	int flags= JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
	final IType declaring= field.getDeclaringType();
	try {
		if (!Flags.isPrivate(declaring.getFlags()))
			flags|= RefactoringDescriptor.MULTI_CHANGE;
		if (declaring.isAnonymous() || declaring.isLocal())
			flags|= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
	} catch (JavaModelException exception) {
		JavaPlugin.log(exception);
	}
	final String description= Messages.format(RefactoringCoreMessages.RenameEnumConstProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fField.getElementName()));
	final String header= Messages.format(RefactoringCoreMessages.RenameEnumConstProcessor_descriptor_description, new String[] { BasicElementLabels.getJavaElementName(field.getElementName()), JavaElementLabels.getElementLabel(field.getParent(), JavaElementLabels.ALL_FULLY_QUALIFIED), BasicElementLabels.getJavaElementName(getNewElementName())});
	final String comment= new JDTRefactoringDescriptorComment(project, this, header).asString();
	final RenameJavaElementDescriptor descriptor= RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_ENUM_CONSTANT);
	descriptor.setProject(project);
	descriptor.setDescription(description);
	descriptor.setComment(comment);
	descriptor.setFlags(flags);
	descriptor.setJavaElement(field);
	descriptor.setNewName(getNewElementName());
	descriptor.setUpdateReferences(fUpdateReferences);
	descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
	return descriptor;
}
 
Example 4
Source File: GetterSetterUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Create a stub for a getter of the given field using getter/setter templates. The resulting code
 * has to be formatted and indented.
 * @param field The field to create a getter for
 * @param setterName The chosen name for the setter
 * @param addComments If <code>true</code>, comments will be added.
 * @param flags The flags signaling visibility, if static, synchronized or final
 * @return Returns the generated stub.
 * @throws CoreException when stub creation failed
 */
public static String getSetterStub(IField field, String setterName, boolean addComments, int flags) throws CoreException {

	String fieldName= field.getElementName();
	IType parentType= field.getDeclaringType();

	String returnSig= field.getTypeSignature();
	String typeName= Signature.toString(returnSig);

	IJavaProject project= field.getJavaProject();

	String accessorName= StubUtility.getBaseName(field);
	String argname= StubUtility.suggestArgumentName(project, accessorName, EMPTY);

	boolean isStatic= Flags.isStatic(flags);
	boolean isSync= Flags.isSynchronized(flags);
	boolean isFinal= Flags.isFinal(flags);

	String lineDelim= "\n"; // Use default line delimiter, as generated stub has to be formatted anyway //$NON-NLS-1$
	StringBuffer buf= new StringBuffer();
	if (addComments) {
		String comment= CodeGeneration.getSetterComment(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, field.getElementName(), typeName, argname, accessorName, lineDelim);
		if (comment != null) {
			buf.append(comment);
			buf.append(lineDelim);
		}
	}
	buf.append(JdtFlags.getVisibilityString(flags));
	buf.append(' ');
	if (isStatic)
		buf.append("static "); //$NON-NLS-1$
	if (isSync)
		buf.append("synchronized "); //$NON-NLS-1$
	if (isFinal)
		buf.append("final "); //$NON-NLS-1$

	buf.append("void "); //$NON-NLS-1$
	buf.append(setterName);
	buf.append('(');
	buf.append(typeName);
	buf.append(' ');
	buf.append(argname);
	buf.append(") {"); //$NON-NLS-1$
	buf.append(lineDelim);

	boolean useThis= StubUtility.useThisForFieldAccess(project);
	if (argname.equals(fieldName) || (useThis && !isStatic)) {
		if (isStatic)
			fieldName= parentType.getElementName() + '.' + fieldName;
		else
			fieldName= "this." + fieldName; //$NON-NLS-1$
	}
	String body= CodeGeneration.getSetterMethodBodyContent(field.getCompilationUnit(), parentType.getTypeQualifiedName('.'), setterName, fieldName, argname, lineDelim);
	if (body != null) {
		buf.append(body);
	}
	buf.append("}"); //$NON-NLS-1$
	buf.append(lineDelim);
	return buf.toString();
}