diff options
2 files changed, 76 insertions, 21 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java index 7a6385e..c47cd2d 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/AdtUtils.java @@ -798,6 +798,39 @@ public class AdtUtils { } /** + * Returns the applicable build code (for + * {@code android.os.Build.VERSION_CODES}) for the corresponding API level, + * or null if it's unknown. + * + * @param api the API level to look up a version code for + * @return the corresponding build code field name, or null + */ + @Nullable + public static String getBuildCodes(int api) { + // See http://developer.android.com/reference/android/os/Build.VERSION_CODES.html + switch (api) { + case 1: return "BASE"; //$NON-NLS-1$ + case 2: return "BASE_1_1"; //$NON-NLS-1$ + case 3: return "CUPCAKE"; //$NON-NLS-1$ + case 4: return "DONUT"; //$NON-NLS-1$ + case 5: return "ECLAIR"; //$NON-NLS-1$ + case 6: return "ECLAIR_0_1"; //$NON-NLS-1$ + case 7: return "ECLAIR_MR1"; //$NON-NLS-1$ + case 8: return "FROYO"; //$NON-NLS-1$ + case 9: return "GINGERBREAD"; //$NON-NLS-1$ + case 10: return "GINGERBREAD_MR1"; //$NON-NLS-1$ + case 11: return "HONEYCOMB"; //$NON-NLS-1$ + case 12: return "HONEYCOMB_MR1"; //$NON-NLS-1$ + case 13: return "HONEYCOMB_MR2"; //$NON-NLS-1$ + case 14: return "ICE_CREAM_SANDWICH"; //$NON-NLS-1$ + case 15: return "ICE_CREAM_SANDWICH_MR1"; //$NON-NLS-1$ + case 16: return "JELLY_BEAN"; //$NON-NLS-1$ + } + + return null; + } + + /** * Returns the Android version and code name of the given API level * * @param api the api level diff --git a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java index f755e1e..df196de 100644 --- a/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java +++ b/eclipse/plugins/com.android.ide.eclipse.adt/src/com/android/ide/eclipse/adt/internal/lint/AddSuppressAnnotation.java @@ -23,6 +23,8 @@ import static com.android.tools.lint.detector.api.LintConstants.TARGET_API; import static org.eclipse.jdt.core.dom.ArrayInitializer.EXPRESSIONS_PROPERTY; import static org.eclipse.jdt.core.dom.SingleMemberAnnotation.VALUE_PROPERTY; +import com.android.annotations.NonNull; +import com.android.annotations.Nullable; import com.android.ide.eclipse.adt.AdtPlugin; import com.android.ide.eclipse.adt.AdtUtils; import com.android.ide.eclipse.adt.internal.editors.IconFactory; @@ -45,7 +47,6 @@ import org.eclipse.jdt.core.dom.Expression; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.NodeFinder; -import org.eclipse.jdt.core.dom.NumberLiteral; import org.eclipse.jdt.core.dom.SingleMemberAnnotation; import org.eclipse.jdt.core.dom.StringLiteral; import org.eclipse.jdt.core.dom.TypeDeclaration; @@ -79,12 +80,18 @@ class AddSuppressAnnotation implements IMarkerResolution2 { private final String mId; private final BodyDeclaration mNode; private final String mDescription; - /** Should it create a {@code @TargetApi} annotation instead of {@code SuppressLint} ? - * If so pass a positive API number */ - private final int mTargetApi; + /** + * Should it create a {@code @TargetApi} annotation instead of + * {@code SuppressLint} ? If so pass a non null API level + */ + private final String mTargetApi; - private AddSuppressAnnotation(String id, IMarker marker, BodyDeclaration node, - String description, int targetApi) { + private AddSuppressAnnotation( + @NonNull String id, + @NonNull IMarker marker, + @NonNull BodyDeclaration node, + @NonNull String description, + @Nullable String targetApi) { mId = id; mMarker = marker; mNode = node; @@ -120,7 +127,7 @@ class AddSuppressAnnotation implements IMarkerResolution2 { ICompilationUnit compilationUnit = manager.getWorkingCopy(editorInput); try { MultiTextEdit edit; - if (mTargetApi <= 0) { + if (mTargetApi == null) { edit = addSuppressAnnotation(document, compilationUnit, mNode); } else { edit = addTargetApiAnnotation(document, compilationUnit, mNode); @@ -247,28 +254,21 @@ class AddSuppressAnnotation implements IMarkerResolution2 { } ImportRewrite importRewrite = ImportRewrite.create(compilationUnit, true); + importRewrite.addImport("android.os.Build"); //$NON-NLS-1$ String local = importRewrite.addImport(FQCN_TARGET_API); AST ast = declaration.getAST(); ASTRewrite rewriter = ASTRewrite.create(ast); if (existing == null) { SingleMemberAnnotation newAnnotation = ast.newSingleMemberAnnotation(); newAnnotation.setTypeName(ast.newSimpleName(local)); - NumberLiteral value = ast.newNumberLiteral(Integer.toString(mTargetApi)); - //value.setLiteralValue(mId); + Expression value = createLiteral(ast); newAnnotation.setValue(value); ListRewrite listRewrite = rewriter.getListRewrite(declaration, declaration.getModifiersProperty()); listRewrite.insertFirst(newAnnotation, null); } else { - Expression existingValue = existing.getValue(); - if (existingValue instanceof NumberLiteral) { - // Change the value to the new value - NumberLiteral value = ast.newNumberLiteral(Integer.toString(mTargetApi)); - rewriter.set(existing, VALUE_PROPERTY, value, null); - } else { - assert false : existingValue; - return null; - } + Expression value = createLiteral(ast); + rewriter.set(existing, VALUE_PROPERTY, value, null); } TextEdit importEdits = importRewrite.rewriteImports(new NullProgressMonitor()); @@ -282,6 +282,23 @@ class AddSuppressAnnotation implements IMarkerResolution2 { return edit; } + private Expression createLiteral(AST ast) { + Expression value; + if (!isCodeName()) { + value = ast.newQualifiedName( + ast.newQualifiedName(ast.newSimpleName("Build"), //$NON-NLS-1$ + ast.newSimpleName("VERSION_CODES")), //$NON-NLS-1$ + ast.newSimpleName(mTargetApi)); + } else { + value = ast.newNumberLiteral(mTargetApi); + } + return value; + } + + private boolean isCodeName() { + return Character.isDigit(mTargetApi.charAt(0)); + } + /** * Adds any applicable suppress lint fix resolutions into the given list * @@ -384,14 +401,19 @@ class AddSuppressAnnotation implements IMarkerResolution2 { } String desc = String.format("Add @SuppressLint '%1$s\' to '%2$s'", id, target); - resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, -1)); + resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, null)); if (api != -1 // @TargetApi is only valid on methods and classes, not fields etc && (body instanceof MethodDeclaration || body instanceof TypeDeclaration)) { - desc = String.format("Add @TargetApi(%1$d) to '%2$s'", api, target); - resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, api)); + String apiString = AdtUtils.getBuildCodes(api); + if (apiString == null) { + apiString = Integer.toString(api); + } + desc = String.format("Add @TargetApi(%1$s) to '%2$s'", apiString, target); + resolutions.add(new AddSuppressAnnotation(id, marker, declaration, desc, + apiString)); } } } |