/** * Sets the source code to be parsed. * * <p>This method automatically sets the project (and compiler * options) based on the given compilation unit of class file, in a manner * equivalent to {@link #setProject(IJavaProject) setProject(source.getJavaProject())}.</p> * <p>If the source is a class file without source attachment, the creation of the * ast will fail with an {@link IllegalStateException}.</p> * * <p>This source is not used when the AST is built using * {@link #createASTs(ICompilationUnit[], String[], ASTRequestor, IProgressMonitor)}.</p> * * @param source the Java model compilation unit or class file whose corresponding source code * is to be parsed, or <code>null</code> if none * @since 3.3 */ public void setSource(ITypeRoot source) { this.typeRoot = source; // clear the raw source this.rawSource = null; if (source != null) { this.project = source.getJavaProject(); Map<String, String> options = this.project.getOptions(true); options.remove(JavaCore.COMPILER_TASK_TAGS); // no need to parse task tags this.compilerOptions = options; } }
public TextEdit getDeleteEdit() { final ASTRewrite rewriter= ASTRewrite.create(fDeclaration.getAST()); rewriter.remove(fDeclaration, null); return rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true)); }
public TextEdit getDeleteEdit() { final ASTRewrite rewriter= ASTRewrite.create(fDeclaration.getAST()); rewriter.remove(fDeclaration, null); return rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true)); }
private String[] getBlocks(RangeMarker[] markers) throws BadLocationException { String[] result= new String[markers.length]; for (int i= 0; i < markers.length; i++) { RangeMarker marker= markers[i]; String content= fDocument.get(marker.getOffset(), marker.getLength()); String lines[]= Strings.convertIntoLines(content); Strings.trimIndentation(lines, fTypeRoot.getJavaProject(), false); if (fMarkerMode == STATEMENT_MODE && lines.length == 2 && isSingleControlStatementWithoutBlock()) { lines[1]= CodeFormatterUtil.createIndentString(1, fTypeRoot.getJavaProject()) + lines[1]; } result[i]= Strings.concatenate(lines, TextUtilities.getDefaultLineDelimiter(fDocument)); } return result; }
public TextEdit getDeleteEdit() { final ASTRewrite rewriter= ASTRewrite.create(fDeclaration.getAST()); rewriter.remove(fDeclaration, null); return rewriter.rewriteAST(fDocument, fTypeRoot.getJavaProject().getOptions(true)); }
private String[] getBlocks(RangeMarker[] markers) throws BadLocationException { String[] result= new String[markers.length]; for (int i= 0; i < markers.length; i++) { RangeMarker marker= markers[i]; String content= fDocument.get(marker.getOffset(), marker.getLength()); String lines[]= Strings.convertIntoLines(content); Strings.trimIndentation(lines, fTypeRoot.getJavaProject(), false); if (fMarkerMode == STATEMENT_MODE && lines.length == 2 && isSingleControlStatementWithoutBlock()) { lines[1]= CodeFormatterUtil.createIndentString(1, fTypeRoot.getJavaProject()) + lines[1]; } result[i]= Strings.concatenate(lines, TextUtilities.getDefaultLineDelimiter(fDocument)); } return result; }
private String[] getBlocks(RangeMarker[] markers) throws BadLocationException { String[] result= new String[markers.length]; for (int i= 0; i < markers.length; i++) { RangeMarker marker= markers[i]; String content= fDocument.get(marker.getOffset(), marker.getLength()); String lines[]= Strings.convertIntoLines(content); Strings.trimIndentation(lines, fTypeRoot.getJavaProject(), false); if (fMarkerMode == STATEMENT_MODE && lines.length == 2 && isSingleControlStatementWithoutBlock()) { lines[1]= CodeFormatterUtil.createIndentString(1, fTypeRoot.getJavaProject()) + lines[1]; } result[i]= Strings.concatenate(lines, TextUtilities.getDefaultLineDelimiter(fDocument)); } return result; }
private boolean needsUserSelection(IJavaElement[] elements, ITypeRoot input) { if (elements[0] instanceof IPackageFragment) { IJavaProject javaProject= input.getJavaProject(); if (JavaModelUtil.is9OrHigher(javaProject)) { try { if (javaProject.getModuleDescription() != null) { for (IJavaElement element : elements) { IPackageFragmentRoot root= (IPackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); if (root.getModuleDescription() != null) return true; } } } catch (JavaModelException e) { // silent } } // below 9 or with no modules in the picture: // if there are multiple IPackageFragments that could be selected, use the first one on the build path. return false; } return true; }
private static String getExpressionBaseName(Expression expr) { IBinding argBinding= Bindings.resolveExpressionBinding(expr, true); if (argBinding instanceof IVariableBinding) { IJavaProject project= null; ASTNode root= expr.getRoot(); if (root instanceof CompilationUnit) { ITypeRoot typeRoot= ((CompilationUnit) root).getTypeRoot(); if (typeRoot != null) project= typeRoot.getJavaProject(); } return StubUtility.getBaseName((IVariableBinding)argBinding, project); } if (expr instanceof SimpleName) return ((SimpleName) expr).getIdentifier(); return null; }
private static String getExpressionBaseName(Expression expr) { IBinding argBinding= Bindings.resolveExpressionBinding(expr, true); if (argBinding instanceof IVariableBinding) { IJavaProject project= null; ASTNode root= expr.getRoot(); if (root instanceof CompilationUnit) { ITypeRoot typeRoot= ((CompilationUnit) root).getTypeRoot(); if (typeRoot != null) project= typeRoot.getJavaProject(); } return StubUtility.getBaseName((IVariableBinding)argBinding, project); } if (expr instanceof SimpleName) return ((SimpleName) expr).getIdentifier(); return null; }
private static String getExpressionBaseName(Expression expr) { IBinding argBinding= Bindings.resolveExpressionBinding(expr, true); if (argBinding instanceof IVariableBinding) { IJavaProject project= null; ASTNode root= expr.getRoot(); if (root instanceof CompilationUnit) { ITypeRoot typeRoot= ((CompilationUnit) root).getTypeRoot(); if (typeRoot != null) { project= typeRoot.getJavaProject(); } } return StubUtility.getBaseName((IVariableBinding)argBinding, project); } if (expr instanceof SimpleName) { return ((SimpleName) expr).getIdentifier(); } return null; }
public Change createChange(IProgressMonitor pm) throws CoreException { // TODO: update for fSelectionStart == -1 final Map arguments= new HashMap(); String project= null; IJavaProject javaProject= fSelectionTypeRoot.getJavaProject(); if (javaProject != null) project= javaProject.getElementName(); final IMethodBinding binding= fSourceProvider.getDeclaration().resolveBinding(); int flags= RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT; if (!Modifier.isPrivate(binding.getModifiers())) flags|= RefactoringDescriptor.MULTI_CHANGE; final String description= Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_descriptor_description_short, binding.getName()); final String header= Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_descriptor_description, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(binding.getDeclaringClass(), JavaElementLabels.ALL_FULLY_QUALIFIED)}); final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header); comment.addSetting(Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED))); if (!fTargetProvider.isSingle()) comment.addSetting(RefactoringCoreMessages.ReplaceInvocationsRefactoring_replace_references); final JDTRefactoringDescriptor descriptor= new JDTRefactoringDescriptor(ID_REPLACE_INVOCATIONS, project, description, comment.asString(), arguments, flags); arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_INPUT, descriptor.elementToHandle(fSelectionTypeRoot)); arguments.put(JDTRefactoringDescriptor.ATTRIBUTE_SELECTION, new Integer(fSelectionStart).toString() + " " + new Integer(fSelectionLength).toString()); //$NON-NLS-1$ arguments.put(ATTRIBUTE_MODE, new Integer(fTargetProvider.isSingle() ? 0 : 1).toString()); return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.ReplaceInvocationsRefactoring_change_name, fChangeManager.getAllChanges()); }
@Override public Change createChange(IProgressMonitor pm) throws CoreException { // TODO: update for fSelectionStart == -1 final Map<String, String> arguments= new HashMap<>(); String project= null; IJavaProject javaProject= fSelectionTypeRoot.getJavaProject(); if (javaProject != null) project= javaProject.getElementName(); final IMethodBinding binding= fSourceProvider.getDeclaration().resolveBinding(); int flags= RefactoringDescriptor.STRUCTURAL_CHANGE | JavaRefactoringDescriptor.JAR_REFACTORING | JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT; if (!Modifier.isPrivate(binding.getModifiers())) flags|= RefactoringDescriptor.MULTI_CHANGE; final String description= Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_descriptor_description_short, BasicElementLabels.getJavaElementName(binding.getName())); final String header= Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_descriptor_description, new String[] { BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED), BindingLabelProvider.getBindingLabel(binding.getDeclaringClass(), JavaElementLabels.ALL_FULLY_QUALIFIED)}); final JDTRefactoringDescriptorComment comment= new JDTRefactoringDescriptorComment(project, this, header); comment.addSetting(Messages.format(RefactoringCoreMessages.ReplaceInvocationsRefactoring_original_pattern, BindingLabelProvider.getBindingLabel(binding, JavaElementLabels.ALL_FULLY_QUALIFIED))); if (!fTargetProvider.isSingle()) comment.addSetting(RefactoringCoreMessages.ReplaceInvocationsRefactoring_replace_references); final JavaRefactoringDescriptor descriptor= new JavaRefactoringDescriptor(ID_REPLACE_INVOCATIONS, project, description, comment.asString(), arguments, flags){}; //REVIEW Unregistered ID! arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_INPUT, JavaRefactoringDescriptorUtil.elementToHandle(project, fSelectionTypeRoot)); arguments.put(JavaRefactoringDescriptorUtil.ATTRIBUTE_SELECTION, Integer.valueOf(fSelectionStart).toString() + " " + Integer.valueOf(fSelectionLength).toString()); //$NON-NLS-1$ arguments.put(ATTRIBUTE_MODE, Integer.valueOf(fTargetProvider.isSingle() ? 0 : 1).toString()); return new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.ReplaceInvocationsRefactoring_change_name, fChangeManager.getAllChanges()); }
/** * Creates a TokenScanner * @param typeRoot The type root to scan on * @throws CoreException thrown if the buffer cannot be accessed */ public TokenScanner(ITypeRoot typeRoot) throws CoreException { IJavaProject project= typeRoot.getJavaProject(); IBuffer buffer= typeRoot.getBuffer(); if (buffer == null) { throw new CoreException(createError(DOCUMENT_ERROR, "Element has no source", null)); //$NON-NLS-1$ } String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScanner= ToolFactory.createScanner(true, false, true, sourceLevel, complianceLevel); // line info required fScanner.setSource(buffer.getCharacters()); fDocument= null; // use scanner for line information fEndPosition= fScanner.getSource().length - 1; }
/** * Creates a TokenScanner * @param typeRoot The type root to scan on * @throws CoreException thrown if the buffer cannot be accessed */ public TokenScanner(ITypeRoot typeRoot) throws CoreException { IJavaProject project= typeRoot.getJavaProject(); IBuffer buffer= typeRoot.getBuffer(); if (buffer == null) { throw new CoreException(createError(DOCUMENT_ERROR, "Element has no source", null)); //$NON-NLS-1$ } String sourceLevel= project.getOption(JavaCore.COMPILER_SOURCE, true); String complianceLevel= project.getOption(JavaCore.COMPILER_COMPLIANCE, true); fScanner= ToolFactory.createScanner(true, false, true, sourceLevel, complianceLevel); // line info required fScanner.setSource(buffer.getCharacters()); fDocument= null; // use scanner for line information fEndPosition= fScanner.getSource().length - 1; }
public void initialize() { Block body= fDeclaration.getBody(); // first collect the static imports. This is necessary to not mark // static imported fields and methods as implicit visible. fTypesToImport= new ArrayList<>(); fStaticsToImport= new ArrayList<>(); ImportReferencesCollector.collect(body, fTypeRoot.getJavaProject(), null, fTypesToImport, fStaticsToImport); // Now collect implicit references and name references body.accept(new UpdateCollector()); int numberOfLocals= LocalVariableIndex.perform(fDeclaration); FlowContext context= new FlowContext(0, numberOfLocals + 1); context.setConsiderAccessMode(true); context.setComputeMode(FlowContext.MERGE); InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(context); FlowInfo info= flowAnalyzer.perform(getStatements()); for (Iterator<SingleVariableDeclaration> iter= fDeclaration.parameters().iterator(); iter.hasNext();) { SingleVariableDeclaration element= iter.next(); IVariableBinding binding= element.resolveBinding(); ParameterData data= (ParameterData)element.getProperty(ParameterData.PROPERTY); data.setAccessMode(info.getAccessMode(context, binding)); } }
public static CompilationUnit parse(final ITypeRoot unit, final IProgressMonitor mon) { ASTParser parser = ASTParser.newParser(AST.JLS3); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(unit); parser.setProject(unit.getJavaProject()); parser.setResolveBindings(true); parser.setStatementsRecovery(true); return (CompilationUnit) parser.createAST(mon); // parse }
public void initialize() { Block body= fDeclaration.getBody(); // first collect the static imports. This is necessary to not mark // static imported fields and methods as implicit visible. fTypesToImport= new ArrayList<>(); fStaticsToImport= new ArrayList<>(); ImportReferencesCollector.collect(body, fTypeRoot.getJavaProject(), null, fTypesToImport, fStaticsToImport); // Now collect implicit references and name references body.accept(new UpdateCollector()); int numberOfLocals= LocalVariableIndex.perform(fDeclaration); FlowContext context= new FlowContext(0, numberOfLocals + 1); context.setConsiderAccessMode(true); context.setComputeMode(FlowContext.MERGE); InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(context); FlowInfo info= flowAnalyzer.perform(getStatements()); for (Iterator<SingleVariableDeclaration> iter= fDeclaration.parameters().iterator(); iter.hasNext();) { SingleVariableDeclaration element= iter.next(); IVariableBinding binding= element.resolveBinding(); ParameterData data= (ParameterData)element.getProperty(ParameterData.PROPERTY); data.setAccessMode(info.getAccessMode(context, binding)); } }
private Location computeDefinitionNavigation(ITypeRoot unit, int line, int column, IProgressMonitor monitor) { try { IJavaElement element = JDTUtils.findElementAtSelection(unit, line, column, this.preferenceManager, monitor); if (element == null) { return null; } ICompilationUnit compilationUnit = (ICompilationUnit) element.getAncestor(IJavaElement.COMPILATION_UNIT); IClassFile cf = (IClassFile) element.getAncestor(IJavaElement.CLASS_FILE); if (compilationUnit != null || (cf != null && cf.getSourceRange() != null) ) { return fixLocation(element, JDTUtils.toLocation(element), unit.getJavaProject()); } if (element instanceof IMember && ((IMember) element).getClassFile() != null) { return fixLocation(element, JDTUtils.toLocation(((IMember) element).getClassFile()), unit.getJavaProject()); } } catch (JavaModelException e) { JavaLanguageServerPlugin.logException("Problem computing definition for" + unit.getElementName(), e); } return null; }
public void initialize() { Block body= fDeclaration.getBody(); // first collect the static imports. This is necessary to not mark // static imported fields and methods as implicit visible. fTypesToImport= new ArrayList(); fStaticsToImport= new ArrayList(); ImportReferencesCollector collector= new ImportReferencesCollector( fTypeRoot.getJavaProject(), null, fTypesToImport, fStaticsToImport); body.accept(collector); // Now collect implicit references and name references body.accept(new UpdateCollector()); int numberOfLocals= LocalVariableIndex.perform(fDeclaration); FlowContext context= new FlowContext(0, numberOfLocals + 1); context.setConsiderAccessMode(true); context.setComputeMode(FlowContext.MERGE); InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(context); FlowInfo info= flowAnalyzer.perform(getStatements()); for (Iterator iter= fDeclaration.parameters().iterator(); iter.hasNext();) { SingleVariableDeclaration element= (SingleVariableDeclaration) iter.next(); IVariableBinding binding= element.resolveBinding(); ParameterData data= (ParameterData)element.getProperty(ParameterData.PROPERTY); data.setAccessMode(info.getAccessMode(context, binding)); } }