Skip to content

Commit 29d00bd

Browse files
author
Vincent Potucek
committed
fix 'StringBuilder buffer' can be replaced with 'String'
1 parent a410aa6 commit 29d00bd

File tree

94 files changed

+350
-237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+350
-237
lines changed

compat/maven-artifact/src/main/java/org/apache/maven/artifact/repository/ArtifactRepositoryPolicy.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,17 @@ public boolean checkOutOfDate(Date lastModified) {
132132

133133
@Override
134134
public String toString() {
135-
return "{enabled=" + enabled + ", checksums=" + checksumPolicy + ", updates=" + updatePolicy + '}';
135+
String buffer = "{enabled=" +
136+
enabled +
137+
", checksums=" +
138+
checksumPolicy +
139+
", updates=" +
140+
updatePolicy +
141+
'}';
142+
return buffer;
136143
}
137144

138-
public ArtifactRepositoryPolicy merge(ArtifactRepositoryPolicy policy) {
145+
public void merge(ArtifactRepositoryPolicy policy) {
139146
if (policy != null && policy.isEnabled()) {
140147
setEnabled(true);
141148

@@ -147,7 +154,6 @@ public ArtifactRepositoryPolicy merge(ArtifactRepositoryPolicy policy) {
147154
setUpdatePolicy(policy.getUpdatePolicy());
148155
}
149156
}
150-
return policy;
151157
}
152158

153159
private int ordinalOfCksumPolicy(String policy) {

compat/maven-compat/src/main/java/org/apache/maven/artifact/repository/layout/FlatRepositoryLayout.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,8 @@ public String pathOfLocalRepositoryMetadata(ArtifactMetadata metadata, ArtifactR
6666
}
6767

6868
private String pathOfRepositoryMetadata(String filename) {
69-
StringBuilder path = new StringBuilder(128);
70-
71-
path.append(filename);
7269

73-
return path.toString();
70+
return filename;
7471
}
7572

7673
public String pathOfRemoteRepositoryMetadata(ArtifactMetadata metadata) {

compat/maven-compat/src/main/java/org/apache/maven/artifact/repository/metadata/AbstractRepositoryMetadata.java

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,13 @@
3333
import org.apache.maven.metadata.v4.MetadataStaxReader;
3434
import org.apache.maven.metadata.v4.MetadataStaxWriter;
3535

36-
import static java.lang.System.lineSeparator;
37-
import static org.apache.maven.artifact.Artifact.LATEST_VERSION;
38-
import static org.apache.maven.artifact.Artifact.RELEASE_VERSION;
39-
4036
/**
4137
* Shared methods of the repository metadata handling.
4238
*
4339
*/
4440
@Deprecated
4541
public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
42+
private static final String LS = System.lineSeparator();
4643

4744
private Metadata metadata;
4845

@@ -69,37 +66,51 @@ public void storeInLocalRepository(ArtifactRepository localRepository, ArtifactR
6966

7067
protected void updateRepositoryMetadata(ArtifactRepository localRepository, ArtifactRepository remoteRepository)
7168
throws IOException, XMLStreamException {
69+
Metadata metadata = null;
70+
7271
File metadataFile = new File(
7372
localRepository.getBasedir(), localRepository.pathOfLocalRepositoryMetadata(this, remoteRepository));
74-
Metadata metadata = this.metadata;
75-
if (metadataFile.exists()) {
76-
try (InputStream input = Files.newInputStream(metadataFile.toPath())) {
77-
metadata = new Metadata(new MetadataStaxReader().read(input, false));
78-
}
79-
} else if (metadataFile.length() == 0) {
73+
74+
if (metadataFile.length() == 0) {
8075
if (!metadataFile.delete()) {
8176
// sleep for 10ms just in case this is windows holding a file lock
8277
try {
8378
Thread.sleep(10);
84-
} catch (InterruptedException ignore) {
79+
} catch (InterruptedException e) {
80+
// ignore
8581
}
8682
metadataFile.delete(); // if this fails, forget about it, we'll try to overwrite it anyway so no need
8783
// to delete on exit
8884
}
85+
} else if (metadataFile.exists()) {
86+
try (InputStream input = Files.newInputStream(metadataFile.toPath())) {
87+
metadata = new Metadata(new MetadataStaxReader().read(input, false));
88+
}
89+
}
90+
91+
boolean changed;
92+
93+
// If file could not be found or was not valid, start from scratch
94+
if (metadata == null) {
95+
metadata = this.metadata;
96+
97+
changed = true;
98+
} else {
99+
changed = metadata.merge(this.metadata);
89100
}
90-
writeMetadata(metadata, metadataFile);
91-
}
92101

93-
private void writeMetadata(Metadata metadata, File metadataFile) throws IOException, XMLStreamException {
94102
// beware meta-versions!
95-
if (LATEST_VERSION.equals(metadata.getVersion()) || RELEASE_VERSION.equals(metadata.getVersion())) {
103+
String version = metadata.getVersion();
104+
if (Artifact.LATEST_VERSION.equals(version) || Artifact.RELEASE_VERSION.equals(version)) {
96105
// meta-versions are not valid <version/> values...don't write them.
97106
metadata.setVersion(null);
98107
}
99-
if (metadata.merge(this.metadata) || !metadataFile.exists()) {
108+
109+
if (changed || !metadataFile.exists()) {
100110
metadataFile.getParentFile().mkdirs();
101111
try (OutputStream output = Files.newOutputStream(metadataFile.toPath())) {
102-
new MetadataStaxWriter().write(output, metadata.getDelegate());
112+
MetadataStaxWriter mappingWriter = new MetadataStaxWriter();
113+
mappingWriter.write(output, metadata.getDelegate());
103114
}
104115
} else {
105116
metadataFile.setLastModified(System.currentTimeMillis());
@@ -136,21 +147,25 @@ public Metadata getMetadata() {
136147
public void merge(org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata) {
137148
// TODO not sure that it should assume this, maybe the calls to addMetadata should pre-merge, then artifact
138149
// replaces?
139-
this.metadata.merge(((AbstractRepositoryMetadata) metadata).getMetadata());
150+
AbstractRepositoryMetadata repoMetadata = (AbstractRepositoryMetadata) metadata;
151+
this.metadata.merge(repoMetadata.getMetadata());
140152
}
141153

142154
public void merge(ArtifactMetadata metadata) {
143155
// TODO not sure that it should assume this, maybe the calls to addMetadata should pre-merge, then artifact
144156
// replaces?
145-
this.metadata.merge(((AbstractRepositoryMetadata) metadata).getMetadata());
157+
AbstractRepositoryMetadata repoMetadata = (AbstractRepositoryMetadata) metadata;
158+
this.metadata.merge(repoMetadata.getMetadata());
146159
}
147160

148161
public String extendedToString() {
149-
return lineSeparator() + "Repository Metadata" + lineSeparator() + "--------------------------"
150-
+ lineSeparator()
151-
+ "GroupId: " + getGroupId() + lineSeparator()
152-
+ "ArtifactId: " + getArtifactId() + lineSeparator()
153-
+ "Metadata Type: " + getClass().getName();
162+
163+
String buffer = LS + "Repository Metadata" + LS + "--------------------------" +
164+
LS + "GroupId: " + getGroupId() +
165+
LS + "ArtifactId: " + getArtifactId() +
166+
LS + "Metadata Type: " + getClass().getName();
167+
168+
return buffer;
154169
}
155170

156171
public int getNature() {
@@ -160,7 +175,9 @@ public int getNature() {
160175
public ArtifactRepositoryPolicy getPolicy(ArtifactRepository repository) {
161176
int nature = getNature();
162177
if ((nature & RepositoryMetadata.RELEASE_OR_SNAPSHOT) == RepositoryMetadata.RELEASE_OR_SNAPSHOT) {
163-
return new ArtifactRepositoryPolicy(repository.getReleases()).merge(repository.getSnapshots());
178+
ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy(repository.getReleases());
179+
policy.merge(repository.getSnapshots());
180+
return policy;
164181
} else if ((nature & RepositoryMetadata.SNAPSHOT) != 0) {
165182
return repository.getSnapshots();
166183
} else {

compat/maven-compat/src/main/java/org/apache/maven/artifact/resolver/ArtifactResolutionRequest.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,20 @@ public boolean isResolveTransitively() {
207207
}
208208

209209
public String toString() {
210-
StringBuilder sb = new StringBuilder()
211-
.append("REQUEST: ")
212-
.append(LS)
213-
.append("artifact: ")
214-
.append(artifact)
215-
.append(LS)
216-
.append(artifactDependencies)
217-
.append(LS)
218-
.append("localRepository: ")
219-
.append(localRepository)
220-
.append(LS)
221-
.append("remoteRepositories: ")
222-
.append(remoteRepositories);
223-
224-
return sb.toString();
210+
String sb = "REQUEST: " +
211+
LS +
212+
"artifact: " +
213+
artifact +
214+
LS +
215+
artifactDependencies +
216+
LS +
217+
"localRepository: " +
218+
localRepository +
219+
LS +
220+
"remoteRepositories: " +
221+
remoteRepositories;
222+
223+
return sb;
225224
}
226225

227226
public boolean isOffline() {

compat/maven-compat/src/main/java/org/apache/maven/repository/MetadataResolutionRequest.java

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -134,22 +134,21 @@ public MetadataResolutionRequest setManagedVersionMap(Map managedVersionMap) {
134134
}
135135

136136
public String toString() {
137-
StringBuilder sb = new StringBuilder()
138-
.append("REQUEST: ")
139-
.append("\n")
140-
.append("artifact: ")
141-
.append(mad)
142-
.append("\n")
143-
.append(artifactDependencies)
144-
.append("\n")
145-
.append("localRepository: ")
146-
.append(localRepository)
147-
.append("\n")
148-
.append("remoteRepositories: ")
149-
.append(remoteRepositories)
150-
.append("\n");
151-
152-
return sb.toString();
137+
String sb = "REQUEST: " +
138+
"\n" +
139+
"artifact: " +
140+
mad +
141+
"\n" +
142+
artifactDependencies +
143+
"\n" +
144+
"localRepository: " +
145+
localRepository +
146+
"\n" +
147+
"remoteRepositories: " +
148+
remoteRepositories +
149+
"\n";
150+
151+
return sb;
153152
}
154153

155154
public boolean isAsList() {

compat/maven-compat/src/main/java/org/apache/maven/repository/legacy/metadata/AbstractArtifactMetadata.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020

2121
import org.apache.maven.artifact.Artifact;
2222

23-
import static java.lang.System.lineSeparator;
24-
2523
/**
2624
* Common elements of artifact metadata.
2725
*
2826
*/
2927
@Deprecated
3028
public abstract class AbstractArtifactMetadata implements ArtifactMetadata {
29+
private static final String LS = System.lineSeparator();
3130

3231
protected Artifact artifact;
3332

@@ -48,9 +47,12 @@ public String getArtifactId() {
4847
}
4948

5049
public String extendedToString() {
51-
return lineSeparator() + "Artifact Metadata" + lineSeparator() + "--------------------------" + lineSeparator()
52-
+ "GroupId: " + getGroupId() + lineSeparator()
53-
+ "ArtifactId: " + getArtifactId() + lineSeparator()
54-
+ "Metadata Type: " + getClass().getName();
50+
51+
String buffer = LS + "Artifact Metadata" + LS + "--------------------------" +
52+
LS + "GroupId: " + getGroupId() +
53+
LS + "ArtifactId: " + getArtifactId() +
54+
LS + "Metadata Type: " + getClass().getName();
55+
56+
return buffer;
5557
}
5658
}

compat/maven-embedder/src/main/java/org/apache/maven/cli/transfer/AbstractMavenTransferListener.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,10 @@ public void transferInitiated(TransferEvent event) {
199199
String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
200200

201201
TransferResource resource = event.getResource();
202-
StringBuilder message = new StringBuilder();
203-
message.append(darkOn).append(action).append(' ').append(direction).append(' ');
204-
message.append(darkOff).append(resource.getRepositoryId());
205-
message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
206-
message.append(darkOff).append(resource.getResourceName());
202+
String message = darkOn + action + ' ' + direction + ' ' +
203+
darkOff + resource.getRepositoryId() +
204+
darkOn + ": " + resource.getRepositoryUrl() +
205+
darkOff + resource.getResourceName();
207206

208207
out.println(message);
209208
}

compat/maven-embedder/src/main/java/org/apache/maven/cli/transfer/Slf4jMavenTransferListener.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ public void transferInitiated(TransferEvent event) {
4848
String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
4949

5050
TransferResource resource = event.getResource();
51-
StringBuilder message = new StringBuilder();
52-
message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
53-
message.append(": ");
54-
message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
51+
String message = action + ' ' + direction + ' ' + resource.getRepositoryId() +
52+
": " +
53+
resource.getRepositoryUrl() + resource.getResourceName();
5554

56-
out.info(message.toString());
55+
out.info(message);
5756
}
5857

5958
@Override

compat/maven-model-builder/src/main/java/org/apache/maven/model/building/DefaultModelProblem.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ public Version getVersion() {
165165

166166
@Override
167167
public String toString() {
168-
return "[" + getSeverity() + "] " + getMessage() + " @ " + ModelProblemUtils.formatLocation(this, null);
168+
169+
String buffer = "[" + getSeverity() + "] " +
170+
getMessage() +
171+
" @ " + ModelProblemUtils.formatLocation(this, null);
172+
173+
return buffer;
169174
}
170175
}

compat/maven-model-builder/src/main/java/org/apache/maven/model/building/ModelData.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,14 @@ public void setVersion(String version) {
189189
* @return The effective identifier of the model, never {@code null}.
190190
*/
191191
public String getId() {
192-
return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
192+
193+
String buffer = getGroupId() +
194+
':' +
195+
getArtifactId() +
196+
':' +
197+
getVersion();
198+
199+
return buffer;
193200
}
194201

195202
@Override

0 commit comments

Comments
 (0)