-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the logic of MissingAnnotationAttributes to o.e.j.core.manipulation
Signed-off-by: David Thompson <davthomp@redhat.com>
- Loading branch information
Showing
3 changed files
with
192 additions
and
101 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...ore extension/org/eclipse/jdt/internal/corext/fix/MissingAnnotationAttributesFixCore.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.internal.corext.fix; | ||
|
||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.Annotation; | ||
import org.eclipse.jdt.core.dom.CompilationUnit; | ||
|
||
import org.eclipse.jdt.internal.ui.text.correction.CorrectionMessages; | ||
import org.eclipse.jdt.internal.ui.text.correction.IProblemLocationCore; | ||
|
||
public class MissingAnnotationAttributesFixCore extends CompilationUnitRewriteOperationsFixCore { | ||
|
||
public MissingAnnotationAttributesFixCore(String name, CompilationUnit compilationUnit, CompilationUnitRewriteOperation operation) { | ||
super(name, compilationUnit, operation); | ||
} | ||
|
||
/** | ||
* Returns a fix that adds the missing required attributes to the annotation at the problem | ||
* location, or null if the given problem location isn't an annotation. | ||
* | ||
* @param cu the AST of the compilation unit where an attribute is missing required attributes | ||
* @param problemLocation the error marker that marks the annotation that's missing attributes | ||
* @return a fix that adds the missing required attributes to the annotation at the problem | ||
* location, or null if the given problem location isn't an annotation | ||
*/ | ||
public static MissingAnnotationAttributesFixCore addMissingAnnotationAttributesProposal(CompilationUnit cu, IProblemLocationCore problemLocation) { | ||
ASTNode selectedNode= problemLocation.getCoveringNode(cu); | ||
if (!(selectedNode instanceof Annotation)) { | ||
return null; | ||
} | ||
Annotation annotation= (Annotation) selectedNode; | ||
MissingAnnotationAttributesProposalOperation rewriteProposal= new MissingAnnotationAttributesProposalOperation(annotation); | ||
|
||
return new MissingAnnotationAttributesFixCore(CorrectionMessages.MissingAnnotationAttributesProposal_add_missing_attributes_label, cu, rewriteProposal); | ||
} | ||
|
||
} |
132 changes: 132 additions & 0 deletions
132
...ion/org/eclipse/jdt/internal/corext/fix/MissingAnnotationAttributesProposalOperation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 Red Hat Inc. and others. | ||
* | ||
* This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License 2.0 | ||
* which accompanies this distribution, and is available at | ||
* https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Red Hat Inc. - initial API and implementation | ||
*******************************************************************************/ | ||
package org.eclipse.jdt.internal.corext.fix; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.eclipse.core.runtime.Assert; | ||
import org.eclipse.core.runtime.CoreException; | ||
|
||
import org.eclipse.jdt.core.dom.AST; | ||
import org.eclipse.jdt.core.dom.ASTNode; | ||
import org.eclipse.jdt.core.dom.Annotation; | ||
import org.eclipse.jdt.core.dom.ArrayInitializer; | ||
import org.eclipse.jdt.core.dom.Expression; | ||
import org.eclipse.jdt.core.dom.IMethodBinding; | ||
import org.eclipse.jdt.core.dom.ITypeBinding; | ||
import org.eclipse.jdt.core.dom.MarkerAnnotation; | ||
import org.eclipse.jdt.core.dom.MemberValuePair; | ||
import org.eclipse.jdt.core.dom.Name; | ||
import org.eclipse.jdt.core.dom.NormalAnnotation; | ||
import org.eclipse.jdt.core.dom.SingleMemberAnnotation; | ||
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; | ||
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite; | ||
import org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext; | ||
import org.eclipse.jdt.core.dom.rewrite.ListRewrite; | ||
|
||
import org.eclipse.jdt.internal.core.manipulation.dom.ASTResolving; | ||
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext; | ||
import org.eclipse.jdt.internal.corext.fix.CompilationUnitRewriteOperationsFixCore.CompilationUnitRewriteOperation; | ||
import org.eclipse.jdt.internal.corext.refactoring.structure.CompilationUnitRewrite; | ||
|
||
public class MissingAnnotationAttributesProposalOperation extends CompilationUnitRewriteOperation { | ||
|
||
private Annotation fAnnotation; | ||
|
||
public MissingAnnotationAttributesProposalOperation(Annotation annotation) { | ||
fAnnotation= annotation; | ||
Assert.isNotNull(annotation.resolveTypeBinding()); | ||
} | ||
|
||
@Override | ||
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore linkedModel) throws CoreException { | ||
ASTRewrite rewrite= cuRewrite.getASTRewrite(); | ||
|
||
AST ast= fAnnotation.getAST(); | ||
|
||
ListRewrite listRewrite; | ||
if (fAnnotation instanceof NormalAnnotation) { | ||
listRewrite= rewrite.getListRewrite(fAnnotation, NormalAnnotation.VALUES_PROPERTY); | ||
} else { | ||
NormalAnnotation newAnnotation= ast.newNormalAnnotation(); | ||
newAnnotation.setTypeName((Name) rewrite.createMoveTarget(fAnnotation.getTypeName())); | ||
rewrite.replace(fAnnotation, newAnnotation, null); | ||
|
||
listRewrite= rewrite.getListRewrite(newAnnotation, NormalAnnotation.VALUES_PROPERTY); | ||
} | ||
addMissingAtributes(fAnnotation.resolveTypeBinding(), listRewrite, cuRewrite, linkedModel); | ||
|
||
} | ||
|
||
private void addMissingAtributes(ITypeBinding binding, ListRewrite listRewriter, CompilationUnitRewrite cuRewrite, LinkedProposalModelCore linkedModel) { | ||
Set<String> implementedAttribs= new HashSet<>(); | ||
if (fAnnotation instanceof NormalAnnotation) { | ||
List<MemberValuePair> list= ((NormalAnnotation) fAnnotation).values(); | ||
for (MemberValuePair curr : list) { | ||
implementedAttribs.add(curr.getName().getIdentifier()); | ||
} | ||
} else if (fAnnotation instanceof SingleMemberAnnotation) { | ||
implementedAttribs.add("value"); //$NON-NLS-1$ | ||
} | ||
ASTRewrite rewriter= listRewriter.getASTRewrite(); | ||
AST ast= rewriter.getAST(); | ||
ImportRewriteContext context= null; | ||
ASTNode bodyDeclaration= ASTResolving.findParentBodyDeclaration(listRewriter.getParent()); | ||
if (bodyDeclaration != null) { | ||
context= new ContextSensitiveImportRewriteContext(bodyDeclaration, cuRewrite.getImportRewrite()); | ||
} | ||
|
||
IMethodBinding[] declaredMethods= binding.getDeclaredMethods(); | ||
for (int i= 0; i < declaredMethods.length; i++) { | ||
IMethodBinding curr= declaredMethods[i]; | ||
if (!implementedAttribs.contains(curr.getName()) && curr.getDefaultValue() == null) { | ||
MemberValuePair pair= ast.newMemberValuePair(); | ||
pair.setName(ast.newSimpleName(curr.getName())); | ||
pair.setValue(newDefaultExpression(ast, curr.getReturnType(), context, cuRewrite.getImportRewrite())); | ||
listRewriter.insertLast(pair, null); | ||
|
||
linkedModel.getPositionGroup("val_name_" + i, true).addPosition(rewriter.track(pair.getName()), false); //$NON-NLS-1$ | ||
linkedModel.getPositionGroup("val_type_" + i, true).addPosition(rewriter.track(pair.getValue()), false); //$NON-NLS-1$ | ||
} | ||
} | ||
} | ||
|
||
private Expression newDefaultExpression(AST ast, ITypeBinding type, ImportRewriteContext context, ImportRewrite importRewrite) { | ||
if (type.isPrimitive()) { | ||
String name= type.getName(); | ||
if ("boolean".equals(name)) { //$NON-NLS-1$ | ||
return ast.newBooleanLiteral(false); | ||
} else { | ||
return ast.newNumberLiteral("0"); //$NON-NLS-1$ | ||
} | ||
} | ||
if (type == ast.resolveWellKnownType("java.lang.String")) { //$NON-NLS-1$ | ||
return ast.newStringLiteral(); | ||
} | ||
if (type.isArray()) { | ||
ArrayInitializer initializer= ast.newArrayInitializer(); | ||
initializer.expressions().add(newDefaultExpression(ast, type.getElementType(), context, importRewrite)); | ||
return initializer; | ||
} | ||
if (type.isAnnotation()) { | ||
MarkerAnnotation annotation= ast.newMarkerAnnotation(); | ||
annotation.setTypeName(ast.newName(importRewrite.addImport(type, context))); | ||
return annotation; | ||
} | ||
return ast.newNullLiteral(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters