Java Code Examples for org.eclipse.jdt.core.IJavaProject#getOption()

The following examples show how to use org.eclipse.jdt.core.IJavaProject#getOption() . 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: TokenScanner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a TokenScanner
 * @param document The textbuffer to create the scanner on
 * @param project the current Java project
 */
public TokenScanner(IDocument document, IJavaProject project) {
	String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true);
	String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
	fScanner= ToolFactory.createScanner(true, false, false, sourceLevel, complianceLevel); // no line info required
	fScanner.setSource(document.get().toCharArray());
	fDocument= document;
	fEndPosition= fScanner.getSource().length - 1;
}
 
Example 2
Source File: StubUtility.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean hasConstantName(IJavaProject project, String name) {
	if (Character.isUpperCase(name.charAt(0)))
		return true;
	String prefixes= project.getOption(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_PREFIXES, true);
	String suffixes= project.getOption(JavaCore.CODEASSIST_STATIC_FINAL_FIELD_SUFFIXES, true);
	return hasPrefixOrSuffix(prefixes, suffixes, name);
}
 
Example 3
Source File: NLSScanner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static NLSLine[] scan(ICompilationUnit cu) throws JavaModelException, BadLocationException, InvalidInputException {
	IJavaProject javaProject= cu.getJavaProject();
	IScanner scanner= null;
	if (javaProject != null) {
		String complianceLevel= javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
		String sourceLevel= javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
		scanner= ToolFactory.createScanner(true, true, true, sourceLevel, complianceLevel);
	} else {
		scanner= ToolFactory.createScanner(true, true, false, true);
	}
	return scan(scanner, cu.getBuffer().getCharacters());
}
 
Example 4
Source File: CuCollectingSearchRequestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected IScanner getScanner(ICompilationUnit unit) {
	IJavaProject project= unit.getJavaProject();
	if (project.equals(fProjectCache))
		return fScannerCache;

	fProjectCache= project;
	String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true);
	String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
	fScannerCache= ToolFactory.createScanner(false, false, false, sourceLevel, complianceLevel);
	return fScannerCache;
}
 
Example 5
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void addNullityAnnotationTypesProposals(ICompilationUnit cu, Name node, Collection<ICommandAccess> proposals) throws CoreException {
	ASTNode parent= node.getParent();
	boolean isAnnotationName= parent instanceof Annotation && ((Annotation) parent).getTypeNameProperty() == node.getLocationInParent();
	if (!isAnnotationName) {
		boolean isImportName= parent instanceof ImportDeclaration && ImportDeclaration.NAME_PROPERTY == node.getLocationInParent();
		if (!isImportName)
			return;
	}
	
	final IJavaProject javaProject= cu.getJavaProject();
	String name= node.getFullyQualifiedName();
	
	String nullityAnnotation= null;
	String[] annotationNameOptions= { JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, JavaCore.COMPILER_NONNULL_BY_DEFAULT_ANNOTATION_NAME };
	Hashtable<String, String> defaultOptions= JavaCore.getDefaultOptions();
	for (String annotationNameOption : annotationNameOptions) {
		String annotationName= javaProject.getOption(annotationNameOption, true);
		if (! annotationName.equals(defaultOptions.get(annotationNameOption)))
			return;
		if (JavaModelUtil.isMatchingName(name, annotationName)) {
			nullityAnnotation= annotationName;
		}
	}
	if (nullityAnnotation == null)
		return;
	if (javaProject.findType(defaultOptions.get(annotationNameOptions[0])) != null)
		return;
	String version= JavaModelUtil.is18OrHigher(javaProject) ? "2" : "[1.1.0,2.0.0)"; //$NON-NLS-1$ //$NON-NLS-2$
	Bundle[] annotationsBundles= JavaPlugin.getDefault().getBundles("org.eclipse.jdt.annotation", version); //$NON-NLS-1$
	if (annotationsBundles == null)
		return;
	
	if (! cu.getJavaProject().getProject().hasNature("org.eclipse.pde.PluginNature")) //$NON-NLS-1$
		addCopyAnnotationsJarProposal(cu, node, nullityAnnotation, annotationsBundles[0], proposals);
}
 
