Skip to content

Commit bf5a3e7

Browse files
committed
Simplify "string" generation/cleanups by dropping pre 1.5
ECJ mandates 1.8+ and JDT UI converts compiler settings to that unconditionally so this code is simply non-reachable.
1 parent 21b6bc4 commit bf5a3e7

File tree

13 files changed

+46
-680
lines changed

13 files changed

+46
-680
lines changed

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/AbstractToStringGenerator.java

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2019 Mateusz Matela and others.
2+
* Copyright (c) 2008, 2024 Mateusz Matela and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -254,16 +254,9 @@ protected MethodDeclaration createHelperToStringMethod(boolean array) {
254254
final String lengthParamName= createNameSuggestion("len", NamingConventions.VK_PARAMETER); //$NON-NLS-1$
255255
final String maxLenParamName= createNameSuggestion(MAX_LEN_VARIABLE_NAME, NamingConventions.VK_PARAMETER);
256256
String paramName;
257-
String stringBuilderName;
258-
String stringBuilderTypeName;
259257

260-
if (fContext.is50orHigher()) {
261-
stringBuilderTypeName= "java.lang.StringBuilder"; //$NON-NLS-1$
262-
stringBuilderName= createNameSuggestion("builder", NamingConventions.VK_LOCAL); //$NON-NLS-1$
263-
} else {
264-
stringBuilderTypeName= "java.lang.StringBuffer"; //$NON-NLS-1$
265-
stringBuilderName= createNameSuggestion("buffer", NamingConventions.VK_LOCAL); //$NON-NLS-1$
266-
}
258+
String stringBuilderTypeName= "java.lang.StringBuilder"; //$NON-NLS-1$
259+
String stringBuilderName= createNameSuggestion("builder", NamingConventions.VK_LOCAL); //$NON-NLS-1$
267260

268261
//private arrayToString() {
269262
MethodDeclaration arrayToStringMethod= fAst.newMethodDeclaration();
@@ -288,13 +281,9 @@ protected MethodDeclaration createHelperToStringMethod(boolean array) {
288281
paramName= createNameSuggestion("collection", NamingConventions.VK_PARAMETER); //$NON-NLS-1$
289282
SingleVariableDeclaration param= fAst.newSingleVariableDeclaration();
290283
Type collectionType= fAst.newSimpleType(addImport("java.util.Collection")); //$NON-NLS-1$
291-
if (fContext.is50orHigher()) {
292-
ParameterizedType genericType= fAst.newParameterizedType(collectionType);
293-
genericType.typeArguments().add(fAst.newWildcardType());
294-
param.setType(genericType);
295-
} else {
296-
param.setType(collectionType);
297-
}
284+
ParameterizedType genericType= fAst.newParameterizedType(collectionType);
285+
genericType.typeArguments().add(fAst.newWildcardType());
286+
param.setType(genericType);
298287
param.setName(fAst.newSimpleName(paramName));
299288
arrayToStringMethod.parameters().add(param);
300289
}
@@ -406,13 +395,9 @@ protected MethodDeclaration createHelperToStringMethod(boolean array) {
406395
fragment.setInitializer(createMethodInvocation(paramName, "iterator", null)); //$NON-NLS-1$
407396
VariableDeclarationExpression vExpression= fAst.newVariableDeclarationExpression(fragment);
408397
SimpleType iteratorType= fAst.newSimpleType(addImport("java.util.Iterator")); //$NON-NLS-1$
409-
if (fContext.is50orHigher()) {
410-
ParameterizedType pType= fAst.newParameterizedType(iteratorType);
411-
pType.typeArguments().add(fAst.newWildcardType());
412-
vExpression.setType(pType);
413-
} else {
414-
vExpression.setType(iteratorType);
415-
}
398+
ParameterizedType pType= fAst.newParameterizedType(iteratorType);
399+
pType.typeArguments().add(fAst.newWildcardType());
400+
vExpression.setType(pType);
416401

417402
forStatement.initializers().add(vExpression);
418403

@@ -501,7 +486,7 @@ protected void checkNeedForHelperMethods() {
501486
}
502487
if (fContext.isCustomArray() && memberType.isArray()) {
503488
ITypeBinding componentType= memberType.getComponentType();
504-
if (componentType.isPrimitive() && (!fContext.is50orHigher() || (!fContext.is60orHigher() && fContext.isLimitItems()))) {
489+
if (componentType.isPrimitive() && (!fContext.is60orHigher() && fContext.isLimitItems())) {
505490
if (!typesThatNeedArrayToStringMethod.contains(componentType))
506491
typesThatNeedArrayToStringMethod.add(componentType);
507492
} else if (!componentType.isPrimitive())
@@ -758,28 +743,8 @@ protected Expression createMemberAccessExpression(Object member, boolean ignoreA
758743
}
759744
} else {
760745
if (isArray && fContext.isCustomArray()) {
761-
if (fContext.is50orHigher()) {
762-
// Arrays.toString(member)
763-
return createMethodInvocation(addImport("java.util.Arrays"), METHODNAME_TO_STRING, createMemberAccessExpression(member, true, true)); //$NON-NLS-1$
764-
} else {
765-
ITypeBinding arrayComponentType= memberType.getComponentType();
766-
if (!arrayComponentType.isPrimitive() && typesThatNeedArrayToStringMethod.isEmpty()) {
767-
// Arrays.asList(member)
768-
accessExpression= createMethodInvocation(addImport("java.util.Arrays"), "asList", createMemberAccessExpression(member, true, true)); //$NON-NLS-1$ //$NON-NLS-2$
769-
} else {
770-
// arrayToString(member, member.length, maxLen)
771-
FieldAccess lengthAccess= fAst.newFieldAccess();
772-
lengthAccess.setExpression(createMemberAccessExpression(member, true, true));
773-
lengthAccess.setName(fAst.newSimpleName("length")); //$NON-NLS-1$
774-
MethodInvocation arrayToStringInvocation= fAst.newMethodInvocation();
775-
if (fContext.isKeywordThis())
776-
arrayToStringInvocation.setExpression(fAst.newThisExpression());
777-
arrayToStringInvocation.setName(fAst.newSimpleName(HELPER_ARRAYTOSTRING_METHOD_NAME));
778-
arrayToStringInvocation.arguments().add(createMemberAccessExpression(member, true, true));
779-
arrayToStringInvocation.arguments().add(lengthAccess);
780-
accessExpression= arrayToStringInvocation;
781-
}
782-
}
746+
// Arrays.toString(member)
747+
return createMethodInvocation(addImport("java.util.Arrays"), METHODNAME_TO_STRING, createMemberAccessExpression(member, true, true)); //$NON-NLS-1$
783748
}
784749
}
785750
if (accessExpression != null) {

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/CustomBuilderGenerator.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2019 Mateusz Matela and others.
2+
* Copyright (c) 2008, 2024 Mateusz Matela and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -273,19 +273,11 @@ private MethodInvocation createAppendMethodForMember(Object member) {
273273
ITypeBinding memberType= getMemberType(member);
274274
String memberTypeName= memberType.getQualifiedName();
275275

276-
Expression memberAccessExpression= null;
277-
278276
AppendMethodInformation ami= appendMethodSpecificTypes.get(memberTypeName);
279277
if (ami == null && memberType.isPrimitive()) {
280278
memberTypeName= wrapperTypes[primitiveTypes.indexOf(memberTypeName)];
281279
memberType= fAst.resolveWellKnownType(memberTypeName);
282280
ami= appendMethodSpecificTypes.get(memberTypeName);
283-
if (!getContext().is50orHigher()) {
284-
ClassInstanceCreation classInstance= fAst.newClassInstanceCreation();
285-
classInstance.setType(fAst.newSimpleType(addImport(memberTypeName)));
286-
classInstance.arguments().add(createMemberAccessExpression(member, true, true));
287-
memberAccessExpression= classInstance;
288-
}
289281
}
290282
while (ami == null) {
291283
ITypeBinding oldMemberType= memberType;
@@ -297,9 +289,7 @@ private MethodInvocation createAppendMethodForMember(Object member) {
297289
ami= appendMethodSpecificTypes.get(memberTypeName);
298290
}
299291

300-
if (memberAccessExpression == null) {
301-
memberAccessExpression= createMemberAccessExpression(member, false, getContext().isSkipNulls());
302-
}
292+
Expression memberAccessExpression= createMemberAccessExpression(member, false, getContext().isSkipNulls());
303293

304294
MethodInvocation appendInvocation= fAst.newMethodInvocation();
305295
appendInvocation.setName(fAst.newSimpleName(getContext().getCustomBuilderAppendMethod()));

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/StringBuilderGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2019 Mateusz Matela and others.
2+
* Copyright (c) 2008, 2024 Mateusz Matela and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -74,12 +74,12 @@ protected void flushBuffer(Block target) {
7474
@Override
7575
protected void initialize() {
7676
super.initialize();
77-
fBuilderVariableName= createNameSuggestion(getContext().is50orHigher() ? "builder" : "buffer", NamingConventions.VK_LOCAL); //$NON-NLS-1$ //$NON-NLS-2$
77+
fBuilderVariableName= createNameSuggestion("builder", NamingConventions.VK_LOCAL); //$NON-NLS-1$
7878
fBuffer= new StringBuffer();
7979
VariableDeclarationFragment fragment= fAst.newVariableDeclarationFragment();
8080
fragment.setName(fAst.newSimpleName(fBuilderVariableName));
8181
ClassInstanceCreation classInstance= fAst.newClassInstanceCreation();
82-
Name typeName= addImport(getContext().is50orHigher() ? "java.lang.StringBuilder" : "java.lang.StringBuffer"); //$NON-NLS-1$ //$NON-NLS-2$
82+
Name typeName= addImport("java.lang.StringBuilder"); //$NON-NLS-1$
8383
classInstance.setType(fAst.newSimpleType(typeName));
8484
fragment.setInitializer(classInstance);
8585
VariableDeclarationStatement vStatement= fAst.newVariableDeclarationStatement(fragment);

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/StringFormatGenerator.java

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2019 Mateusz Matela and others.
2+
* Copyright (c) 2008, 2024 Mateusz Matela and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -20,11 +20,7 @@
2020

2121
import org.eclipse.core.runtime.CoreException;
2222

23-
import org.eclipse.jdt.core.dom.ArrayCreation;
24-
import org.eclipse.jdt.core.dom.ArrayInitializer;
25-
import org.eclipse.jdt.core.dom.ClassInstanceCreation;
2623
import org.eclipse.jdt.core.dom.Expression;
27-
import org.eclipse.jdt.core.dom.ITypeBinding;
2824
import org.eclipse.jdt.core.dom.MethodInvocation;
2925
import org.eclipse.jdt.core.dom.ReturnStatement;
3026
import org.eclipse.jdt.core.dom.StringLiteral;
@@ -61,25 +57,12 @@ protected void initialize() {
6157
protected void complete() throws CoreException {
6258
super.complete();
6359
ReturnStatement rStatement= fAst.newReturnStatement();
64-
String formatClass;
65-
if (getContext().is50orHigher())
66-
formatClass= "java.lang.String"; //$NON-NLS-1$
67-
else
68-
formatClass= "java.text.MessageFormat"; //$NON-NLS-1$
60+
String formatClass= "java.lang.String"; //$NON-NLS-1$
6961
MethodInvocation formatInvocation= createMethodInvocation(addImport(formatClass), "format", null); //$NON-NLS-1$
7062
StringLiteral literal= fAst.newStringLiteral();
7163
literal.setLiteralValue(buffer.toString());
7264
formatInvocation.arguments().add(literal);
73-
if (getContext().is50orHigher()) {
74-
formatInvocation.arguments().addAll(arguments);
75-
} else {
76-
ArrayCreation arrayCreation= fAst.newArrayCreation();
77-
arrayCreation.setType(fAst.newArrayType(fAst.newSimpleType(addImport("java.lang.Object")))); //$NON-NLS-1$
78-
ArrayInitializer initializer= fAst.newArrayInitializer();
79-
arrayCreation.setInitializer(initializer);
80-
initializer.expressions().addAll(arguments);
81-
formatInvocation.arguments().add(arrayCreation);
82-
}
65+
formatInvocation.arguments().addAll(arguments);
8366
rStatement.setExpression(formatInvocation);
8467
toStringMethod.getBody().statements().add(rStatement);
8568
}
@@ -99,34 +82,8 @@ protected void addElement(Object element) {
9982
}
10083
if (element instanceof Expression) {
10184
arguments.add((Expression) element);
102-
if (getContext().is50orHigher()) {
103-
buffer.append("%s"); //$NON-NLS-1$
104-
} else {
105-
buffer.append("{" + (arguments.size() - 1) + "}"); //$NON-NLS-1$ //$NON-NLS-2$
106-
}
107-
}
108-
}
109-
110-
@Override
111-
protected Expression createMemberAccessExpression(Object member, boolean ignoreArraysCollections, boolean ignoreNulls) {
112-
ITypeBinding type= getMemberType(member);
113-
if (!getContext().is50orHigher() && type.isPrimitive()) {
114-
String nonPrimitiveType= null;
115-
String typeName= type.getName();
116-
if ("byte".equals(typeName))nonPrimitiveType= "java.lang.Byte"; //$NON-NLS-1$ //$NON-NLS-2$
117-
if ("short".equals(typeName))nonPrimitiveType= "java.lang.Short"; //$NON-NLS-1$ //$NON-NLS-2$
118-
if ("char".equals(typeName))nonPrimitiveType= "java.lang.Character"; //$NON-NLS-1$ //$NON-NLS-2$
119-
if ("int".equals(typeName))nonPrimitiveType= "java.lang.Integer"; //$NON-NLS-1$ //$NON-NLS-2$
120-
if ("long".equals(typeName))nonPrimitiveType= "java.lang.Long"; //$NON-NLS-1$ //$NON-NLS-2$
121-
if ("float".equals(typeName))nonPrimitiveType= "java.lang.Float"; //$NON-NLS-1$ //$NON-NLS-2$
122-
if ("double".equals(typeName))nonPrimitiveType= "java.lang.Double"; //$NON-NLS-1$ //$NON-NLS-2$
123-
if ("boolean".equals(typeName))nonPrimitiveType= "java.lang.Boolean"; //$NON-NLS-1$ //$NON-NLS-2$
124-
ClassInstanceCreation classInstance= fAst.newClassInstanceCreation();
125-
classInstance.setType(fAst.newSimpleType(addImport(nonPrimitiveType)));
126-
classInstance.arguments().add(super.createMemberAccessExpression(member, true, true));
127-
return classInstance;
85+
buffer.append("%s"); //$NON-NLS-1$
12886
}
129-
return super.createMemberAccessExpression(member, ignoreArraysCollections, ignoreNulls);
13087
}
13188

13289
}

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/ToStringGenerationContext.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2008, 2019 Mateusz Matela and others.
2+
* Copyright (c) 2008, 2024 Mateusz Matela and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -81,10 +81,6 @@ public ITypeBinding getTypeBinding() {
8181
return fType;
8282
}
8383

84-
public boolean is50orHigher() {
85-
return fSettings.is50orHigher;
86-
}
87-
8884
public boolean is60orHigher() {
8985
return fSettings.is60orHigher;
9086
}

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/codemanipulation/tostringgeneration/ToStringGenerationSettingsCore.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2019 Mateusz Matela and others.
2+
* Copyright (c) 2019, 2024 Mateusz Matela and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -99,9 +99,6 @@ public static class CustomBuilderSettings {
9999
/** should blocks be forced in if/for/while statements? */
100100
public boolean useBlocks;
101101

102-
/** can generated code use jdk 1.5 API? **/
103-
public boolean is50orHigher;
104-
105102
/** can generated code use jdk 1.6 API? **/
106103
public boolean is60orHigher;
107104

org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/ConvertToMessageFormatFixCore.java

Lines changed: 5 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
import org.eclipse.jdt.internal.core.manipulation.dom.ASTResolving;
4747
import org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext;
4848
import org.eclipse.jdt.internal.corext.dom.ASTNodes;
49-
import org.eclipse.jdt.internal.corext.dom.Bindings;
5049
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSElement;
5150
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSLine;
5251
import org.eclipse.jdt.internal.corext.refactoring.nls.NLSScanner;
@@ -97,7 +96,6 @@ public static ConvertToMessageFormatFixCore createConvertToMessageFormatFix(Comp
9796
return null;
9897
}
9998

100-
boolean is50OrHigher= JavaModelUtil.is50OrHigher(compilationUnit.getTypeRoot().getJavaProject());
10199
// collect operands
102100
List<Expression> operands= new ArrayList<>();
103101
collectInfixPlusOperands(oldInfixExpression, operands);
@@ -108,12 +106,6 @@ public static ConvertToMessageFormatFixCore createConvertToMessageFormatFix(Comp
108106
// we need to loop through all to exclude any null binding scenarios.
109107
for (Expression operand : operands) {
110108
if (!(operand instanceof StringLiteral)) {
111-
if (!is50OrHigher) {
112-
ITypeBinding binding= operand.resolveTypeBinding();
113-
if (binding == null) {
114-
return null;
115-
}
116-
}
117109
foundNoneLiteralOperand= true;
118110
} else {
119111
// ensure either all string literals are nls-tagged or none are
@@ -218,9 +210,7 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
218210
final List<String> fLiterals= new ArrayList<>();
219211
String fIndent= ""; //$NON-NLS-1$
220212
ICompilationUnit cu= cuRewrite.getCu();
221-
boolean is50OrHigher= JavaModelUtil.is50OrHigher(cu.getJavaProject());
222213
boolean is15OrHigher= JavaModelUtil.is15OrHigher(cu.getJavaProject());
223-
AST fAst= cuRewrite.getAST();
224214

225215
ASTRewrite rewrite= cuRewrite.getASTRewrite();
226216
CompilationUnit root= cuRewrite.getRoot();
@@ -280,26 +270,9 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
280270
fLiterals.add("\"{" + Integer.toString(i) + "}\""); //$NON-NLS-1$ //$NON-NLS-2$
281271
formatString.append("{").append(i).append("}"); //$NON-NLS-1$ //$NON-NLS-2$
282272

283-
String argument;
284-
if (is50OrHigher) {
285-
int origStart= root.getExtendedStartPosition(operand);
286-
int origLength= root.getExtendedLength(operand);
287-
argument= cuContents.substring(origStart, origStart + origLength);
288-
} else {
289-
ITypeBinding binding= operand.resolveTypeBinding();
290-
int origStart= root.getExtendedStartPosition(operand);
291-
int origLength= root.getExtendedLength(operand);
292-
argument= cuContents.substring(origStart, origStart + origLength);
293-
294-
if (binding.isPrimitive()) {
295-
ITypeBinding boxedBinding= Bindings.getBoxedTypeBinding(binding, fAst);
296-
if (boxedBinding != binding) {
297-
importRewrite.addImport(boxedBinding, fAst, importContext);
298-
String cic= "new " + boxedBinding.getName() + "(" + argument + ")"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
299-
argument= cic;
300-
}
301-
}
302-
}
273+
int origStart= root.getExtendedStartPosition(operand);
274+
int origLength= root.getExtendedLength(operand);
275+
String argument= cuContents.substring(origStart, origStart + origLength);
303276

304277
formatArguments.add(argument);
305278
i++;
@@ -370,19 +343,8 @@ public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModelCore
370343
buffer.append("\"" + formatString.toString().replaceAll("\"", "\\\"") + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
371344
}
372345

373-
if (is50OrHigher) {
374-
for (String formatArgument : formatArguments) {
375-
buffer.append(", " + formatArgument); //$NON-NLS-1$
376-
}
377-
} else {
378-
buffer.append(", new Object[]{"); //$NON-NLS-1$
379-
if (formatArguments.size() > 0) {
380-
buffer.append(formatArguments.get(0));
381-
}
382-
for (int i1= 1; i1 < formatArguments.size(); ++i1) {
383-
buffer.append(", " + formatArguments.get(i1)); //$NON-NLS-1$
384-
}
385-
buffer.append("}"); //$NON-NLS-1$
346+
for (String formatArgument : formatArguments) {
347+
buffer.append(", " + formatArgument); //$NON-NLS-1$
386348
}
387349
buffer.append(")"); //$NON-NLS-1$
388350

0 commit comments

Comments
 (0)