Skip to content

Commit

Permalink
change formatting and move cleanup to codestyle page
Browse files Browse the repository at this point in the history
  • Loading branch information
carstenartur committed Oct 9, 2024
1 parent 174ae8b commit 5488280
Show file tree
Hide file tree
Showing 25 changed files with 133 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,5 @@ StringBufferToStringBuilderCleanUp_description=Convert StringBuffer to StringBui
StringConcatToTextBlockCleanUp_description=Convert String concatenation to Text Block
StringConcatToTextBlockStringBuffer_description=Convert String/StringBuffer/StringBuilder concatenation to Text Block
StringBuilderForLocalVarsOnlyCleanUp_description=Convert StringBuffer to StringBuilder for local variables
ExplicitEncodingCleanUp_description=Fix reliance on default encoding ''{0}'' using {1}
ExplicitEncodingCleanUp_description=Set explicit encoding or default encoding where applicable on methods ''{0}'' using {1}
ExplicitEncodingCleanUpFix_refactor=Use explicit encoding
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public ICleanUpFix createFix(final CleanUpContext context) throws CoreException
}

private ChangeBehavior computeRefactorDeepth() {
ChangeBehavior cb=ChangeBehavior.KEEP;
ChangeBehavior cb=ChangeBehavior.KEEP_BEHAVIOR;
if(isEnabled(EXPLICITENCODING_KEEP_BEHAVIOR)) {
cb=ChangeBehavior.KEEP;
cb=ChangeBehavior.KEEP_BEHAVIOR;
}
if(isEnabled(EXPLICITENCODING_INSERT_UTF8)) {
cb=ChangeBehavior.USE_UTF8;
cb=ChangeBehavior.ENFORCE_UTF8;
}
if(isEnabled(EXPLICITENCODING_AGGREGATE_TO_UTF8)) {
cb=ChangeBehavior.USE_UTF8_AGGREGATE;
cb=ChangeBehavior.ENFORCE_UTF8_AGGREGATE;
}
return cb;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ public SourceRange computeSourceRange(final ASTNode nodeWithComment) {
return super.computeSourceRange(nodeWithComment);
}
};

@Override
public String toString() {
return explicitencoding.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public abstract class AbstractExplicitEncoding<T extends ASTNode> {
"US-ASCII", "US_ASCII" //$NON-NLS-1$ //$NON-NLS-2$
);
static Set<String> encodings=encodingmap.keySet();
public enum ChangeBehavior {KEEP, USE_UTF8, USE_UTF8_AGGREGATE}
public enum ChangeBehavior {KEEP_BEHAVIOR, ENFORCE_UTF8, ENFORCE_UTF8_AGGREGATE}

static class Nodedata {
public boolean replace;
Expand All @@ -72,19 +72,19 @@ public abstract void rewrite(UseExplicitEncodingFixCore useExplicitEncodingFixCo
protected static Expression computeCharsetASTNode(final CompilationUnitRewrite cuRewrite, AST ast, ChangeBehavior cb, String charset) {
Expression callToCharsetDefaultCharset=null;
switch(cb) {
case KEEP:
case KEEP_BEHAVIOR:
if(charset!=null) {
callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast,charset);
} else {
// needs Java 1.5
callToCharsetDefaultCharset= addCharsetComputation(cuRewrite, ast);
}
break;
case USE_UTF8_AGGREGATE:
case ENFORCE_UTF8_AGGREGATE:
/**
* @TODO not implemented
*/
case USE_UTF8:
case ENFORCE_UTF8:
// needs Java 1.7
callToCharsetDefaultCharset= addCharsetUTF8(cuRewrite, ast,charset);
break;
Expand All @@ -95,13 +95,13 @@ protected static Expression computeCharsetASTNode(final CompilationUnitRewrite c
protected static String computeCharsetforPreview(ChangeBehavior cb) {
String insert=""; //$NON-NLS-1$
switch(cb) {
case KEEP:
case KEEP_BEHAVIOR:
insert="Charset.defaultCharset()"; //$NON-NLS-1$
break;
case USE_UTF8_AGGREGATE:
case ENFORCE_UTF8_AGGREGATE:
// insert="charset_constant"; //$NON-NLS-1$
//$FALL-THROUGH$
case USE_UTF8:
case ENFORCE_UTF8:
insert="StandardCharsets.UTF_8"; //$NON-NLS-1$
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
String insert=""; //$NON-NLS-1$
switch(cb) {
case KEEP:
case KEEP_BEHAVIOR:
insert="Charset.defaultCharset().displayName()"; //$NON-NLS-1$
break;
case USE_UTF8_AGGREGATE:
case ENFORCE_UTF8_AGGREGATE:
// insert="charset_constant"; //$NON-NLS-1$
//$FALL-THROUGH$
case USE_UTF8:
case ENFORCE_UTF8:
insert="StandardCharsets.UTF_8.displayName()"; //$NON-NLS-1$
break;
}
Expand All @@ -153,4 +153,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
"""; //$NON-NLS-1$
}

@Override
public String toString() {
return "ba.toString()"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
return "Reader r=Channels.newReader(ch,\"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}

@Override
public String toString() {
return "Channels.newReader(ch,StandardCharsets.UTF_8)"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
return "Writer w=Channels.newWriter(ch, \"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}

@Override
public String toString() {
return "Channels.newWriter(ch,StandardCharsets.UTF_8)"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,13 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Charset s=StandardCharsets.UTF_8;\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
return "Charset s=StandardCharsets.UTF_8;\n"; //$NON-NLS-1$
}
return "Charset s=Charset.forName(\"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
return "Charset s=Charset.forName(\"UTF-8\");\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "Charset.forName(\"UTF-8\")"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
return "Reader r=new FileReader(inputfile);\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "new FileReader(inputfile)"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
return "Writer w=new FileWriter(outputfile);\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "new FileWriter(outputfile)"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
return "Formatter r=new java.util.Formatter(out);\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "new java.util.Formatter(out)"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
return "Reader r=new InputStreamReader(in);\nInputStreamReader is=new InputStreamReader(new FileInputStream(\"\"), StandardCharsets.UTF_8);\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "new InputStreamReader(in)"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
return "Writer w = new OutputStreamWriter(out);\nOutputStreamWriter os=new OutputStreamWriter(new FileOutputStream(\"\"), \"UTF-8\");\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "new OutputStreamWriter(out)"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
Stream w=new PrintStream(new File("out.txt"),"UTF-8");
"""; //$NON-NLS-1$
}

@Override
public String toString() {
return "new PrintStream(\"out.txt\")"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
return "Writer w=new PrintWriter(outputfile);\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "new PrintWriter(outputfile)"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,14 @@ public void rewrite(UseExplicitEncodingFixCore upp,final MethodInvocation visite
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "Properties p=new Properties();\n"+ //$NON-NLS-1$
"p.storeToXML(java.io.OutputStream,String,"+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$
"p.storeToXML(java.io.OutputStream,String,StandardCharsets.UTF_8);\n"; //$NON-NLS-1$
}
return "Properties p=new Properties();\n"+ //$NON-NLS-1$
"p.storeToXML(java.io.OutputStream,String,\"UTF-8\");\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "Properties.storeToXML()"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,13 @@ public void rewrite(UseExplicitEncodingFixCore upp,final ClassInstanceCreation v
@Override
public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
if(afterRefactoring) {
return "new java.util.Scanner(\"asdf\","+computeCharsetforPreview(cb)+");\n"; //$NON-NLS-1$ //$NON-NLS-2$
return "new java.util.Scanner(\"asdf\",StandardCharsets.UTF_8);\n"; //$NON-NLS-1$
}
return "new java.util.Scanner(\"asdf\", \"UTF-8\");\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "new java.util.Scanner()"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,4 +128,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
}
return "String s=new String(byte[],\"UTF-8\");\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "new String(byte[])"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
return "String s=\"asdf\";\n"+ //$NON-NLS-1$
"byte[] bytes= s.getBytes();\n"; //$NON-NLS-1$
}

@Override
public String toString() {
return "String.getBytes()"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
if (ASTNodes.usesGivenSignature(visited, URLDecoder.class.getCanonicalName(), METHOD_DECODE, String.class.getCanonicalName())) {
Nodedata nd=new Nodedata();
switch(cb) {
case KEEP:
case KEEP_BEHAVIOR:
nd.encoding=null;
break;
case USE_UTF8:
case ENFORCE_UTF8:
nd.encoding="UTF_8"; //$NON-NLS-1$
break;
case USE_UTF8_AGGREGATE:
case ENFORCE_UTF8_AGGREGATE:
break;
}
nd.replace=false;
Expand Down Expand Up @@ -137,4 +137,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
return "java.net.URLDecoder.decode(\"asdf\", \"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}

@Override
public String toString() {
return "URLDecoder.decode()"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ private static boolean processFoundNode(UseExplicitEncodingFixCore fixcore,
if (ASTNodes.usesGivenSignature(visited, URLEncoder.class.getCanonicalName(), METHOD_ENCODE, String.class.getCanonicalName())) {
Nodedata nd=new Nodedata();
switch(cb) {
case KEEP:
case KEEP_BEHAVIOR:
nd.encoding=null;
break;
case USE_UTF8:
case ENFORCE_UTF8:
nd.encoding="UTF_8"; //$NON-NLS-1$
break;
case USE_UTF8_AGGREGATE:
case ENFORCE_UTF8_AGGREGATE:
break;
}
nd.replace=false;
Expand Down Expand Up @@ -137,4 +137,9 @@ public String getPreview(boolean afterRefactoring,ChangeBehavior cb) {
return "java.net.URLEncoder.encode(\"asdf\", \"UTF-8\");\n"+ //$NON-NLS-1$
""; //$NON-NLS-1$
}

@Override
public String toString() {
return "URLEncoder.encode()"; //$NON-NLS-1$
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public class CleanUpMessages extends NLS {

public static String JavaFeatureTabPage_CheckboxName_StringBufferBuilderToTextBlock;

public static String JavaFeatureTabPage_GroupName_Java1d6;
public static String JavaFeatureTabPage_GroupName_Encoding;
public static String JavaFeatureTabPage_CheckboxName_ExplicitEncoding;
public static String JavaFeatureTabPage_RadioName_Keep_Behavior;
public static String JavaFeatureTabPage_RadioName_Insert_UTF8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ JavaFeatureTabPage_CheckboxName_ConstantsForSystemProperty_PathSeparator=Path se
JavaFeatureTabPage_CheckboxName_ConstantsForSystemProperty_JavaVersionProperty=Java Version property
JavaFeatureTabPage_CheckboxName_ConstantsForSystemProperty_JavaVersionSpecProperty=Java Spec Version property

JavaFeatureTabPage_CheckboxName_ExplicitEncoding=Fix reliance on default encoding
JavaFeatureTabPage_CheckboxName_ExplicitEncoding=Set explicit encoding
JavaFeatureTabPage_RadioName_Aggregate_to_UTF8=Insert/replace to make use of UTF-8 instead of platform encoding.\n Try to use single constant per project.
JavaFeatureTabPage_RadioName_Insert_UTF8=Insert/replace to make use of UTF-8 instead of platform encoding.
JavaFeatureTabPage_RadioName_Keep_Behavior=Don't change behavior - just insert/replace code to make usage of platform encoding visible.
JavaFeatureTabPage_GroupName_Java1d6=Java 6
JavaFeatureTabPage_GroupName_Encoding=Encoding
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.eclipse.jdt.internal.ui.fix.ReduceIndentationCleanUp;
import org.eclipse.jdt.internal.ui.fix.SimplifyBooleanIfElseCleanUpCore;
import org.eclipse.jdt.internal.ui.fix.SwitchCleanUpCore;
import org.eclipse.jdt.internal.ui.fix.UseExplicitEncodingCleanUp;
import org.eclipse.jdt.internal.ui.fix.VariableDeclarationCleanUpCore;

public final class CodeStyleTabPage extends AbstractCleanUpTabPage {
Expand All @@ -53,7 +54,8 @@ protected AbstractCleanUp[] createPreviewCleanUps(Map<String, String> values) {
new InstanceofCleanUp(values),
new VariableDeclarationCleanUpCore(values),
new SimplifyBooleanIfElseCleanUpCore(values),
new LambdaExpressionAndMethodRefCleanUp(values)
new LambdaExpressionAndMethodRefCleanUp(values),
new UseExplicitEncodingCleanUp(values)
};
}

Expand Down Expand Up @@ -122,5 +124,16 @@ protected void doCreatePreferences(Composite composite, int numColumns) {

CheckboxPreference simplifyLambdaExpressionAndMethodRef= createCheckboxPref(functionalInterfacesGroup, numColumns, CleanUpMessages.CodeStyleTabPage_CheckboxName_SimplifyLambdaExpressionAndMethodRefSyntax, CleanUpConstants.SIMPLIFY_LAMBDA_EXPRESSION_AND_METHOD_REF, CleanUpModifyDialog.FALSE_TRUE);
registerPreference(simplifyLambdaExpressionAndMethodRef);

Group encodingGroup= createGroup(numColumns, composite, CleanUpMessages.JavaFeatureTabPage_GroupName_Encoding);
final CheckboxPreference explicit_encoding= createCheckboxPref(encodingGroup, numColumns, CleanUpMessages.JavaFeatureTabPage_CheckboxName_ExplicitEncoding, CleanUpConstants.EXPLICITENCODING_CLEANUP, CleanUpModifyDialog.FALSE_TRUE);
intent(encodingGroup);
final RadioPreference useKeep_Behavior= createRadioPref(encodingGroup, numColumns - 1, CleanUpMessages.JavaFeatureTabPage_RadioName_Keep_Behavior, CleanUpConstants.EXPLICITENCODING_KEEP_BEHAVIOR, CleanUpModifyDialog.FALSE_TRUE);
intent(encodingGroup);
final RadioPreference useINSERT_UTF8= createRadioPref(encodingGroup, numColumns - 1, CleanUpMessages.JavaFeatureTabPage_RadioName_Insert_UTF8, CleanUpConstants.EXPLICITENCODING_INSERT_UTF8, CleanUpModifyDialog.FALSE_TRUE);
intent(encodingGroup);
final RadioPreference useAGGREGATE_TO_UTF8= createRadioPref(encodingGroup, numColumns - 1, CleanUpMessages.JavaFeatureTabPage_RadioName_Aggregate_to_UTF8, CleanUpConstants.EXPLICITENCODING_AGGREGATE_TO_UTF8, CleanUpModifyDialog.FALSE_TRUE);
registerSlavePreference(explicit_encoding, new RadioPreference[] {useKeep_Behavior, useINSERT_UTF8, useAGGREGATE_TO_UTF8});

}
}
Loading

0 comments on commit 5488280

Please sign in to comment.