use of org.eclipse.jdt.internal.corext.dom.Selection 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;
}
use of org.eclipse.jdt.internal.corext.dom.Selection 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;
}
use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.
the class ExtractMethodAnalyzer method visit.
@Override
public boolean visit(MethodDeclaration node) {
Block body = node.getBody();
if (body == null)
return false;
Selection selection = getSelection();
int nodeStart = body.getStartPosition();
int nodeExclusiveEnd = nodeStart + body.getLength();
// if selection node inside of the method body ignore method
if (!(nodeStart < selection.getOffset() && selection.getExclusiveEnd() < nodeExclusiveEnd))
return false;
return super.visit(node);
}
use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.
the class ExtractMethodAnalyzer method removeSelectedDeclarations.
private IVariableBinding[] removeSelectedDeclarations(IVariableBinding[] bindings) {
List<IVariableBinding> result = new ArrayList<IVariableBinding>(bindings.length);
Selection selection = getSelection();
for (int i = 0; i < bindings.length; i++) {
ASTNode decl = ((CompilationUnit) fEnclosingBodyDeclaration.getRoot()).findDeclaringNode(bindings[i]);
if (!selection.covers(decl))
result.add(bindings[i]);
}
return result.toArray(new IVariableBinding[result.size()]);
}
use of org.eclipse.jdt.internal.corext.dom.Selection in project che by eclipse.
the class ExtractMethodAnalyzer method computeOutput.
private void computeOutput(RefactoringStatus status) {
// First find all writes inside the selection.
FlowContext flowContext = new FlowContext(0, fMaxVariableId + 1);
flowContext.setConsiderAccessMode(true);
flowContext.setComputeMode(FlowContext.RETURN_VALUES);
FlowInfo returnInfo = new InOutFlowAnalyzer(flowContext).perform(getSelectedNodes());
IVariableBinding[] returnValues = returnInfo.get(flowContext, FlowInfo.WRITE | FlowInfo.WRITE_POTENTIAL | FlowInfo.UNKNOWN);
// Compute a selection that exactly covers the selected nodes
IRegion region = getSelectedNodeRange();
Selection selection = Selection.createFromStartLength(region.getOffset(), region.getLength());
List<IVariableBinding> localReads = new ArrayList<IVariableBinding>();
flowContext.setComputeMode(FlowContext.ARGUMENTS);
FlowInfo argInfo = new InputFlowAnalyzer(flowContext, selection, true).perform(fEnclosingBodyDeclaration);
IVariableBinding[] reads = argInfo.get(flowContext, FlowInfo.READ | FlowInfo.READ_POTENTIAL | FlowInfo.UNKNOWN);
outer: for (int i = 0; i < returnValues.length && localReads.size() < returnValues.length; i++) {
IVariableBinding binding = returnValues[i];
for (int x = 0; x < reads.length; x++) {
if (reads[x] == binding) {
localReads.add(binding);
fReturnValue = binding;
continue outer;
}
}
}
switch(localReads.size()) {
case 0:
fReturnValue = null;
break;
case 1:
break;
default:
fReturnValue = null;
StringBuffer affectedLocals = new StringBuffer();
for (int i = 0; i < localReads.size(); i++) {
IVariableBinding binding = localReads.get(i);
String bindingName = BindingLabelProvider.getBindingLabel(binding, BindingLabelProvider.DEFAULT_TEXTFLAGS | JavaElementLabels.F_PRE_TYPE_SIGNATURE);
affectedLocals.append(bindingName);
if (i != localReads.size() - 1) {
affectedLocals.append('\n');
}
}
String message = MessageFormat.format(RefactoringCoreMessages.ExtractMethodAnalyzer_assignments_to_local, new Object[] { affectedLocals.toString() });
status.addFatalError(message, JavaStatusContext.create(fCUnit, getSelection()));
return;
}
List<IVariableBinding> callerLocals = new ArrayList<IVariableBinding>(5);
FlowInfo localInfo = new InputFlowAnalyzer(flowContext, selection, false).perform(fEnclosingBodyDeclaration);
IVariableBinding[] writes = localInfo.get(flowContext, FlowInfo.WRITE | FlowInfo.WRITE_POTENTIAL | FlowInfo.UNKNOWN);
for (int i = 0; i < writes.length; i++) {
IVariableBinding write = writes[i];
if (getSelection().covers(ASTNodes.findDeclaration(write, fEnclosingBodyDeclaration)))
callerLocals.add(write);
}
fCallerLocals = callerLocals.toArray(new IVariableBinding[callerLocals.size()]);
if (fReturnValue != null && getSelection().covers(ASTNodes.findDeclaration(fReturnValue, fEnclosingBodyDeclaration)))
fReturnLocal = fReturnValue;
}
Aggregations