1 11 12 package org.eclipse.jdt.internal.ui.text.correction; 13 14 import java.util.HashSet ; 15 import java.util.List ; 16 import java.util.Set ; 17 18 import org.eclipse.core.runtime.Assert; 19 import org.eclipse.core.runtime.CoreException; 20 21 import org.eclipse.jdt.core.ICompilationUnit; 22 import org.eclipse.jdt.core.dom.AST; 23 import org.eclipse.jdt.core.dom.Annotation; 24 import org.eclipse.jdt.core.dom.ArrayInitializer; 25 import org.eclipse.jdt.core.dom.CompilationUnit; 26 import org.eclipse.jdt.core.dom.Expression; 27 import org.eclipse.jdt.core.dom.IMethodBinding; 28 import org.eclipse.jdt.core.dom.ITypeBinding; 29 import org.eclipse.jdt.core.dom.MarkerAnnotation; 30 import org.eclipse.jdt.core.dom.MemberValuePair; 31 import org.eclipse.jdt.core.dom.Name; 32 import org.eclipse.jdt.core.dom.NormalAnnotation; 33 import org.eclipse.jdt.core.dom.SingleMemberAnnotation; 34 import org.eclipse.jdt.core.dom.rewrite.ASTRewrite; 35 import org.eclipse.jdt.core.dom.rewrite.ListRewrite; 36 37 import org.eclipse.jdt.internal.ui.JavaPluginImages; 38 39 public class MissingAnnotationAttributesProposal extends LinkedCorrectionProposal { 40 41 private Annotation fAnnotation; 42 43 public MissingAnnotationAttributesProposal(ICompilationUnit cu, Annotation annotation, int relevance) { 44 super(CorrectionMessages.MissingAnnotationAttributesProposal_add_missing_attributes_label, cu, null, relevance, null); 45 setImage(JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE)); 46 47 fAnnotation= annotation; 48 Assert.isNotNull(fAnnotation.resolveTypeBinding()); 49 } 50 51 54 protected ASTRewrite getRewrite() throws CoreException { 55 AST ast= fAnnotation.getAST(); 56 57 ASTRewrite rewrite= ASTRewrite.create(ast); 58 createImportRewrite((CompilationUnit) fAnnotation.getRoot()); 59 60 ListRewrite listRewrite; 61 if (fAnnotation instanceof NormalAnnotation) { 62 listRewrite= rewrite.getListRewrite(fAnnotation, NormalAnnotation.VALUES_PROPERTY); 63 } else { 64 NormalAnnotation newAnnotation= ast.newNormalAnnotation(); 65 newAnnotation.setTypeName((Name) rewrite.createMoveTarget(fAnnotation.getTypeName())); 66 rewrite.replace(fAnnotation, newAnnotation, null); 67 68 listRewrite= rewrite.getListRewrite(newAnnotation, NormalAnnotation.VALUES_PROPERTY); 69 } 70 addMissingAtributes(fAnnotation.resolveTypeBinding(), listRewrite); 71 72 return rewrite; 73 } 74 75 private void addMissingAtributes(ITypeBinding binding, ListRewrite listRewriter) { 76 Set implementedAttribs= new HashSet (); 77 if (fAnnotation instanceof NormalAnnotation) { 78 List list= ((NormalAnnotation) fAnnotation).values(); 79 for (int i= 0; i < list.size(); i++) { 80 MemberValuePair curr= (MemberValuePair) list.get(i); 81 implementedAttribs.add(curr.getName().getIdentifier()); 82 } 83 } else if (fAnnotation instanceof SingleMemberAnnotation){ 84 implementedAttribs.add("value"); } 86 ASTRewrite rewriter= listRewriter.getASTRewrite(); 87 AST ast= rewriter.getAST(); 88 89 IMethodBinding[] declaredMethods= binding.getDeclaredMethods(); 90 for (int i= 0; i < declaredMethods.length; i++) { 91 IMethodBinding curr= declaredMethods[i]; 92 if (!implementedAttribs.contains(curr.getName()) && curr.getDefaultValue() == null) { 93 MemberValuePair pair= ast.newMemberValuePair(); 94 pair.setName(ast.newSimpleName(curr.getName())); 95 pair.setValue(newDefaultExpression(ast, curr.getReturnType())); 96 listRewriter.insertLast(pair, null); 97 98 addLinkedPosition(rewriter.track(pair.getName()), false, "val_name_" + i); addLinkedPosition(rewriter.track(pair.getValue()), false, "val_type_" + i); } 101 } 102 } 103 104 private Expression newDefaultExpression(AST ast, ITypeBinding type) { 105 if (type.isPrimitive()) { 106 String name= type.getName(); 107 if ("boolean".equals(name)) { return ast.newBooleanLiteral(false); 109 } else { 110 return ast.newNumberLiteral("0"); } 112 } 113 if (type == ast.resolveWellKnownType("java.lang.String")) { return ast.newStringLiteral(); 115 } 116 if (type.isArray()) { 117 ArrayInitializer initializer= ast.newArrayInitializer(); 118 initializer.expressions().add(newDefaultExpression(ast, type.getElementType())); 119 return initializer; 120 } 121 if (type.isAnnotation()) { 122 MarkerAnnotation annotation= ast.newMarkerAnnotation(); 123 annotation.setTypeName(ast.newName(getImportRewrite().addImport(type))); 124 return annotation; 125 } 126 return ast.newNullLiteral(); 127 } 128 129 130 } 131 | Popular Tags |