Skip to content
Permalink
Browse files
IGNITE-16445 Always specify charset explicitly (#617)
  • Loading branch information
rpuch committed Feb 8, 2022
1 parent 33b449a commit ca09fba9c535c521468b6013fa63b2d3e1fc660e
Show file tree
Hide file tree
Showing 33 changed files with 231 additions and 95 deletions.
@@ -42,6 +42,11 @@ Run code style checks only:
mvn clean validate -Pcheckstyle -Dmaven.all-checks.skip
```

Run checks for Legacy API only:
```
mvn clean validate -Pmodernizer -Dmaven.all-checks.skip
```

Run javadoc style checks for public api only:
```
mvn clean checkstyle:checkstyle-aggregate -P javadoc-public-api
@@ -30,6 +30,7 @@ for xpath in "project/dependencyManagement/dependencies/dependency[not(scope='im
fi
done
for parent_xpath in "project/build/plugins" \
"project/profiles/profile/build/plugins" \
"project/dependencies"; do
if xpath -q -e "${parent_xpath}" ${ROOT}/parent/pom.xml 2>&1 | \
grep -E "<artifactId>${declaration}</artifactId>" 2>&1 1>/dev/null; then
@@ -0,0 +1,41 @@
<?xml version="1.0"?>

<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
-->

<modernizer>
<!-- Additional rules that augment the standard ones -->

<!-- Disallow String#getBytes() -->
<violation>
<name>java/lang/String.getBytes:()[B</name>
<version>1.6</version>
<comment>Prefer java.lang.String.getBytes(java.nio.charset.Charset)</comment>
</violation>

<!-- Disallow String(byte[], int, int) constructor -->
<violation>
<name>java/lang/String."&lt;init&gt;":([BII)V</name>
<version>1.6</version>
<comment>Prefer java.lang.String.&lt;init&gt;(byte[], int, int, java.nio.charset.Charset)</comment>
</violation>

<!-- Disallow String(byte[]) constructor -->
<violation>
<name>java/lang/String."&lt;init&gt;":([B)V</name>
<version>1.6</version>
<comment>Prefer java.lang.String.&lt;init&gt;(byte[], java.nio.charset.Charset)</comment>
</violation>
</modernizer>
@@ -175,7 +175,7 @@ public static <T extends Number & Comparable<? super T>> String compact(
return "[]";
}

StringBuffer sb = new StringBuffer();
StringBuilder sb = new StringBuilder();
sb.append('[');

List<T> l = new ArrayList<>(col);
@@ -16,16 +16,6 @@
*/
package com.facebook.presto.bytecode.expression;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.facebook.presto.bytecode.ClassDefinition;
import com.facebook.presto.bytecode.DynamicClassLoader;
import com.facebook.presto.bytecode.MethodDefinition;
import com.facebook.presto.bytecode.Parameter;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static com.facebook.presto.bytecode.Access.FINAL;
import static com.facebook.presto.bytecode.Access.PUBLIC;
import static com.facebook.presto.bytecode.Access.STATIC;
@@ -46,6 +36,16 @@
import static com.facebook.presto.bytecode.expression.BytecodeExpressions.invokeStatic;
import static com.facebook.presto.bytecode.expression.BytecodeExpressions.newArray;

import com.facebook.presto.bytecode.ClassDefinition;
import com.facebook.presto.bytecode.DynamicClassLoader;
import com.facebook.presto.bytecode.MethodDefinition;
import com.facebook.presto.bytecode.Parameter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

public class TestArrayBytecodeExpressions {
private static final DynamicClassLoader classLoader = new DynamicClassLoader(TestArrayBytecodeExpressions.class.getClassLoader());
private static final ClassDefinition classDefinition = new ClassDefinition(a(PUBLIC, FINAL), "DummyClass", type(Object.class));
@@ -138,10 +138,9 @@ public void testSetElement()
}

@Test
public void testGetElement()
throws Exception {
assertBytecodeExpression(constantString("abc").invoke("getBytes", byte[].class).getElement(1), "abc".getBytes()[1], "\"abc\".getBytes()[1]");
assertBytecodeExpression(constantString("abc").invoke("getBytes", byte[].class).getElement(constantInt(1)), "abc".getBytes()[1], "\"abc\".getBytes()[1]");
public void testGetElement() throws Exception {
assertBytecodeExpression(constantString("abc").invoke("toCharArray", char[].class).getElement(1), "abc".toCharArray()[1], "\"abc\".toCharArray()[1]");
assertBytecodeExpression(constantString("abc").invoke("toCharArray", char[].class).getElement(constantInt(1)), "abc".toCharArray()[1], "\"abc\".toCharArray()[1]");
}

private static MethodDefinition defineSetAndGetMethod(Class<?> aClass) {
@@ -17,6 +17,8 @@

package org.apache.ignite.cli.builtins.init;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@@ -203,7 +205,7 @@ private void fillNewConfigFile(File f,
@NotNull Path cfgDir,
@NotNull Path logDir
) {
try (FileWriter fileWriter = new FileWriter(f)) {
try (FileWriter fileWriter = new FileWriter(f, UTF_8)) {
Properties props = new Properties();

props.setProperty("bin", binDir.toString());
@@ -17,6 +17,8 @@

package org.apache.ignite.cli.builtins.node;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
@@ -250,7 +252,7 @@ public void createPidFile(String nodeName, long pid, Path pidsDir) {

Path pidPath = pidsDir.resolve(nodeName + "_" + System.currentTimeMillis() + ".pid");

try (FileWriter fileWriter = new FileWriter(pidPath.toFile())) {
try (FileWriter fileWriter = new FileWriter(pidPath.toFile(), UTF_8)) {
fileWriter.write(String.valueOf(pid));
} catch (IOException e) {
throw new IgniteCliException("Can't write pid file " + pidPath);
@@ -17,6 +17,7 @@

package org.apache.ignite.cli;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.doNothing;
@@ -554,7 +555,7 @@ void setHocon() throws IOException, InterruptedException {
verify(httpClient).send(
argThat(r -> "http://localhost:8081/management/v1/configuration/node/".equals(r.uri().toString())
&& "PATCH".equals(r.method())
&& r.bodyPublisher().get().contentLength() == expSentContent.getBytes().length
&& r.bodyPublisher().get().contentLength() == expSentContent.getBytes(UTF_8).length
&& "application/json".equals(r.headers().firstValue("Content-Type").get())),
any());
assertEquals("Configuration was updated successfully.\n\n"
@@ -584,7 +585,7 @@ void setJson() throws IOException, InterruptedException {
verify(httpClient).send(
argThat(r -> "http://localhost:8081/management/v1/configuration/node/".equals(r.uri().toString())
&& "PATCH".equals(r.method())
&& r.bodyPublisher().get().contentLength() == expSentContent.getBytes().length
&& r.bodyPublisher().get().contentLength() == expSentContent.getBytes(UTF_8).length
&& "application/json".equals(r.headers().firstValue("Content-Type").get())),
any());
assertEquals("Configuration was updated successfully.\n\n"
@@ -17,6 +17,8 @@

package org.apache.ignite.internal.jdbc;

import static java.nio.charset.StandardCharsets.UTF_8;

import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
@@ -618,7 +620,7 @@ public byte[] getBytes(int colIdx) throws SQLException {
} else if (cls == Double.class) {
return ByteBuffer.allocate(8).putDouble(((Double) val)).array();
} else if (cls == String.class) {
return ((String) val).getBytes();
return ((String) val).getBytes(UTF_8);
} else if (cls == UUID.class) {
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);

@@ -26,19 +26,19 @@
*/
public class LoggerHelperTest {
/** Message parameter 1. */
Integer i1 = new Integer(1);
Integer i1 = 1;

/** Message parameter 2. */
Integer i2 = new Integer(2);
Integer i2 = 2;

/** Message parameter 3. */
Integer i3 = new Integer(3);
Integer i3 = 3;

/** Array message parameter 0. */
Integer[] ia0 = new Integer[]{i1, i2, i3};

/** Array message parameter 1. */
Integer[] ia1 = new Integer[]{new Integer(10), new Integer(20), new Integer(30)};
Integer[] ia1 = new Integer[]{10, 20, 30};

/** Variable for the result message. */
String result;
@@ -17,6 +17,7 @@

package org.apache.ignite.internal.metastorage.server;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.function.Function.identity;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -2058,10 +2059,10 @@ private static void fill(KeyValueStorage storage, int keySuffix, int num) {
}

private static byte[] key(int k) {
return ("key" + k).getBytes();
return ("key" + k).getBytes(UTF_8);
}

private static byte[] keyValue(int k, int v) {
return ("key" + k + '_' + "val" + v).getBytes();
return ("key" + k + '_' + "val" + v).getBytes(UTF_8);
}
}
@@ -17,6 +17,7 @@

package org.apache.ignite.internal.network.direct.stream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.ignite.internal.util.ArrayUtils.BOOLEAN_ARRAY;
import static org.apache.ignite.internal.util.ArrayUtils.BYTE_ARRAY;
import static org.apache.ignite.internal.util.ArrayUtils.CHAR_ARRAY;
@@ -484,7 +485,7 @@ public void writeBooleanArray(boolean[] val) {
public void writeString(String val) {
if (val != null) {
if (curStrBackingArr == null) {
curStrBackingArr = val.getBytes();
curStrBackingArr = val.getBytes(UTF_8);
}

writeByteArray(curStrBackingArr);
@@ -1045,7 +1046,7 @@ public boolean[] readBooleanArray() {
public String readString() {
byte[] arr = readByteArray();

return arr != null ? new String(arr) : null;
return arr != null ? new String(arr, UTF_8) : null;
}

/** {@inheritDoc} */
@@ -17,6 +17,7 @@

package org.apache.ignite.internal.network.serialization.marshal;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.ignite.internal.network.serialization.marshal.Throwables.causalChain;
import static org.apache.ignite.internal.network.serialization.marshal.Throwables.rootCauseOf;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -175,7 +176,7 @@ private static Stream<Arguments> readWriteSpecs() {
),

// the following test writing methods only (readers are just to help testing them)
new ReadWriteSpec<>("writeBytes", oos -> oos.writeBytes("abc"), input -> readBytesFully(input, 3), "abc".getBytes()),
new ReadWriteSpec<>("writeBytes", oos -> oos.writeBytes("abc"), input -> readBytesFully(input, 3), "abc".getBytes(UTF_8)),
new ReadWriteSpec<>("writeChars", oos -> oos.writeChars("a"), DataInput::readChar, 'a'),

// the following test reading methods only (writers are just to help testing them)
@@ -18,6 +18,7 @@
package org.apache.ignite.raft.jraft.core;

import static java.lang.Thread.sleep;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -182,7 +183,7 @@ public void testTransferLeader() throws Exception {
private void sendTestTaskAndWait(Node node, int code) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(10);
for (int i = 0; i < 10; i++) {
ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes());
ByteBuffer data = ByteBuffer.wrap(("hello" + i).getBytes(UTF_8));
Task task = new Task(data, new ExpectClosure(code, null, latch));
node.apply(task);
}

0 comments on commit ca09fba

Please sign in to comment.