Example 6
Source File: TokenScanner.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Creates a TokenScanner
 * @param typeRoot The type root to scan on
 * @throws CoreException thrown if the buffer cannot be accessed
 */
public TokenScanner(ITypeRoot typeRoot) throws CoreException {
	IJavaProject project= typeRoot.getJavaProject();
	IBuffer buffer= typeRoot.getBuffer();
	if (buffer == null) {
		throw new CoreException(createError(DOCUMENT_ERROR, "Element has no source", null)); //$NON-NLS-1$
	}
	String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true);
	String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
	fScanner= ToolFactory.createScanner(true, false, true, sourceLevel, complianceLevel); // line info required

	fScanner.setSource(buffer.getCharacters());
	fDocument= null; // use scanner for line information
	fEndPosition= fScanner.getSource().length - 1;
}
 
Example 7
Source File: SuppressWarningsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static final boolean hasSuppressWarningsProposal(IJavaProject javaProject, int problemId) {
	if (CorrectionEngine.getWarningToken(problemId) != null && JavaModelUtil.is50OrHigher(javaProject)) {
		String optionId= JavaCore.getOptionForConfigurableSeverity(problemId);
		if (optionId != null) {
			String optionValue= javaProject.getOption(optionId, true);
			return JavaCore.WARNING.equals(optionValue) ||
					(JavaCore.ERROR.equals(optionValue) && JavaCore.ENABLED.equals(javaProject.getOption(JavaCore.COMPILER_PB_SUPPRESS_OPTIONAL_ERRORS, true)));
		}
	}
	return false;
}
 
Example 8
Source File: JdtUtils.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @param javaElt
 * @return true, if corresponding java project has compiler setting to
 *         generate bytecode for jdk 1.5 and above
 */
private static boolean is50OrHigher(IJavaElement javaElt) {
    IJavaProject project = javaElt.getJavaProject();
    String option = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
    boolean result = JavaCore.VERSION_1_5.equals(option);
    if (result) {
        return result;
    }
    // probably > 1.5?
    result = JavaCore.VERSION_1_4.equals(option);
    if (result) {
        return false;
    }
    result = JavaCore.VERSION_1_3.equals(option);
    if (result) {
        return false;
    }
    result = JavaCore.VERSION_1_2.equals(option);
    if (result) {
        return false;
    }
    result = JavaCore.VERSION_1_1.equals(option);
    if (result) {
        return false;
    }
    // unknown = > 1.5
    return true;
}
 
Example 9
Source File: ReorgCorrectionsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds a proposal to increase the compiler compliance level
 * @param context the context
 * @param problem the current problem
 * @param proposals the resulting proposals
 * @param requiredVersion the minimal required Java compiler version
 */
public static void getNeedHigherComplianceProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals, String requiredVersion) {
	IJavaProject project= context.getCompilationUnit().getJavaProject();

	String label1= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_change_project_compliance_description, requiredVersion);
	proposals.add(new ChangeToRequiredCompilerCompliance(label1, project, false, requiredVersion, IProposalRelevance.CHANGE_PROJECT_COMPLIANCE));

	if (project.getOption(JavaCore.COMPILER_COMPLIANCE, false) == null) {
		String label2= Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_change_workspace_compliance_description, requiredVersion);
		proposals.add(new ChangeToRequiredCompilerCompliance(label2, project, true, requiredVersion, IProposalRelevance.CHANGE_WORKSPACE_COMPLIANCE));
	}
}
 
Example 10
Source File: PackageFragment.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
protected boolean internalIsValidPackageName() {
	// if package fragment refers to folder in another IProject, then
	// resource().getProject() is different than getJavaProject().getProject()
	// use the other java project's options to verify the name
	IJavaProject javaProject = JavaCore.create(resource().getProject());
	String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
	String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
	for (int i = 0, length = this.names.length; i < length; i++) {
		if (!Util.isValidFolderNameForPackage(this.names[i], sourceLevel, complianceLevel))
			return false;
	}
	return true;
}
 
Example 11
Source File: JavaConventionsUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @param context
 *            an {@link IJavaElement} or <code>null</code>
 * @return a <code>String[]</code> whose <code>[0]</code> is the
 *         {@link JavaCore#COMPILER_SOURCE} and whose <code>[1]</code> is
 *         the {@link JavaCore#COMPILER_COMPLIANCE} level at the given
 *         <code>context</code>.
 */
public static String[] getSourceComplianceLevels(IJavaElement context) {
	if (context != null) {
		IJavaProject javaProject = context.getJavaProject();
		if (javaProject != null) {
			return new String[] { javaProject.getOption(JavaCore.COMPILER_SOURCE, true), javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true) };
		}
	}
	return new String[] { JavaCore.getOption(JavaCore.COMPILER_SOURCE), JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE) };
}
 
Example 12
Source File: CompletionProposalReplacementProvider.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private final boolean shouldProposeGenerics(IJavaProject project) {
	String sourceVersion;
	if (project != null) {
		sourceVersion= project.getOption(JavaCore.COMPILER_SOURCE, true);
	} else {
		sourceVersion= JavaCore.getOption(JavaCore.COMPILER_SOURCE);
	}

	return !isVersionLessThan(sourceVersion, JavaCore.VERSION_1_5);
}
 
Example 13
Source File: SnippetCompletionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static CompletionItem getRecordSnippet(SnippetCompletionContext scc, IProgressMonitor monitor) {
	ICompilationUnit cu = scc.getCompilationUnit();
	IJavaProject javaProject = cu.getJavaProject();
	if (javaProject == null) {
		return null;
	}
	String version = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
	if (JavaModelUtil.isVersionLessThan(version, JavaCore.VERSION_14)) {
		//not checking if preview features are enabled, as Java 14+ might support records without preview flag
		return null;
	}
	if (!accept(cu, scc.getCompletionContext(), false /* Or should it be true???*/)) {
		return null;
	}
	if (monitor.isCanceled()) {
		return null;
	}
	final CompletionItem recordSnippet = new CompletionItem();
	recordSnippet.setFilterText(RECORD_SNIPPET_LABEL);
	recordSnippet.setLabel(RECORD_SNIPPET_LABEL);
	recordSnippet.setSortText(SortTextHelper.convertRelevance(0));

	try {
		CodeGenerationTemplate template = (scc.needsPublic(monitor)) ? CodeGenerationTemplate.RECORDSNIPPET_PUBLIC : CodeGenerationTemplate.RECORDSNIPPET_DEFAULT;
		recordSnippet.setInsertText(getSnippetContent(scc, template, true));
		setFields(recordSnippet, cu);
	} catch (CoreException e) {
		JavaLanguageServerPlugin.log(e.getStatus());
		return null;
	}
	return recordSnippet;
}
 
Example 14
Source File: XbaseBuilderConfigurationBlock.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String javaValue(final String javaPreference) {
	IJavaProject javaProject = JavaCore.create(getProject());
	if (javaProject != null && javaProject.exists() && javaProject.getProject().isAccessible()) {
		return javaProject.getOption(javaPreference, true);
	} else {
		return JavaCore.getOption(javaPreference);
	}
}
 
Example 15
Source File: XbaseValidationConfigurationBlock.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected String javaValue(final String javaIssueCode) {
	String delegatedValue;
	String decodedDelegateKey = XbaseSeverityConverter.decodeDelegationKey(javaIssueCode).getFirst();
	IJavaProject javaProject = JavaCore.create(getProject());
	if (javaProject != null && javaProject.exists() && javaProject.getProject().isAccessible()) {
		delegatedValue = javaProject.getOption(decodedDelegateKey, true);
	} else {
		delegatedValue = JavaCore.getOption(decodedDelegateKey);
	}
	return delegatedValue;
}
 
