Search in sources :

Example 1 with ASTRequestor

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

the class InferTypeArgumentsRefactoring method checkFinalConditions.

/*
	 * @see org.eclipse.ltk.core.refactoring.Refactoring#checkFinalConditions(org.eclipse.core.runtime.IProgressMonitor)
	 */
@Override
public RefactoringStatus checkFinalConditions(final IProgressMonitor pm) throws CoreException, OperationCanceledException {
    HashMap<IJavaProject, ArrayList<IJavaElement>> projectsToElements = getJavaElementsPerProject(fElements);
    //$NON-NLS-1$
    pm.beginTask("", projectsToElements.size() + 2);
    final RefactoringStatus result = new RefactoringStatus();
    try {
        fTCModel = new InferTypeArgumentsTCModel();
        final InferTypeArgumentsConstraintCreator unitCollector = new InferTypeArgumentsConstraintCreator(fTCModel, fAssumeCloneReturnsSameType);
        for (Iterator<Entry<IJavaProject, ArrayList<IJavaElement>>> iter = projectsToElements.entrySet().iterator(); iter.hasNext(); ) {
            Entry<IJavaProject, ArrayList<IJavaElement>> entry = iter.next();
            IJavaProject project = entry.getKey();
            ArrayList<IJavaElement> javaElementsList = entry.getValue();
            IJavaElement[] javaElements = javaElementsList.toArray(new IJavaElement[javaElementsList.size()]);
            List<ICompilationUnit> cus = Arrays.asList(JavaModelUtil.getAllCompilationUnits(javaElements));
            int batchSize = 150;
            int batches = ((cus.size() - 1) / batchSize) + 1;
            SubProgressMonitor projectMonitor = new SubProgressMonitor(pm, 1);
            //$NON-NLS-1$
            projectMonitor.beginTask("", batches);
            projectMonitor.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_building);
            for (int i = 0; i < batches; i++) {
                List<ICompilationUnit> batch = cus.subList(i * batchSize, Math.min(cus.size(), (i + 1) * batchSize));
                ICompilationUnit[] batchCus = batch.toArray(new ICompilationUnit[batch.size()]);
                final SubProgressMonitor batchMonitor = new SubProgressMonitor(projectMonitor, 1);
                batchMonitor.subTask(RefactoringCoreMessages.InferTypeArgumentsRefactoring_calculating_dependencies);
                ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
                parser.setProject(project);
                parser.setCompilerOptions(RefactoringASTParser.getCompilerOptions(project));
                parser.setResolveBindings(true);
                parser.createASTs(batchCus, new String[0], new ASTRequestor() {

                    @Override
                    public void acceptAST(final ICompilationUnit source, final CompilationUnit ast) {
                        batchMonitor.subTask(BasicElementLabels.getFileName(source));
                        SafeRunner.run(new ISafeRunnable() {

                            public void run() throws Exception {
                                IProblem[] problems = ast.getProblems();
                                for (int p = 0; p < problems.length; p++) {
                                    if (problems[p].isError()) {
                                        String cuName = JavaElementLabels.getElementLabel(source, JavaElementLabels.CU_QUALIFIED);
                                        String msg = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_error_in_cu_skipped, new Object[] { cuName });
                                        result.addError(msg, JavaStatusContext.create(source, SourceRangeFactory.create(problems[p])));
                                        return;
                                    }
                                }
                                ast.accept(unitCollector);
                            }

                            public void handleException(Throwable exception) {
                                String cuName = JavaElementLabels.getElementLabel(source, JavaElementLabels.CU_QUALIFIED);
                                String msg = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_internal_error, new Object[] { cuName });
                                JavaPlugin.log(new Status(IStatus.ERROR, JavaPlugin.getPluginId(), IJavaStatusConstants.INTERNAL_ERROR, msg, null));
                                String msg2 = Messages.format(RefactoringCoreMessages.InferTypeArgumentsRefactoring_error_skipped, new Object[] { cuName });
                                result.addError(msg2, JavaStatusContext.create(source));
                            }
                        });
                        fTCModel.newCu();
                    }

                    @Override
                    public void acceptBinding(String bindingKey, IBinding binding) {
                    //do nothing
                    }
                }, batchMonitor);
            }
            projectMonitor.done();
            fTCModel.newCu();
        }
        //			Display.getDefault().syncExec(new Runnable() {
        //				public void run() {
        //					MessageDialog.openInformation(Display.getCurrent().getActiveShell(), "Debugging...", "after constraint gen");
        //				}
        //			});
        pm.setTaskName(RefactoringCoreMessages.InferTypeArgumentsRefactoring_solving);
        InferTypeArgumentsConstraintsSolver solver = new InferTypeArgumentsConstraintsSolver(fTCModel);
        InferTypeArgumentsUpdate updates = solver.solveConstraints(new SubProgressMonitor(pm, 1));
        //free caches
        solver = null;
        fChangeManager = new TextChangeManager();
        rewriteDeclarations(updates, new SubProgressMonitor(pm, 1));
        IFile[] filesToModify = ResourceUtil.getFiles(fChangeManager.getAllCompilationUnits());
        result.merge(Checks.validateModifiesFiles(filesToModify, getValidationContext()));
        return result;
    } finally {
        pm.done();
        clearGlobalState();
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) Entry(java.util.Map.Entry) ASTRequestor(org.eclipse.jdt.core.dom.ASTRequestor) ISafeRunnable(org.eclipse.core.runtime.ISafeRunnable) RefactoringASTParser(org.eclipse.jdt.internal.corext.refactoring.util.RefactoringASTParser) ASTParser(org.eclipse.jdt.core.dom.ASTParser) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) IStatus(org.eclipse.core.runtime.IStatus) Status(org.eclipse.core.runtime.Status) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) IJavaElement(org.eclipse.jdt.core.IJavaElement) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IProblem(org.eclipse.jdt.core.compiler.IProblem) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IJavaProject(org.eclipse.jdt.core.IJavaProject) TextChangeManager(org.eclipse.jdt.internal.corext.refactoring.util.TextChangeManager)

