Search in sources :

Example 76 with IJavaProject

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;
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IDocument(org.eclipse.jface.text.IDocument) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 77 with IJavaProject

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;
    }
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) IField(org.eclipse.jdt.core.IField) IType(org.eclipse.jdt.core.IType)

Example 78 with IJavaProject

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;
}
Also used : TemplateVariableType(org.eclipse.jface.text.templates.TemplateVariableType) IJavaProject(org.eclipse.jdt.core.IJavaProject) TemplateException(org.eclipse.jface.text.templates.TemplateException) MultiVariable(org.eclipse.jdt.internal.ui.text.template.contentassist.MultiVariable) TemplateBuffer(org.eclipse.jface.text.templates.TemplateBuffer) TemplateTranslator(org.eclipse.jface.text.templates.TemplateTranslator)

Example 79 with IJavaProject

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.
    }
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) IJavaElement(org.eclipse.jdt.core.IJavaElement) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IResource(org.eclipse.core.resources.IResource)

Example 80 with IJavaProject

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);
    }
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) Hashtable(java.util.Hashtable) IPackageFragmentRoot(org.eclipse.jdt.core.IPackageFragmentRoot) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

IJavaProject (org.eclipse.jdt.core.IJavaProject)743 IProject (org.eclipse.core.resources.IProject)178 IFile (org.eclipse.core.resources.IFile)149 Test (org.junit.Test)146 CoreException (org.eclipse.core.runtime.CoreException)127 JavaModelException (org.eclipse.jdt.core.JavaModelException)124 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)123 IPath (org.eclipse.core.runtime.IPath)110 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)103 ArrayList (java.util.ArrayList)87 IFolder (org.eclipse.core.resources.IFolder)78 IResource (org.eclipse.core.resources.IResource)75 IJavaElement (org.eclipse.jdt.core.IJavaElement)63 IType (org.eclipse.jdt.core.IType)63 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)62 Path (org.eclipse.core.runtime.Path)57 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)56 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)47 StringInputStream (org.eclipse.xtext.util.StringInputStream)39 HashMap (java.util.HashMap)38