Example 16
Source File: JavadocOptionsManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void loadDefaults(List<?> sel) {
	fInitialElements= getInitialElementsFromSelection(sel);

	IJavaProject project= getSingleProjectFromInitialSelection();

	if (project != null) {
		fAntpath= getRecentSettings().getAntpath(project);
		fDestination= getRecentSettings().getDestination(project);
		fHRefs= getRecentSettings().getHRefs(project);
	} else {
		fAntpath= ""; //$NON-NLS-1$
		fDestination= ""; //$NON-NLS-1$
		fHRefs= new String[0];
	}

	fAccess= PUBLIC;

	fDocletname= ""; //$NON-NLS-1$
	fDocletpath= ""; //$NON-NLS-1$
	fTitle= ""; //$NON-NLS-1$
	fStylesheet= ""; //$NON-NLS-1$
	fVMParams= ""; //$NON-NLS-1$
	fAdditionalParams= ""; //$NON-NLS-1$
	fOverview= ""; //$NON-NLS-1$

	fUse= true;
	fAuthor= true;
	fVersion= true;
	fNodeprecated= false;
	fNoDeprecatedlist= false;
	fNonavbar= false;
	fNoindex= false;
	fNotree= false;
	fSplitindex= true;
	fOpenInBrowser= false;
	fSource= "1.3"; //$NON-NLS-1$
	if (project != null) {
		fSource= project.getOption(JavaCore.COMPILER_SOURCE, true);
	}

	//by default it is empty all project map to the empty string
	fFromStandard= true;
}
 
Example 17
Source File: JavadocOptionsManager.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private void loadFromDialogStore(IDialogSettings settings, List<?> sel) {
	fInitialElements= getInitialElementsFromSelection(sel);

	IJavaProject project= getSingleProjectFromInitialSelection();

	fAccess= settings.get(VISIBILITY);
	if (fAccess == null)
		fAccess= PROTECTED;

	//this is defaulted to false.
	fFromStandard= settings.getBoolean(FROMSTANDARD);

	//doclet is loaded even if the standard doclet is being used
	fDocletpath= settings.get(DOCLETPATH);
	fDocletname= settings.get(DOCLETNAME);
	if (fDocletpath == null || fDocletname == null) {
		fFromStandard= true;
		fDocletpath= ""; //$NON-NLS-1$
		fDocletname= ""; //$NON-NLS-1$
	}


	if (project != null) {
		fAntpath= getRecentSettings().getAntpath(project);
	} else {
		fAntpath= settings.get(ANTPATH);
		if (fAntpath == null) {
			fAntpath= ""; //$NON-NLS-1$
		}
	}


	if (project != null) {
		fDestination= getRecentSettings().getDestination(project);
	} else {
		fDestination= settings.get(DESTINATION);
		if (fDestination == null) {
			fDestination= ""; //$NON-NLS-1$
		}
	}

	fTitle= settings.get(TITLE);
	if (fTitle == null)
		fTitle= ""; //$NON-NLS-1$

	fStylesheet= settings.get(STYLESHEETFILE);
	if (fStylesheet == null)
		fStylesheet= ""; //$NON-NLS-1$

	fVMParams= settings.get(VMOPTIONS);
	if (fVMParams == null)
		fVMParams= ""; //$NON-NLS-1$

	fAdditionalParams= settings.get(EXTRAOPTIONS);
	if (fAdditionalParams == null)
		fAdditionalParams= ""; //$NON-NLS-1$

	fOverview= settings.get(OVERVIEW);
	if (fOverview == null)
		fOverview= ""; //$NON-NLS-1$

	fUse= loadBoolean(settings.get(USE));
	fAuthor= loadBoolean(settings.get(AUTHOR));
	fVersion= loadBoolean(settings.get(VERSION));
	fNodeprecated= loadBoolean(settings.get(NODEPRECATED));
	fNoDeprecatedlist= loadBoolean(settings.get(NODEPRECATEDLIST));
	fNonavbar= loadBoolean(settings.get(NONAVBAR));
	fNoindex= loadBoolean(settings.get(NOINDEX));
	fNotree= loadBoolean(settings.get(NOTREE));
	fSplitindex= loadBoolean(settings.get(SPLITINDEX));
	fOpenInBrowser= loadBoolean(settings.get(OPENINBROWSER));

	fSource= settings.get(SOURCE);
	if (project != null) {
		fSource= project.getOption(JavaCore.COMPILER_SOURCE, true);
	}

	if (project != null) {
		fHRefs= getRecentSettings().getHRefs(project);
	} else {
		fHRefs= new String[0];
	}
}
 
