use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.
the class JavaContext method getIndentation.
/**
* Returns the indentation level at the position of code completion.
*
* @return the indentation level at the position of the code completion
*/
private int getIndentation() {
int start = getStart();
IDocument document = getDocument();
try {
IRegion region = document.getLineInformationOfOffset(start);
String lineContent = document.get(region.getOffset(), region.getLength());
IJavaProject project = getJavaProject();
return Strings.computeIndentUnits(lineContent, project);
} catch (BadLocationException e) {
return 0;
}
}
use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.
the class JavaContext method addStaticImport.
/**
* Adds a static import for the member with name <code>qualifiedMemberName</code>. The member is
* either a static field or a static method or a '*' to import all static members of a type.
*
* @param qualifiedMemberName the fully qualified name of the member to import or a qualified type
* name plus a '.*' suffix.
* @return returns either the simple member name if the import was successful or else the qualified name.
* @since 3.4
*/
public String addStaticImport(String qualifiedMemberName) {
if (isReadOnly())
return qualifiedMemberName;
ICompilationUnit cu = getCompilationUnit();
if (cu == null)
return qualifiedMemberName;
int memberOffset = qualifiedMemberName.lastIndexOf('.');
if (memberOffset == -1)
return qualifiedMemberName;
String typeName = qualifiedMemberName.substring(0, memberOffset);
String memberName = qualifiedMemberName.substring(memberOffset + 1, qualifiedMemberName.length());
try {
boolean isField;
if ("*".equals(memberName)) {
//$NON-NLS-1$
isField = true;
} else {
IJavaProject javaProject = cu.getJavaProject();
IType type = javaProject.findType(typeName);
if (type == null)
return qualifiedMemberName;
IField field = type.getField(memberName);
if (field.exists()) {
isField = true;
} else if (hasMethod(type, memberName)) {
isField = false;
} else {
return qualifiedMemberName;
}
}
CompilationUnit root = getASTRoot(cu);
if (fImportRewrite == null) {
if (root == null) {
fImportRewrite = StubUtility.createImportRewrite(cu, true);
} else {
fImportRewrite = StubUtility.createImportRewrite(root, true);
}
}
ImportRewriteContext context;
if (root == null)
context = null;
else
context = new ContextSensitiveImportRewriteContext(root, getCompletionOffset(), fImportRewrite);
return fImportRewrite.addStaticImport(typeName, memberName, isField, context);
} catch (JavaModelException e) {
handleException(null, e);
return typeName;
}
}
use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.
the class JavaContext method evaluate.
/*
* @see TemplateContext#evaluate(Template template)
*/
@Override
public TemplateBuffer evaluate(Template template) throws BadLocationException, TemplateException {
clear();
if (!canEvaluate(template))
throw new TemplateException(JavaTemplateMessages.Context_error_cannot_evaluate);
TemplateTranslator translator = new TemplateTranslator() {
@Override
protected TemplateVariable createVariable(TemplateVariableType type, String name, int[] offsets) {
// TemplateVariableResolver resolver= getContextType().getResolver(type.getName());
// return resolver.createVariable();
MultiVariable variable = new JavaVariable(type, name, offsets);
fVariables.put(name, variable);
return variable;
}
};
TemplateBuffer buffer = translator.translate(template);
getContextType().resolve(buffer, this);
rewriteImports();
// IPreferenceStore prefs = JavaPlugin.getDefault().getPreferenceStore();
//prefs.getBoolean(PreferenceConstants.TEMPLATES_USE_CODEFORMATTER);
boolean useCodeFormatter = true;
IJavaProject project = getJavaProject();
JavaFormatter formatter = new JavaFormatter(TextUtilities.getDefaultLineDelimiter(getDocument()), getIndentation(), useCodeFormatter, project);
formatter.format(buffer, this);
clear();
return buffer;
}
use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.
the class JavaSearchScopeFactory method addJavaElements.
private void addJavaElements(Set<IJavaElement> javaElements, IWorkingSet workingSet) {
if (workingSet == null)
return;
if (workingSet.isAggregateWorkingSet() && workingSet.isEmpty()) {
try {
IJavaProject[] projects = JavaCore.create(ResourcesPlugin.getWorkspace().getRoot()).getJavaProjects();
javaElements.addAll(Arrays.asList(projects));
} catch (JavaModelException e) {
JavaPlugin.log(e);
}
return;
}
IAdaptable[] elements = workingSet.getElements();
for (int i = 0; i < elements.length; i++) {
IJavaElement javaElement = (IJavaElement) elements[i].getAdapter(IJavaElement.class);
if (javaElement != null) {
addJavaElements(javaElements, javaElement);
continue;
}
IResource resource = (IResource) elements[i].getAdapter(IResource.class);
if (resource != null) {
addJavaElements(javaElements, resource);
}
// else we don't know what to do with it, ignore.
}
}
use of org.eclipse.jdt.core.IJavaProject in project che by eclipse.
the class RenamePackageTest method testImportFromMultiRoots4.
@Test
@Ignore
public void testImportFromMultiRoots4() throws Exception {
//circular buildpath references
IJavaProject projectPrg = null;
IJavaProject projectTest = null;
Hashtable options = JavaCore.getOptions();
Object cyclicPref = JavaCore.getOption(JavaCore.CORE_CIRCULAR_CLASSPATH);
try {
projectPrg = JavaProjectHelper.createJavaProject("RenamePack1", "bin");
assertNotNull(JavaProjectHelper.addRTJar(projectPrg));
IPackageFragmentRoot srcPrg = JavaProjectHelper.addSourceContainer(projectPrg, "srcPrg");
projectTest = JavaProjectHelper.createJavaProject("RenamePack2", "bin");
assertNotNull(JavaProjectHelper.addRTJar(projectTest));
IPackageFragmentRoot srcTest = JavaProjectHelper.addSourceContainer(projectTest, "srcTest");
options.put(JavaCore.CORE_CIRCULAR_CLASSPATH, JavaCore.WARNING);
JavaCore.setOptions(options);
JavaProjectHelper.addRequiredProject(projectTest, projectPrg);
JavaProjectHelper.addRequiredProject(projectPrg, projectTest);
helperMultiProjects(new IPackageFragmentRoot[] { srcPrg, srcTest }, new String[][] { new String[] { "p" }, new String[] { "p" } }, "a.b.c", new String[][][] { new String[][] { new String[] { "A", "B" } }, new String[][] { new String[] { "ATest" } } });
} finally {
options.put(JavaCore.CORE_CIRCULAR_CLASSPATH, cyclicPref);
JavaCore.setOptions(options);
JavaProjectHelper.delete(projectPrg);
JavaProjectHelper.delete(projectTest);
}
}
Aggregations