Example 2 with ASTRequestor

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

the class LazyGenericTypeProposal method getExpectedType.

/**
	 * Returns the type binding of the expected type as it is contained in the
	 * code completion context.
	 *
	 * @return the binding of the expected type
	 */
private ITypeBinding getExpectedType() {
    char[][] chKeys = fInvocationContext.getCoreContext().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 CheASTParser parser = CheASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    parser.setProject(fCompilationUnit.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) CheASTParser(org.eclipse.jdt.core.dom.CheASTParser) Point(org.eclipse.swt.graphics.Point)

Example 3 with ASTRequestor

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

the class TypeEnvironment method createTypeBindings.

public static ITypeBinding[] createTypeBindings(TType[] types, IJavaProject project) {
    final Map<String, Object> mapping = new HashMap<String, Object>();
    List<String> keys = new ArrayList<String>();
    for (int i = 0; i < types.length; i++) {
        TType type = types[i];
        String bindingKey = type.getBindingKey();
        mapping.put(bindingKey, type);
        keys.add(bindingKey);
    }
    ASTParser parser = ASTParser.newParser(ASTProvider.SHARED_AST_LEVEL);
    parser.setProject(project);
    parser.setResolveBindings(true);
    parser.createASTs(new ICompilationUnit[0], keys.toArray(new String[keys.size()]), new ASTRequestor() {

        @Override
        public void acceptBinding(String bindingKey, IBinding binding) {
            mapping.put(bindingKey, binding);
        }
    }, null);
    ITypeBinding[] result = new ITypeBinding[types.length];
    for (int i = 0; i < types.length; i++) {
        TType type = types[i];
        String bindingKey = type.getBindingKey();
        Object value = mapping.get(bindingKey);
        if (value instanceof ITypeBinding) {
            result[i] = (ITypeBinding) value;
        }
    }
    return result;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IBinding(org.eclipse.jdt.core.dom.IBinding) ArrayList(java.util.ArrayList) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRequestor(org.eclipse.jdt.core.dom.ASTRequestor) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Example 4 with ASTRequestor

use of org.eclipse.jdt.core.dom.ASTRequestor in project XobotOS by xamarin.

the class XobotBuilder method stage1_parse.

private List<CompilationUnitPair> stage1_parse(final List<ICompilationUnit> sources, IProgressMonitor monitor) {
    final int totalWork = PARSING_PRICE * sources.size();
    final IProgressMonitor subMonitor = new SubProgressMonitor(monitor, totalWork);
    final ArrayList<CompilationUnitPair> pairs = new ArrayList<CompilationUnitPair>(sources.size());
    ASTRequestor requestor = new ASTRequestor() {

        @Override
        public void acceptAST(ICompilationUnit source, CompilationUnit ast) {
            pairs.add(new CompilationUnitPair(source, ast));
            subMonitor.subTask(String.format("Parsing (%d/%d): %s", pairs.size(), sources.size(), getUnitName(source)));
            subMonitor.worked(PARSING_PRICE);
        }
    };
    final ASTParser _parser = ASTParser.newParser(AST.JLS3);
    _parser.setKind(ASTParser.K_COMPILATION_UNIT);
    _parser.setProject(sources.get(0).getJavaProject());
    _parser.setResolveBindings(true);
    final ICompilationUnit[] sourceArray = sources.toArray(new ICompilationUnit[0]);
    Sharpen.Log(Level.INFO, "Parsing %d compilation units.", sources.size());
    try {
        subMonitor.beginTask("parsing compile units", totalWork);
        _parser.createASTs(sourceArray, new String[0], requestor, null);
    } finally {
        subMonitor.done();
    }
    return pairs;
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) CSCompilationUnit(sharpen.core.csharp.ast.CSCompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) CompilationUnitPair(sharpen.core.framework.CompilationUnitPair) ASTRequestor(org.eclipse.jdt.core.dom.ASTRequestor) ASTParser(org.eclipse.jdt.core.dom.ASTParser) SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor)

Example 5 with ASTRequestor

use of org.eclipse.jdt.core.dom.ASTRequestor in project eclipse.jdt.ls 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(IASTSharedValues.SHARED_AST_LEVEL);
    parser.setProject(compilationUnit.getJavaProject());
    parser.setResolveBindings(true);
    parser.setStatementsRecovery(true);
    final Map<String, IBinding> bindings = new HashMap<>();
    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) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTRequestor(org.eclipse.jdt.core.dom.ASTRequestor) ASTParser(org.eclipse.jdt.core.dom.ASTParser)

Aggregations

ASTRequestor (org.eclipse.jdt.core.dom.ASTRequestor)6 ASTParser (org.eclipse.jdt.core.dom.ASTParser)5 IBinding (org.eclipse.jdt.core.dom.IBinding)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)2 SubProgressMonitor (org.eclipse.core.runtime.SubProgressMonitor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)2 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 LinkedHashMap (java.util.LinkedHashMap)1 Entry (java.util.Map.Entry)1 IFile (org.eclipse.core.resources.IFile)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 ISafeRunnable (org.eclipse.core.runtime.ISafeRunnable)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1 IJavaElement (org.eclipse.jdt.core.IJavaElement)1 IJavaProject (org.eclipse.jdt.core.IJavaProject)1 IProblem (org.eclipse.jdt.core.compiler.IProblem)1 CheASTParser (org.eclipse.jdt.core.dom.CheASTParser)1