Example 18
Source File: SelectionAwareSourceRangeComputer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private void initializeRanges() throws CoreException {
	fRanges = new HashMap<>();
	if (fSelectedNodes.length == 0) {
		return;
	}

	fRanges.put(fSelectedNodes[0], super.computeSourceRange(fSelectedNodes[0]));
	int last = fSelectedNodes.length - 1;
	fRanges.put(fSelectedNodes[last], super.computeSourceRange(fSelectedNodes[last]));

	IJavaProject javaProject = ((CompilationUnit) fSelectedNodes[0].getRoot()).getTypeRoot().getJavaProject();
	String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
	String complianceLevel = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
	IScanner scanner = ToolFactory.createScanner(true, false, false, sourceLevel, complianceLevel);
	char[] source = fDocumentPortionToScan.toCharArray();
	scanner.setSource(source);
	fDocumentPortionToScan = null; // initializeRanges() is only called once

	TokenScanner tokenizer = new TokenScanner(scanner);
	int pos = tokenizer.getNextStartOffset(0, false);

	ASTNode currentNode = fSelectedNodes[0];
	int newStart = Math.min(fSelectionStart + pos, currentNode.getStartPosition());
	SourceRange range = fRanges.get(currentNode);
	fRanges.put(currentNode, new SourceRange(newStart, range.getLength() + range.getStartPosition() - newStart));

	currentNode = fSelectedNodes[last];
	int scannerStart = currentNode.getStartPosition() + currentNode.getLength() - fSelectionStart;
	tokenizer.setOffset(scannerStart);
	pos = scannerStart;
	int token = -1;
	try {
		while (true) {
			token = tokenizer.readNext(false);
			pos = tokenizer.getCurrentEndOffset();
		}
	} catch (CoreException e) {
	}
	if (token == ITerminalSymbols.TokenNameCOMMENT_LINE) {
		int index = pos - 1;
		while (index >= 0 && IndentManipulation.isLineDelimiterChar(source[index])) {
			pos--;
			index--;
		}
	}

	int newEnd = Math.max(fSelectionStart + pos, currentNode.getStartPosition() + currentNode.getLength());
	range = fRanges.get(currentNode);
	fRanges.put(currentNode, new SourceRange(range.getStartPosition(), newEnd - range.getStartPosition()));

	// The extended source range of the last child node can end after the selection.
	// We have to ensure that the source range of such child nodes is also capped by the selection range.
	// Example: (/*]*/TRANSFORMER::transform/*[*/)
	// Selection is between /*]*/ and /*[*/, but the extended range of the "transform" node actually includes /*[*/ as well.
	while (true) {
		List<ASTNode> children = ASTNodes.getChildren(currentNode);
		if (!children.isEmpty()) {
			ASTNode lastChild = children.get(children.size() - 1);
			SourceRange extRange = super.computeSourceRange(lastChild);
			if (extRange.getStartPosition() + extRange.getLength() > newEnd) {
				fRanges.put(lastChild, new SourceRange(extRange.getStartPosition(), newEnd - extRange.getStartPosition()));
				currentNode = lastChild;
				continue;
			}
		}
		break;
	}
}
 
Example 19
Source File: RenamingNameSuggestor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private String[] readCommaSeparatedPreference(IJavaProject project, String option) {
	String list= project.getOption(option, true);
	return list == null ? new String[0] : list.split(","); //$NON-NLS-1$
}
 
Example 20
Source File: JavaAutoIndentStrategy.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Returns the possibly <code>project</code>-specific core preference defined under
 * <code>key</code>.
 *
 * @param project the project to get the preference from, or <code>null</code> to get the global
 *            preference
 * @param key the key of the preference
 * @return the value of the preference
 * @since 3.5
 */
private static String getCoreOption(IJavaProject project, String key) {
	if (project == null)
		return JavaCore.getOption(key);
	return project.getOption(key, true);
}