use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.
the class ExtractTempRefactoring method getSelectedExpression.
private IExpressionFragment getSelectedExpression() throws JavaModelException {
if (fSelectedExpression != null)
return fSelectedExpression;
IASTFragment selectedFragment = ASTFragmentFactory.createFragmentForSourceRange(new SourceRange(fSelectionStart, fSelectionLength), fCompilationUnitNode, fCu);
if (selectedFragment instanceof IExpressionFragment && !Checks.isInsideJavadoc(selectedFragment.getAssociatedNode())) {
fSelectedExpression = (IExpressionFragment) selectedFragment;
} else if (selectedFragment != null) {
if (selectedFragment.getAssociatedNode() instanceof ExpressionStatement) {
ExpressionStatement exprStatement = (ExpressionStatement) selectedFragment.getAssociatedNode();
Expression expression = exprStatement.getExpression();
fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(expression);
} else if (selectedFragment.getAssociatedNode() instanceof Assignment) {
Assignment assignment = (Assignment) selectedFragment.getAssociatedNode();
fSelectedExpression = (IExpressionFragment) ASTFragmentFactory.createFragmentForFullSubtree(assignment);
}
}
if (fSelectedExpression != null && Checks.isEnumCase(fSelectedExpression.getAssociatedExpression().getParent())) {
fSelectedExpression = null;
}
return fSelectedExpression;
}
use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.
the class ExtractTempRefactoring method checkMatchingFragments.
private RefactoringStatus checkMatchingFragments() throws JavaModelException {
RefactoringStatus result = new RefactoringStatus();
IASTFragment[] matchingFragments = getMatchingFragments();
for (int i = 0; i < matchingFragments.length; i++) {
ASTNode node = matchingFragments[i].getAssociatedNode();
if (isLeftValue(node) && !isReferringToLocalVariableFromFor((Expression) node)) {
String msg = RefactoringCoreMessages.ExtractTempRefactoring_assigned_to;
result.addWarning(msg, JavaStatusContext.create(fCu, node));
}
}
return result;
}
use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.
the class ExtractConstantRefactoring method getFragmentsToReplace.
private IASTFragment[] getFragmentsToReplace() throws JavaModelException {
List<IASTFragment> toReplace = new ArrayList<IASTFragment>();
if (fReplaceAllOccurrences) {
Iterator<ASTNode> replacementScope = getReplacementScope();
while (replacementScope.hasNext()) {
ASTNode scope = replacementScope.next();
IASTFragment[] allMatches = ASTFragmentFactory.createFragmentForFullSubtree(scope).getSubFragmentsMatching(getSelectedExpression());
IASTFragment[] replaceableMatches = retainOnlyReplacableMatches(allMatches);
for (int i = 0; i < replaceableMatches.length; i++) toReplace.add(replaceableMatches[i]);
}
} else if (canReplace(getSelectedExpression()))
toReplace.add(getSelectedExpression());
return toReplace.toArray(new IASTFragment[toReplace.size()]);
}
use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.
the class ExtractTempRefactoring method addReplaceExpressionWithTemp.
private void addReplaceExpressionWithTemp() throws JavaModelException {
IASTFragment[] fragmentsToReplace = retainOnlyReplacableMatches(getMatchingFragments());
//TODO: should not have to prune duplicates here...
ASTRewrite rewrite = fCURewrite.getASTRewrite();
HashSet<IASTFragment> seen = new HashSet<IASTFragment>();
for (int i = 0; i < fragmentsToReplace.length; i++) {
IASTFragment fragment = fragmentsToReplace[i];
if (!seen.add(fragment))
continue;
SimpleName tempName = fCURewrite.getAST().newSimpleName(fTempName);
TextEditGroup description = fCURewrite.createGroupDescription(RefactoringCoreMessages.ExtractTempRefactoring_replace);
fragment.replace(rewrite, tempName, description);
if (fLinkedProposalModel != null)
fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(rewrite.track(tempName), false);
}
}
use of org.eclipse.jdt.internal.corext.dom.fragments.IASTFragment in project che by eclipse.
the class ExtractConstantRefactoring method replaceExpressionsWithConstant.
private void replaceExpressionsWithConstant() throws JavaModelException {
ASTRewrite astRewrite = fCuRewrite.getASTRewrite();
AST ast = astRewrite.getAST();
IASTFragment[] fragmentsToReplace = getFragmentsToReplace();
for (int i = 0; i < fragmentsToReplace.length; i++) {
IASTFragment fragment = fragmentsToReplace[i];
ASTNode node = fragment.getAssociatedNode();
boolean inTypeDeclarationAnnotation = isInTypeDeclarationAnnotation(node);
if (inTypeDeclarationAnnotation && JdtFlags.VISIBILITY_STRING_PRIVATE == getVisibility())
continue;
SimpleName ref = ast.newSimpleName(fConstantName);
Name replacement = ref;
boolean qualifyReference = qualifyReferencesWithDeclaringClassName();
if (!qualifyReference) {
qualifyReference = inTypeDeclarationAnnotation;
}
if (qualifyReference) {
replacement = ast.newQualifiedName(ast.newSimpleName(getContainingTypeBinding().getName()), ref);
}
TextEditGroup description = fCuRewrite.createGroupDescription(RefactoringCoreMessages.ExtractConstantRefactoring_replace);
fragment.replace(astRewrite, replacement, description);
if (fLinkedProposalModel != null)
fLinkedProposalModel.getPositionGroup(KEY_NAME, true).addPosition(astRewrite.track(ref), false);
}
}
Aggregations