diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseColorIntentionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseColorIntentionAction.java new file mode 100644 index 000000000000..adde45b86af6 --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/BaseColorIntentionAction.java @@ -0,0 +1,80 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.intention.impl; + +import com.intellij.codeInsight.intention.HighPriorityAction; +import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.psi.util.PsiTreeUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import static com.intellij.patterns.PlatformPatterns.psiElement; + +/** + * @author Danila Ponomarenko + */ +public abstract class BaseColorIntentionAction extends PsiElementBaseIntentionAction implements HighPriorityAction { + protected static final String JAVA_AWT_COLOR = "java.awt.Color"; + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + if (!psiElement().inside(psiElement(PsiNewExpression.class)).accepts(element)) { + return false; + } + + final PsiNewExpression expression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class, false); + if (expression == null) { + return false; + } + + return isJavaAwtColor(expression.getClassOrAnonymousClassReference()) && isValueArguments(expression.getArgumentList()); + } + + private static boolean isJavaAwtColor(@Nullable PsiJavaCodeReferenceElement ref) { + if (ref == null) { + return false; + } + + final PsiReference reference = ref.getReference(); + if (reference == null) { + return false; + } + + final PsiElement psiElement = reference.resolve(); + if (psiElement instanceof PsiClass && JAVA_AWT_COLOR.equals(((PsiClass)psiElement).getQualifiedName())) { + return true; + } + + return false; + } + + private static boolean isValueArguments(@Nullable PsiExpressionList arguments) { + if (arguments == null) { + return false; + } + + for (PsiExpression argument : arguments.getExpressions()) { + if (argument instanceof PsiReferenceExpression) { + return false; + } + } + + return true; + } +} diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/ColorChooserIntentionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ColorChooserIntentionAction.java index 6434247509b4..3f1c3b6f6f81 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/ColorChooserIntentionAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ColorChooserIntentionAction.java @@ -46,9 +46,7 @@ import static com.intellij.patterns.PlatformPatterns.psiElement; * @author spleaner * @author Konstantin Bulenkov */ -public class ColorChooserIntentionAction extends PsiElementBaseIntentionAction { - private static final String JAVA_AWT_COLOR = "java.awt.Color"; - +public class ColorChooserIntentionAction extends BaseColorIntentionAction { private static final PsiMethodPattern DECODE_METHOD = PsiJavaPatterns.psiMethod() .definedInClass(JAVA_AWT_COLOR) .withName("decode"); @@ -64,20 +62,7 @@ public class ColorChooserIntentionAction extends PsiElementBaseIntentionAction { @Override public boolean isAvailable(@NotNull final Project project, final Editor editor, @NotNull final PsiElement element) { - // new Color(...) - if (psiElement().inside(psiElement(PsiNewExpression.class)).accepts(element)) { - final PsiNewExpression expression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class, false); - if (expression != null) { - final PsiJavaCodeReferenceElement ref = PsiTreeUtil.getChildOfType(expression, PsiJavaCodeReferenceElement.class); - if (isJavaAwtColor(ref)) return true; - } - } - // Color.decode("...") - if (isInsideDecodeOrGetColorMethod(element)) { - return true; - } - - return false; + return super.isAvailable(project, editor, element) || isInsideDecodeOrGetColorMethod(element); } public static boolean isInsideDecodeOrGetColorMethod(PsiElement element) { @@ -85,24 +70,10 @@ public class ColorChooserIntentionAction extends PsiElementBaseIntentionAction { element = element.getParent(); } - return PsiJavaPatterns.psiExpression().methodCallParameter(0, DECODE_METHOD).accepts(element) - || + return PsiJavaPatterns.psiExpression().methodCallParameter(0, DECODE_METHOD).accepts(element) || PsiJavaPatterns.psiExpression().methodCallParameter(0, GET_COLOR_METHOD).accepts(element); } - private static boolean isJavaAwtColor(final PsiJavaCodeReferenceElement ref) { - if (ref != null) { - final PsiReference reference = ref.getReference(); - if (reference != null) { - final PsiElement psiElement = reference.resolve(); - if (psiElement instanceof PsiClass && JAVA_AWT_COLOR.equals(((PsiClass)psiElement).getQualifiedName())) { - return true; - } - } - } - return false; - } - @Override @NotNull public String getFamilyName() { @@ -116,7 +87,8 @@ public class ColorChooserIntentionAction extends PsiElementBaseIntentionAction { final JComponent editorComponent = editor.getComponent(); if (isInsideDecodeOrGetColorMethod(element)) { invokeForMethodParam(editorComponent, element); - } else { + } + else { invokeForConstructor(editorComponent, element); } } @@ -139,9 +111,9 @@ public class ColorChooserIntentionAction extends PsiElementBaseIntentionAction { if (color == null) return; final int rgb = color.getRGB() - ((255 & 0xFF) << 24); if (color != null && rgb != oldColor.getRGB()) { - final String newText = radix == 16 ? hexPrefix + String.format("%6s" ,Integer.toHexString(rgb)).replace(' ', '0') - : radix == 8 ? "0" + Integer.toOctalString(rgb) - : Integer.toString(rgb); + final String newText = radix == 16 ? hexPrefix + String.format("%6s", Integer.toHexString(rgb)).replace(' ', '0') + : radix == 8 ? "0" + Integer.toOctalString(rgb) + : Integer.toString(rgb); final PsiManager manager = literal.getManager(); final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); final PsiExpression newLiteral = factory.createExpressionFromText("\"" + newText + "\"", literal); @@ -221,12 +193,12 @@ public class ColorChooserIntentionAction extends PsiElementBaseIntentionAction { final PsiManager manager = expression.getManager(); final PsiElementFactory factory = JavaPsiFacade.getInstance(manager.getProject()).getElementFactory(); final PsiExpression newCall = factory.createExpressionFromText( - "new " + JAVA_AWT_COLOR +"(" - + color.getRed() + ", " - + color.getGreen() + ", " - + color.getBlue() - + (color.getAlpha() < 255 ? ", " + color.getAlpha() : "") - +")", expression); + "new " + JAVA_AWT_COLOR + "(" + + color.getRed() + ", " + + color.getGreen() + ", " + + color.getBlue() + + (color.getAlpha() < 255 ? ", " + color.getAlpha() : "") + + ")", expression); final PsiElement insertedElement = expression.replace(newCall); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(manager.getProject()); codeStyleManager.reformat(insertedElement); diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/ConvertColorRepresentationIntentionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ConvertColorRepresentationIntentionAction.java new file mode 100644 index 000000000000..c3944ba6158b --- /dev/null +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/ConvertColorRepresentationIntentionAction.java @@ -0,0 +1,226 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.intention.impl; + +import com.intellij.codeInsight.CodeInsightBundle; +import com.intellij.codeInsight.CodeInsightUtilBase; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.project.Project; +import com.intellij.psi.*; +import com.intellij.psi.impl.JavaConstantExpressionEvaluator; +import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.IncorrectOperationException; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +/** + * @author Danila Ponomarenko + */ +public class ConvertColorRepresentationIntentionAction extends BaseColorIntentionAction { + + @Override + public boolean isAvailable(@NotNull Project project, Editor editor, @NotNull PsiElement element) { + if (!super.isAvailable(project, editor, element)) { + return false; + } + + final PsiNewExpression expression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class, false); + if (expression == null) { + return false; + } + + final PsiExpressionList arguments = expression.getArgumentList(); + if (arguments == null) { + return false; + } + + final PsiMethod constructor = expression.resolveConstructor(); + if (constructor == null) { + return false; + } + + final PsiExpressionList newArguments = createNewArguments(JavaPsiFacade.getElementFactory(project), constructor.getParameterList().getParameters(), arguments.getExpressions()); + + if (newArguments == null) { + return false; + } + + setText(CodeInsightBundle.message("intention.convert.color.representation.text", newArguments.getText())); + + return true; + } + + @Override + public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException { + if (!CodeInsightUtilBase.preparePsiElementForWrite(element)) return; + + final PsiNewExpression expression = PsiTreeUtil.getParentOfType(element, PsiNewExpression.class, false); + if (expression == null) { + return; + } + + final PsiExpressionList arguments = expression.getArgumentList(); + if (arguments == null) { + return; + } + + final PsiMethod constructor = expression.resolveConstructor(); + if (constructor == null) { + return; + } + + final PsiExpressionList newArguments = createNewArguments( + JavaPsiFacade.getElementFactory(project), + constructor.getParameterList().getParameters(), + arguments.getExpressions() + ); + + if (newArguments == null) { + return; + } + + arguments.replace(newArguments); + } + + @Nullable + private static PsiExpressionList createNewArguments(@NotNull PsiElementFactory factory, + @NotNull PsiParameter[] parameters, + @NotNull PsiExpression[] arguments) { + final String[] newValues = createArguments(parameters, arguments); + if (newValues == null) { + return null; + } + + final PsiExpressionList result = ((PsiNewExpression)factory.createExpressionFromText("new Object()", parameters[0])).getArgumentList(); + if (result == null) { + return null; + } + for (String value : newValues) { + result.add(factory.createExpressionFromText(value, parameters[0])); + } + return result; + } + + @Nullable + private static String[] createArguments(@NotNull PsiParameter[] parameters, + @NotNull PsiExpression[] arguments) { + if (parameters.length != arguments.length) { + return null; + } + + switch (parameters.length) { + default: + return null; + case 1: + return createArguments(arguments[0]); + case 2: + return createArguments(arguments[0], arguments[1]); + case 3: + return createArguments(arguments[0], arguments[1], arguments[2]); + case 4: + return createArguments(arguments[0], arguments[1], arguments[2], arguments[3]); + } + } + + @Nullable + private static String[] createArguments(@NotNull PsiExpression rgbExpression) { + return createArguments(rgbExpression, 3); + } + + @Nullable + private static String[] createArguments(@NotNull PsiExpression rgbExpression, + @NotNull PsiExpression hasAlphaExpression) { + final Boolean hasAlpha = computeBoolean(hasAlphaExpression); + if (hasAlpha == null) { + return null; + } + return hasAlpha ? createArguments(rgbExpression, 4) : createArguments(rgbExpression); + } + + @Nullable + private static String[] createArguments(@NotNull PsiExpression rExpression, + @NotNull PsiExpression gExpression, + @NotNull PsiExpression bExpression) { + final Integer value = createInt(computeInteger(rExpression), computeInteger(gExpression), computeInteger(bExpression)); + return value != null ? new String[]{"0x" + Integer.toHexString(value)} : null; + } + + @Nullable + private static String[] createArguments(@NotNull PsiExpression rExpression, + @NotNull PsiExpression gExpression, + @NotNull PsiExpression bExpression, + @NotNull PsiExpression aExpression) { + final Integer value = createInt(computeInteger(rExpression), computeInteger(gExpression), computeInteger(bExpression), computeInteger(aExpression)); + if (value == null) { + return null; + } + + return new String[]{ + "0x" + Integer.toHexString(value), + "true", + }; + } + + @Nullable + private static String[] createArguments(@NotNull PsiExpression rgbExpression, + int parts) { + final Integer rgb = computeInteger(rgbExpression); + if (rgb == null) { + return null; + } + + final String[] result = new String[parts]; + for (int i = 0; i < result.length; i++) { + result[result.length - i - 1] = String.valueOf(rgb >> (i * Byte.SIZE) & 0xFF); + } + return result; + } + + @Nullable + private static Integer createInt(Integer... ints) { + int result = 0; + for (Integer i : ints) { + if (i == null) { + return null; + } + result = result << Byte.SIZE | (i & 0xFF); + } + return result; + } + + @Nullable + public static Integer computeInteger(@NotNull PsiExpression expr) { + final Object result = compute(expr); + return result instanceof Integer ? (Integer)result : null; + } + + @Nullable + public static Boolean computeBoolean(@NotNull PsiExpression expr) { + final Object result = compute(expr); + return result instanceof Boolean ? (Boolean)result : null; + } + + @Nullable + private static Object compute(@NotNull PsiExpression expr) { + return JavaConstantExpressionEvaluator.computeConstantExpression(expr, true); + } + + @NotNull + @Override + public String getFamilyName() { + return CodeInsightBundle.message("intention.convert.color.representation.family"); + } +} diff --git a/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java b/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java index 1eb3ae340a0e..65b09c3f42d4 100644 --- a/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java +++ b/java/java-psi-api/src/com/intellij/psi/util/PsiTypesUtil.java @@ -24,31 +24,31 @@ import org.jetbrains.annotations.Nullable; import java.util.Map; public class PsiTypesUtil { - @NonNls private static final Map ourUnboxedTypes = new THashMap(); - @NonNls private static final Map ourBoxedTypes = new THashMap(); + @NonNls private static final Map ourUnboxedTypes = new THashMap() {{ + put(CommonClassNames.JAVA_LANG_BOOLEAN, "boolean"); + put(CommonClassNames.JAVA_LANG_BYTE, "byte"); + put(CommonClassNames.JAVA_LANG_SHORT, "short"); + put(CommonClassNames.JAVA_LANG_INTEGER, "int"); + put(CommonClassNames.JAVA_LANG_LONG, "long"); + put(CommonClassNames.JAVA_LANG_FLOAT, "float"); + put(CommonClassNames.JAVA_LANG_DOUBLE, "double"); + put(CommonClassNames.JAVA_LANG_CHARACTER, "char"); + }}; - static { - ourUnboxedTypes.put(CommonClassNames.JAVA_LANG_BOOLEAN, "boolean"); - ourUnboxedTypes.put(CommonClassNames.JAVA_LANG_BYTE, "byte"); - ourUnboxedTypes.put(CommonClassNames.JAVA_LANG_SHORT, "short"); - ourUnboxedTypes.put(CommonClassNames.JAVA_LANG_INTEGER, "int"); - ourUnboxedTypes.put(CommonClassNames.JAVA_LANG_LONG, "long"); - ourUnboxedTypes.put(CommonClassNames.JAVA_LANG_FLOAT, "float"); - ourUnboxedTypes.put(CommonClassNames.JAVA_LANG_DOUBLE, "double"); - ourUnboxedTypes.put(CommonClassNames.JAVA_LANG_CHARACTER, "char"); + @NonNls private static final Map ourBoxedTypes = new THashMap() {{ + put("boolean", CommonClassNames.JAVA_LANG_BOOLEAN); + put("byte", CommonClassNames.JAVA_LANG_BYTE); + put("short", CommonClassNames.JAVA_LANG_SHORT); + put("int", CommonClassNames.JAVA_LANG_INTEGER); + put("long", CommonClassNames.JAVA_LANG_LONG); + put("float", CommonClassNames.JAVA_LANG_FLOAT); + put("double", CommonClassNames.JAVA_LANG_DOUBLE); + put("char", CommonClassNames.JAVA_LANG_CHARACTER); + }}; - ourBoxedTypes.put("boolean", CommonClassNames.JAVA_LANG_BOOLEAN); - ourBoxedTypes.put("byte", CommonClassNames.JAVA_LANG_BYTE); - ourBoxedTypes.put("short", CommonClassNames.JAVA_LANG_SHORT); - ourBoxedTypes.put("int", CommonClassNames.JAVA_LANG_INTEGER); - ourBoxedTypes.put("long", CommonClassNames.JAVA_LANG_LONG); - ourBoxedTypes.put("float", CommonClassNames.JAVA_LANG_FLOAT); - ourBoxedTypes.put("double", CommonClassNames.JAVA_LANG_DOUBLE); - ourBoxedTypes.put("char", CommonClassNames.JAVA_LANG_CHARACTER); + private PsiTypesUtil() { } - private PsiTypesUtil() { } - public static String getDefaultValueOfType(PsiType type) { if (type instanceof PsiArrayType) { int count = type.getArrayDimensions() - 1; @@ -86,6 +86,7 @@ public class PsiTypesUtil { /** * Returns the unboxed type name or parameter. + * * @param type boxed java type name * @return unboxed type name if available; same value otherwise */ @@ -93,11 +94,12 @@ public class PsiTypesUtil { public static String unboxIfPossible(final String type) { if (type == null) return null; final String s = ourUnboxedTypes.get(type); - return s == null? type : s; + return s == null ? type : s; } /** * Returns the boxed type name or parameter. + * * @param type primitive java type name * @return boxed type name if available; same value otherwise */ @@ -110,7 +112,7 @@ public class PsiTypesUtil { @Nullable public static PsiClass getPsiClass(final PsiType psiType) { - return psiType instanceof PsiClassType? ((PsiClassType)psiType).resolve() : null; + return psiType instanceof PsiClassType ? ((PsiClassType)psiType).resolve() : null; } public static PsiClassType getClassType(@NotNull PsiClass psiClass) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterHex2Rgb.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterHex2Rgb.java new file mode 100644 index 000000000000..18f771434d33 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterHex2Rgb.java @@ -0,0 +1,21 @@ +// "Convert to 'new Color(37, 100, 120)'" "true" + +package java.awt; + +public class A { + private Color color = new Color(37, 100, 120); +} + +class Color { + Color(int r, int g, int b) { + } + + Color(int r, int g, int b, int a) { + } + + Color(int rgb) { + } + + Color(int rgba, boolean hasAlpha) { + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterHexA2Rgba.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterHexA2Rgba.java new file mode 100644 index 000000000000..70171b80b283 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterHexA2Rgba.java @@ -0,0 +1,21 @@ +// "Convert to 'new Color(37, 100, 120, 140)'" "true" + +package java.awt; + +class A { + private Color color = new Color(37, 100, 120, 140); +} + +class Color { + Color(int r, int g, int b) { + } + + Color(int r, int g, int b, int a) { + } + + Color(int rgb) { + } + + Color(int rgba, boolean hasAlpha) { + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterRgb2Hex.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterRgb2Hex.java new file mode 100644 index 000000000000..c46980af5d4b --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterRgb2Hex.java @@ -0,0 +1,21 @@ +// "Convert to 'new Color(0x256478)'" "true" + +package java.awt; + +class A { + private Color color = new Color(0x256478); +} + +class Color { + Color(int r, int g, int b) { + } + + Color(int r, int g, int b, int a) { + } + + Color(int rgb) { + } + + Color(int rgba, boolean hasAlpha) { + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterRgba2Hex.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterRgba2Hex.java new file mode 100644 index 000000000000..488f9401c649 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/afterRgba2Hex.java @@ -0,0 +1,21 @@ +// "Convert to 'new Color(0x2564788c,true)'" "true" + +package java.awt; + +class A { + private Color color = new Color(0x2564788c, true); +} + +class Color { + Color(int r, int g, int b) { + } + + Color(int r, int g, int b, int a) { + } + + Color(int rgb) { + } + + Color(int rgba, boolean hasAlpha) { + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeHex2Rgb.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeHex2Rgb.java new file mode 100644 index 000000000000..666394ed293b --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeHex2Rgb.java @@ -0,0 +1,21 @@ +// "Convert to 'new Color(37, 100, 120)'" "true" + +package java.awt; + +public class A { + private Color color = new Color(0x256478); +} + +class Color { + Color(int r, int g, int b) { + } + + Color(int r, int g, int b, int a) { + } + + Color(int rgb) { + } + + Color(int rgba, boolean hasAlpha) { + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeHexA2Rgba.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeHexA2Rgba.java new file mode 100644 index 000000000000..39762cdfd810 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeHexA2Rgba.java @@ -0,0 +1,21 @@ +// "Convert to 'new Color(37, 100, 120, 140)'" "true" + +package java.awt; + +class A { + private Color color = new Color(0x2564788c, true); +} + +class Color { + Color(int r, int g, int b) { + } + + Color(int r, int g, int b, int a) { + } + + Color(int rgb) { + } + + Color(int rgba, boolean hasAlpha) { + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeRgb2Hex.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeRgb2Hex.java new file mode 100644 index 000000000000..10f6bc144124 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeRgb2Hex.java @@ -0,0 +1,21 @@ +// "Convert to 'new Color(0x256478)'" "true" + +package java.awt; + +class A { + private Color color = new Color(37, 100, 120); +} + +class Color { + Color(int r, int g, int b) { + } + + Color(int r, int g, int b, int a) { + } + + Color(int rgb) { + } + + Color(int rgba, boolean hasAlpha) { + } +} diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeRgba2Hex.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeRgba2Hex.java new file mode 100644 index 000000000000..cfe125593699 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation/beforeRgba2Hex.java @@ -0,0 +1,21 @@ +// "Convert to 'new Color(0x2564788c,true)'" "true" + +package java.awt; + +class A { + private Color color = new Color(37, 100, 120, 140); +} + +class Color { + Color(int r, int g, int b) { + } + + Color(int r, int g, int b, int a) { + } + + Color(int rgb) { + } + + Color(int rgba, boolean hasAlpha) { + } +} diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ConvertColorRepresentationTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ConvertColorRepresentationTest.java new file mode 100644 index 000000000000..a13f0781fc32 --- /dev/null +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/ConvertColorRepresentationTest.java @@ -0,0 +1,29 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.codeInsight.daemon.quickFix; + +/** + * @author Danila Ponomarenko + */ +public class ConvertColorRepresentationTest extends LightQuickFix15TestCase { + + public void test() throws Exception { doAllTests(); } + + @Override + protected String getBasePath() { + return "/codeInsight/daemonCodeAnalyzer/quickFix/convertColorRepresentation"; + } +} diff --git a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties index 82d004cf780e..168a2c8ba2d0 100644 --- a/platform/platform-resources-en/src/messages/CodeInsightBundle.properties +++ b/platform/platform-resources-en/src/messages/CodeInsightBundle.properties @@ -207,6 +207,8 @@ intention.remove.literal.underscores=Remove underscores from literal intention.insert.literal.underscores=Insert underscores into literal intention.replace.cast.with.var.text=Replace ''{0}'' with ''{1}'' intention.replace.cast.with.var.family=Replace cast with variable +intention.convert.color.representation.text=Convert to ''new Color{0}'' +intention.convert.color.representation.family=Convert Color representation intention.create.test=Create Test diff --git a/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/after.java.template b/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/after.java.template new file mode 100644 index 000000000000..c96256042b95 --- /dev/null +++ b/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/after.java.template @@ -0,0 +1,3 @@ +class UiControl { + public static final Color COLOR = new Color(0x64788c); +} \ No newline at end of file diff --git a/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/before.java.template b/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/before.java.template new file mode 100644 index 000000000000..5648c0c0d891 --- /dev/null +++ b/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/before.java.template @@ -0,0 +1,3 @@ +class UiControl { + public static final Color COLOR = new Color(100, 120, 140); +} \ No newline at end of file diff --git a/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/description.html b/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/description.html new file mode 100644 index 000000000000..ba5ea54635bb --- /dev/null +++ b/resources-en/src/intentionDescriptions/ConvertColorRepresentationIntentionAction/description.html @@ -0,0 +1,5 @@ + + +This intention allows to convert between new Color(int rgb) and new Color(int r, int g, int b) constructor variants. + + diff --git a/resources/src/META-INF/IdeaPlugin.xml b/resources/src/META-INF/IdeaPlugin.xml index 171a4b03001d..362fd21552b9 100644 --- a/resources/src/META-INF/IdeaPlugin.xml +++ b/resources/src/META-INF/IdeaPlugin.xml @@ -704,6 +704,11 @@ Declaration + + com.intellij.codeInsight.intention.impl.ConvertColorRepresentationIntentionAction + Declaration + + com.intellij.codeInsight.intention.impl.ConvertAbsolutePathToRelativeIntentionAction Other