Search in sources :

Example 26 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit 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 27 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class JavaTextSelection method resolveSelectedNodes.

public ASTNode[] resolveSelectedNodes() {
    if (fNodesRequested)
        return fSelectedNodes;
    fNodesRequested = true;
    CompilationUnit root = resolvePartialAstAtOffset();
    if (root == null)
        return null;
    Selection ds = Selection.createFromStartLength(getOffset(), getLength());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(ds, false);
    root.accept(analyzer);
    fSelectedNodes = analyzer.getSelectedNodes();
    fCoveringNode = analyzer.getLastCoveringNode();
    return fSelectedNodes;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) Selection(org.eclipse.jdt.internal.corext.dom.Selection) TextSelection(org.eclipse.che.jface.text.TextSelection)

Example 28 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class ControlStatementsCleanUp method createFix.

/**
	 * {@inheritDoc}
	 */
@Override
public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
    CompilationUnit compilationUnit = context.getAST();
    if (compilationUnit == null)
        return null;
    boolean useBlocks = isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
    if (!useBlocks)
        return null;
    return ControlStatementsFix.createCleanUp(compilationUnit, isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS), isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER), isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit)

Example 29 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class ExpressionsCleanUp method createFix.

/**
	 * {@inheritDoc}
	 */
@Override
public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
    CompilationUnit compilationUnit = context.getAST();
    if (compilationUnit == null)
        return null;
    boolean usePrentheses = isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
    if (!usePrentheses)
        return null;
    return ExpressionsFix.createCleanUp(compilationUnit, isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS), isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER));
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit)

Example 30 with CompilationUnit

use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.

the class ASTProvider method getAST.

/**
     * Returns a shared compilation unit AST for the given Java element.
     * <p>
     * Clients are not allowed to modify the AST and must synchronize all access to its nodes.
     * </p>
     *
     * @param input the Java element, must not be <code>null</code>
     * @param waitFlag {@link SharedASTProvider#WAIT_YES}, {@link SharedASTProvider#WAIT_NO} or
     *            {@link SharedASTProvider#WAIT_ACTIVE_ONLY}
     * @param progressMonitor the progress monitor or <code>null</code>
     * @return the AST or <code>null</code> if the AST is not available
     */
public CompilationUnit getAST(final ITypeRoot input, SharedASTProvider.WAIT_FLAG waitFlag, IProgressMonitor progressMonitor) {
    if (input == null || waitFlag == null)
        //$NON-NLS-1$
        throw new IllegalArgumentException("input or wait flag are null");
    if (progressMonitor != null && progressMonitor.isCanceled())
        return null;
    boolean isActiveElement;
    synchronized (this) {
        //input.equals(fActiveJavaElement);
        isActiveElement = false;
        if (isActiveElement) {
            if (fAST != null) {
                if (DEBUG)
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "returning cached AST:" + toString(fAST) + " for: " + input.getElementName());
                return fAST;
            }
            if (waitFlag == SharedASTProvider.WAIT_NO) {
                if (DEBUG)
                    //$NON-NLS-1$ //$NON-NLS-2$
                    System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "returning null (WAIT_NO) for: " + input.getElementName());
                return null;
            }
        }
    }
    final boolean canReturnNull = waitFlag == SharedASTProvider.WAIT_NO || (waitFlag == SharedASTProvider.WAIT_ACTIVE_ONLY && !(isActiveElement && fAST == null));
    boolean isReconciling = false;
    final ITypeRoot activeElement;
    if (isActiveElement) {
    //            synchronized (fReconcileLock) {
    //                activeElement= fReconcilingJavaElement;
    //                isReconciling= isReconciling(input);
    //                if (!isReconciling && !canReturnNull)
    //                    aboutToBeReconciled(input);
    //            }
    } else
        activeElement = null;
    if (isReconciling) {
        //                }
        return getAST(input, waitFlag, progressMonitor);
    }
    /*else if (canReturnNull)
            return null;*/
    CompilationUnit ast = null;
    try {
        ast = createAST(input, progressMonitor);
        if (progressMonitor != null && progressMonitor.isCanceled()) {
            ast = null;
            if (DEBUG)
                //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "Ignore created AST for: " + input.getElementName() + " - operation has been cancelled");
        }
    } finally {
        if (isActiveElement) {
            if (fAST != null) {
                // in the meantime, reconcile created a new AST. Return that one
                if (DEBUG)
                    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
                    System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "Ignore created AST for " + input.getElementName() + " - AST from reconciler is newer");
                reconciled(fAST, input, null);
                return fAST;
            } else
                reconciled(ast, input, null);
        }
    }
    return ast;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ITypeRoot(org.eclipse.jdt.core.ITypeRoot)

Aggregations

CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)610 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)555 ArrayList (java.util.ArrayList)450 Test (org.junit.Test)438 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)435 CUCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.CUCorrectionProposal)228 Hashtable (java.util.Hashtable)137 ASTNode (org.eclipse.jdt.core.dom.ASTNode)80 SimpleName (org.eclipse.jdt.core.dom.SimpleName)36 ASTParser (org.eclipse.jdt.core.dom.ASTParser)35 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)35 ASTRewriteCorrectionProposal (org.eclipse.jdt.ui.text.java.correction.ASTRewriteCorrectionProposal)35 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)34 Ignore (org.junit.Ignore)34 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)28 Type (org.eclipse.jdt.core.dom.Type)26 AST (org.eclipse.jdt.core.dom.AST)21 Expression (org.eclipse.jdt.core.dom.Expression)20 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)19 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)19