Search in sources :

Example 26 with ASTParser

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

the class TypeContextChecker method parseType.

private static Type parseType(String typeString, IJavaProject javaProject, List<String> problemsCollector) {
    if (//speed up for a common case //$NON-NLS-1$
    "".equals(typeString.trim()))
        return null;
    if (!typeString.trim().equals(typeString))
        return null;
    StringBuffer cuBuff = new StringBuffer();
    //$NON-NLS-1$
    cuBuff.append("interface A{");
    int offset = cuBuff.length();
    //$NON-NLS-1$
    cuBuff.append(typeString).append(" m();}");
    ASTParser p = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    p.setSource(cuBuff.toString().toCharArray());
    p.setProject(javaProject);
    CompilationUnit cu = (CompilationUnit) p.createAST(null);
    Selection selection = Selection.createFromStartLength(offset, typeString.length());
    SelectionAnalyzer analyzer = new SelectionAnalyzer(selection, false);
    cu.accept(analyzer);
    ASTNode selected = analyzer.getFirstSelectedNode();
    if (!(selected instanceof Type))
        return null;
    Type type = (Type) selected;
    if (MethodTypesSyntaxChecker.isVoidArrayType(type))
        return null;
    IProblem[] problems = ASTNodes.getProblems(type, ASTNodes.NODE_ONLY, ASTNodes.PROBLEMS);
    if (problems.length > 0) {
        for (int i = 0; i < problems.length; i++) problemsCollector.add(problems[i].getMessage());
    }
    String typeNodeRange = cuBuff.substring(type.getStartPosition(), ASTNodes.getExclusiveEnd(type));
    if (typeString.equals(typeNodeRange))
        return type;
    else
        return null;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SelectionAnalyzer(org.eclipse.jdt.internal.corext.dom.SelectionAnalyzer) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) IType(org.eclipse.jdt.core.IType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) Type(org.eclipse.jdt.core.dom.Type) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) Selection(org.eclipse.jdt.internal.corext.dom.Selection) ASTNode(org.eclipse.jdt.core.dom.ASTNode) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) IProblem(org.eclipse.jdt.core.compiler.IProblem)

Example 27 with ASTParser

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

the class InlineConstantRefactoring method initialize.

private RefactoringStatus initialize(JavaRefactoringArguments arguments) {
    final String selection = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION);
    if (selection != null) {
        int offset = -1;
        int length = -1;
        final StringTokenizer tokenizer = new StringTokenizer(selection);
        if (tokenizer.hasMoreTokens())
            offset = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (tokenizer.hasMoreTokens())
            length = Integer.valueOf(tokenizer.nextToken()).intValue();
        if (offset >= 0 && length >= 0) {
            fSelectionStart = offset;
            fSelectionLength = length;
        } else
            return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { selection, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION }));
    }
    final String handle = arguments.getAttribute(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT);
    if (handle != null) {
        final IJavaElement element = JavaRefactoringDescriptorUtil.handleToElement(arguments.getProject(), handle, false);
        if (element == null || !element.exists())
            return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
        else {
            if (element instanceof ICompilationUnit) {
                fSelectionCu = (ICompilationUnit) element;
                if (selection == null)
                    return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION));
            } else if (element instanceof IField) {
                final IField field = (IField) element;
                try {
                    final ISourceRange range = field.getNameRange();
                    if (range != null) {
                        fSelectionStart = range.getOffset();
                        fSelectionLength = range.getLength();
                    } else
                        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, IJavaRefactorings.INLINE_CONSTANT));
                } catch (JavaModelException exception) {
                    return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
                }
                fSelectionCu = field.getCompilationUnit();
            } else
                return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_illegal_argument, new Object[] { handle, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT }));
            final ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
            parser.setResolveBindings(true);
            parser.setSource(fSelectionCu);
            final CompilationUnit unit = (CompilationUnit) parser.createAST(null);
            initialize(fSelectionCu, unit);
            if (checkStaticFinalConstantNameSelected().hasFatalError())
                return JavaRefactoringDescriptorUtil.createInputFatalStatus(element, getName(), IJavaRefactorings.INLINE_CONSTANT);
        }
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT));
    final String replace = arguments.getAttribute(ATTRIBUTE_REPLACE);
    if (replace != null) {
        fReplaceAllReferences = Boolean.valueOf(replace).booleanValue();
    } else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REPLACE));
    final String remove = arguments.getAttribute(ATTRIBUTE_REMOVE);
    if (remove != null)
        fRemoveDeclaration = Boolean.valueOf(remove).booleanValue();
    else
        return RefactoringStatus.createFatalErrorStatus(Messages.format(RefactoringCoreMessages.InitializableRefactoring_argument_not_exist, ATTRIBUTE_REMOVE));
    return new RefactoringStatus();
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) StringTokenizer(java.util.StringTokenizer) JavaModelException(org.eclipse.jdt.core.JavaModelException) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IField(org.eclipse.jdt.core.IField) ASTParser(org.eclipse.jdt.core.dom.ASTParser) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 28 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.

