use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ReorgCorrectionsSubProcessor method getWrongTypeNameProposals.
public static void getWrongTypeNameProposals(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
ICompilationUnit cu = context.getCompilationUnit();
boolean isLinked = cu.getResource().isLinked();
IJavaProject javaProject = cu.getJavaProject();
String sourceLevel = javaProject.getOption(JavaCore.COMPILER_SOURCE, true);
String compliance = javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true);
CompilationUnit root = context.getASTRoot();
ASTNode coveredNode = problem.getCoveredNode(root);
if (!(coveredNode instanceof SimpleName))
return;
ASTNode parentType = coveredNode.getParent();
if (!(parentType instanceof AbstractTypeDeclaration))
return;
String currTypeName = ((SimpleName) coveredNode).getIdentifier();
String newTypeName = JavaCore.removeJavaLikeExtension(cu.getElementName());
boolean hasOtherPublicTypeBefore = false;
boolean found = false;
List<AbstractTypeDeclaration> types = root.types();
for (int i = 0; i < types.size(); i++) {
AbstractTypeDeclaration curr = types.get(i);
if (parentType != curr) {
if (newTypeName.equals(curr.getName().getIdentifier())) {
return;
}
if (!found && Modifier.isPublic(curr.getModifiers())) {
hasOtherPublicTypeBefore = true;
}
} else {
found = true;
}
}
if (!JavaConventions.validateJavaTypeName(newTypeName, sourceLevel, compliance).matches(IStatus.ERROR)) {
proposals.add(new CorrectMainTypeNameProposal(cu, context, currTypeName, newTypeName, IProposalRelevance.RENAME_TYPE));
}
if (!hasOtherPublicTypeBefore) {
String newCUName = JavaModelUtil.getRenamedCUName(cu, currTypeName);
ICompilationUnit newCU = ((IPackageFragment) (cu.getParent())).getCompilationUnit(newCUName);
if (!newCU.exists() && !isLinked && !JavaConventions.validateCompilationUnitName(newCUName, sourceLevel, compliance).matches(IStatus.ERROR)) {
RenameCompilationUnitChange change = new RenameCompilationUnitChange(cu, newCUName);
// rename CU
String label = Messages.format(CorrectionMessages.ReorgCorrectionsSubProcessor_renamecu_description, BasicElementLabels.getResourceName(newCUName));
proposals.add(new ChangeCorrectionProposal(label, change, IProposalRelevance.RENAME_CU, JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_RENAME)));
}
}
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class AddUnimplementedMethodsOperation method rewriteAST.
/**
* {@inheritDoc}
*/
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {
IMethodBinding[] unimplementedMethods = getUnimplementedMethods(fTypeNode);
if (unimplementedMethods.length == 0)
return;
ImportRewriteContext context = new ContextSensitiveImportRewriteContext((CompilationUnit) fTypeNode.getRoot(), fTypeNode.getStartPosition(), cuRewrite.getImportRewrite());
ASTRewrite rewrite = cuRewrite.getASTRewrite();
ICompilationUnit unit = cuRewrite.getCu();
CodeGenerationSettings settings = JavaPreferencesSettings.getCodeGenerationSettings(unit.getJavaProject());
ListRewrite listRewrite;
if (fTypeNode instanceof AnonymousClassDeclaration) {
AnonymousClassDeclaration decl = (AnonymousClassDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else if (fTypeNode instanceof AbstractTypeDeclaration) {
AbstractTypeDeclaration decl = (AbstractTypeDeclaration) fTypeNode;
listRewrite = rewrite.getListRewrite(decl, decl.getBodyDeclarationsProperty());
} else if (fTypeNode instanceof EnumConstantDeclaration) {
EnumConstantDeclaration enumConstantDeclaration = (EnumConstantDeclaration) fTypeNode;
AnonymousClassDeclaration anonymousClassDeclaration = enumConstantDeclaration.getAnonymousClassDeclaration();
if (anonymousClassDeclaration == null) {
anonymousClassDeclaration = rewrite.getAST().newAnonymousClassDeclaration();
rewrite.set(enumConstantDeclaration, EnumConstantDeclaration.ANONYMOUS_CLASS_DECLARATION_PROPERTY, anonymousClassDeclaration, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
listRewrite = rewrite.getListRewrite(anonymousClassDeclaration, AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
settings.createComments = false;
} else {
//$NON-NLS-1$
Assert.isTrue(false, "Unknown type node");
return;
}
ImportRewrite imports = cuRewrite.getImportRewrite();
for (int i = 0; i < unimplementedMethods.length; i++) {
IMethodBinding curr = unimplementedMethods[i];
MethodDeclaration newMethodDecl = StubUtility2.createImplementationStub(unit, rewrite, imports, context, curr, curr.getDeclaringClass().getName(), settings, false);
listRewrite.insertLast(newMethodDecl, createTextEditGroup(CorrectionMessages.AddUnimplementedMethodsOperation_AddMissingMethod_group, cuRewrite));
}
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class PotentialProgrammingProblemsFix method getDeclarationNode.
/**
* Returns the declaration node for the originally selected node.
* @param name the name of the node
*
* @return the declaration node
*/
private static ASTNode getDeclarationNode(SimpleName name) {
ASTNode parent = name.getParent();
if (!(parent instanceof AbstractTypeDeclaration)) {
parent = parent.getParent();
if (parent instanceof ParameterizedType || parent instanceof Type)
parent = parent.getParent();
if (parent instanceof ClassInstanceCreation) {
final ClassInstanceCreation creation = (ClassInstanceCreation) parent;
parent = creation.getAnonymousClassDeclaration();
}
}
return parent;
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class VariableDeclarationRewrite method rewriteModifiers.
public static void rewriteModifiers(final FieldDeclaration declarationNode, final VariableDeclarationFragment[] toChange, final int includedModifiers, final int excludedModifiers, final ASTRewrite rewrite, final TextEditGroup group) {
final List<VariableDeclarationFragment> fragmentsToChange = Arrays.asList(toChange);
AST ast = declarationNode.getAST();
/*
* Problem: Same declarationNode can be the subject of multiple calls to this method.
* For the 2nd++ calls, the original declarationNode has already been rewritten, and this has to be taken into account.
*
* Assumption:
* - Modifiers for each VariableDeclarationFragment are modified at most once.
*
* Solution:
* - Maintain a map from original VariableDeclarationFragments to their new FieldDeclaration.
* - Original modifiers in declarationNode belong to the first fragment.
* - When a later fragment needs different modifiers, we create a new FieldDeclaration and move all successive fragments into that declaration
* - When a fragment has been moved to a new declaration, make sure we don't create a new move target again, but instead use the already created one
*/
List<VariableDeclarationFragment> fragments = declarationNode.fragments();
Iterator<VariableDeclarationFragment> iter = fragments.iterator();
ListRewrite blockRewrite;
if (declarationNode.getParent() instanceof AbstractTypeDeclaration) {
blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), ((AbstractTypeDeclaration) declarationNode.getParent()).getBodyDeclarationsProperty());
} else {
blockRewrite = rewrite.getListRewrite(declarationNode.getParent(), AnonymousClassDeclaration.BODY_DECLARATIONS_PROPERTY);
}
VariableDeclarationFragment lastFragment = iter.next();
ASTNode lastStatement = declarationNode;
if (fragmentsToChange.contains(lastFragment)) {
ModifierRewrite modifierRewrite = ModifierRewrite.create(rewrite, declarationNode);
modifierRewrite.setModifiers(includedModifiers, excludedModifiers, group);
}
ListRewrite fragmentsRewrite = null;
while (iter.hasNext()) {
VariableDeclarationFragment currentFragment = iter.next();
@SuppressWarnings("unchecked") Map<VariableDeclarationFragment, MovedFragment> lookup = (Map<VariableDeclarationFragment, MovedFragment>) rewrite.getProperty(MovedFragment.class.getName());
if (lookup == null) {
lookup = new HashMap<VariableDeclarationFragment, MovedFragment>();
rewrite.setProperty(MovedFragment.class.getName(), lookup);
}
MovedFragment currentMovedFragment = lookup.get(currentFragment);
boolean changeLast = fragmentsToChange.contains(lastFragment);
boolean changeCurrent = fragmentsToChange.contains(currentFragment);
if (changeLast != changeCurrent || lookup.containsKey(lastFragment)) {
ModifierRewrite modifierRewrite = null;
if (currentMovedFragment != null) {
if (currentMovedFragment.fUsesOriginalModifiers) {
// Need to put in the right modifiers (removing any existing ones).
modifierRewrite = ModifierRewrite.create(rewrite, currentMovedFragment.fDeclaration);
ListRewrite listRewrite = rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.MODIFIERS2_PROPERTY);
List<IExtendedModifier> extendedList = listRewrite.getRewrittenList();
for (int i = 0; i < extendedList.size(); i++) {
ASTNode curr = (ASTNode) extendedList.get(i);
if (curr instanceof Modifier)
rewrite.remove(curr, group);
}
}
// otherwise, don't need to touch the modifiers, so leave modifierRewrite null
} else {
// need to split an existing field declaration
VariableDeclarationFragment moveTarget;
moveTarget = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
FieldDeclaration newStatement = (FieldDeclaration) ast.createInstance(FieldDeclaration.class);
rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY).insertLast(moveTarget, group);
lookup.put(currentFragment, new MovedFragment(moveTarget, newStatement, !changeCurrent));
rewrite.set(newStatement, FieldDeclaration.TYPE_PROPERTY, rewrite.createCopyTarget(declarationNode.getType()), group);
modifierRewrite = ModifierRewrite.create(rewrite, newStatement);
modifierRewrite.copyAllAnnotations(declarationNode, group);
blockRewrite.insertAfter(newStatement, lastStatement, group);
fragmentsRewrite = rewrite.getListRewrite(newStatement, FieldDeclaration.FRAGMENTS_PROPERTY);
lastStatement = newStatement;
}
if (modifierRewrite != null) {
if (changeCurrent) {
int newModifiers = (declarationNode.getModifiers() & ~excludedModifiers) | includedModifiers;
modifierRewrite.setModifiers(newModifiers, excludedModifiers, group);
} else {
int newModifiers = declarationNode.getModifiers();
modifierRewrite.setModifiers(newModifiers, Modifier.NONE, group);
}
}
} else if (fragmentsRewrite != null) {
VariableDeclarationFragment fragment0;
boolean usesOriginalModifiers = true;
if (currentMovedFragment != null) {
fragment0 = currentMovedFragment.fMoveTarget;
usesOriginalModifiers = currentMovedFragment.fUsesOriginalModifiers;
rewrite.getListRewrite(currentMovedFragment.fDeclaration, FieldDeclaration.FRAGMENTS_PROPERTY).remove(fragment0, group);
} else {
fragment0 = (VariableDeclarationFragment) rewrite.createMoveTarget(currentFragment);
}
lookup.put(currentFragment, new MovedFragment(fragment0, lastStatement, usesOriginalModifiers));
fragmentsRewrite.insertLast(fragment0, group);
}
lastFragment = currentFragment;
}
}
use of org.eclipse.jdt.core.dom.AbstractTypeDeclaration in project che by eclipse.
the class ChangeSignatureProcessor method createChangeManager.
private TextChangeManager createChangeManager(IProgressMonitor pm, RefactoringStatus result) throws CoreException {
pm.beginTask(RefactoringCoreMessages.ChangeSignatureRefactoring_preview, 2);
fChangeManager = new TextChangeManager();
boolean isNoArgConstructor = isNoArgConstructor();
Map<ICompilationUnit, Set<IType>> namedSubclassMapping = null;
if (isNoArgConstructor) {
//create only when needed;
namedSubclassMapping = createNamedSubclassMapping(new SubProgressMonitor(pm, 1));
} else {
pm.worked(1);
}
for (int i = 0; i < fOccurrences.length; i++) {
if (pm.isCanceled())
throw new OperationCanceledException();
SearchResultGroup group = fOccurrences[i];
ICompilationUnit cu = group.getCompilationUnit();
if (cu == null)
continue;
CompilationUnitRewrite cuRewrite;
if (cu.equals(getCu())) {
cuRewrite = fBaseCuRewrite;
} else {
cuRewrite = new CompilationUnitRewrite(cu);
cuRewrite.getASTRewrite().setTargetSourceRangeComputer(new TightSourceRangeComputer());
}
ASTNode[] nodes = ASTNodeSearchUtil.findNodes(group.getSearchResults(), cuRewrite.getRoot());
//IntroduceParameterObjectRefactoring needs to update declarations first:
List<OccurrenceUpdate<? extends ASTNode>> deferredUpdates = new ArrayList<OccurrenceUpdate<? extends ASTNode>>();
for (int j = 0; j < nodes.length; j++) {
OccurrenceUpdate<? extends ASTNode> update = createOccurrenceUpdate(nodes[j], cuRewrite, result);
if (update instanceof DeclarationUpdate) {
update.updateNode();
} else {
deferredUpdates.add(update);
}
}
for (Iterator<OccurrenceUpdate<? extends ASTNode>> iter = deferredUpdates.iterator(); iter.hasNext(); ) {
iter.next().updateNode();
}
if (isNoArgConstructor && namedSubclassMapping.containsKey(cu)) {
//only non-anonymous subclasses may have noArgConstructors to modify - see bug 43444
Set<IType> subtypes = namedSubclassMapping.get(cu);
for (Iterator<IType> iter = subtypes.iterator(); iter.hasNext(); ) {
IType subtype = iter.next();
AbstractTypeDeclaration subtypeNode = ASTNodeSearchUtil.getAbstractTypeDeclarationNode(subtype, cuRewrite.getRoot());
if (subtypeNode != null)
modifyImplicitCallsToNoArgConstructor(subtypeNode, cuRewrite);
}
}
TextChange change = cuRewrite.createChange(true);
if (change != null)
fChangeManager.manage(cu, change);
}
pm.done();
return fChangeManager;
}
Aggregations