the class CompletionProposalReplacementProvider method getExpectedTypeForGenericParameters.

private ITypeBinding getExpectedTypeForGenericParameters() {
    char[][] chKeys = context.getExpectedTypesKeys();
    if (chKeys == null || chKeys.length == 0)
        return null;
    String[] keys = new String[chKeys.length];
    for (int i = 0; i < keys.length; i++) {
        keys[i] = String.valueOf(chKeys[0]);
    }
    final ASTParser parser = ASTParser.newParser(AST.JLS8);
    parser.setProject(compilationUnit.getJavaProject());
    parser.setResolveBindings(true);
    parser.setStatementsRecovery(true);
    final Map<String, IBinding> bindings = new HashMap<String, IBinding>();
    ASTRequestor requestor = new ASTRequestor() {

        @Override
        public void acceptBinding(String bindingKey, IBinding binding) {
            bindings.put(bindingKey, binding);
        }
    };
    parser.createASTs(new ICompilationUnit[0], keys, requestor, null);
    if (bindings.size() > 0)
        return (ITypeBinding) bindings.get(keys[0]);
    return null;
}
Also used : HashMap(java.util.HashMap) IBinding(org.eclipse.jdt.core.dom.IBinding) ASTRequestor(org.eclipse.jdt.core.dom.ASTRequestor) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 29 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.

the class ASTResolving method createQuickFixAST.

public static CompilationUnit createQuickFixAST(ICompilationUnit compilationUnit, IProgressMonitor monitor) {
    ASTParser astParser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    astParser.setSource(compilationUnit);
    astParser.setResolveBindings(true);
    astParser.setStatementsRecovery(ASTProvider.SHARED_AST_STATEMENT_RECOVERY);
    astParser.setBindingsRecovery(ASTProvider.SHARED_BINDING_RECOVERY);
    return (CompilationUnit) astParser.createAST(monitor);
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 30 with ASTParser

use of org.eclipse.jdt.core.dom.ASTParser in project flux by eclipse.

the class ASTNodeFactory method newType.

public static Type newType(AST ast, String content) {
    StringBuffer buffer = new StringBuffer(TYPE_HEADER);
    buffer.append(content);
    buffer.append(TYPE_FOOTER);
    ASTParser p = ASTParser.newParser(ast.apiLevel());
    p.setSource(buffer.toString().toCharArray());
    CompilationUnit root = (CompilationUnit) p.createAST(null);
    List<AbstractTypeDeclaration> list = root.types();
    TypeDeclaration typeDecl = (TypeDeclaration) list.get(0);
    MethodDeclaration methodDecl = typeDecl.getMethods()[0];
    ASTNode type = methodDecl.getReturnType2();
    ASTNode result = ASTNode.copySubtree(ast, type);
    result.accept(new PositionClearer());
    return (Type) result;
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) Type(org.eclipse.jdt.core.dom.Type) UnionType(org.eclipse.jdt.core.dom.UnionType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ASTParser(org.eclipse.jdt.core.dom.ASTParser) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration) AbstractTypeDeclaration(org.eclipse.jdt.core.dom.AbstractTypeDeclaration)

Aggregations

ASTParser (org.eclipse.jdt.core.dom.ASTParser)42 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)34 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)24 ASTNode (org.eclipse.jdt.core.dom.ASTNode)11 RefactoringASTParser (org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser)9 IBinding (org.eclipse.jdt.core.dom.IBinding)7 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)5 IType (org.eclipse.jdt.core.IType)5 IProblem (org.eclipse.jdt.core.compiler.IProblem)5 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)5 ASTRequestor (org.eclipse.jdt.core.dom.ASTRequestor)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)3 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ISourceRange (org.eclipse.jdt.core.ISourceRange)3 ArrayType (org.eclipse.jdt.core.dom.ArrayType)3 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)3 Type (org.eclipse.jdt.core.dom.Type)3 Map (java.util.Map)2