Skip to content

Commit cbb29c7

Browse files
authored
chore(mt): adopt JSpecify for null-safety and update dependencies (#1933)
* chore(core): adopt JSpecify for null-safety and update dependencies - Replace `JetBrains.annotations` with `JSpecify` in build configuration. - Add `@NullMarked` and `@SuppressWarnings("unused")` annotations to translation classes. - Remove unnecessary null checks in `IBMWatsonTranslate`, `YandexCloudTranslate`, and `Google2Translate`. Signed-off-by: Hiroshi Miura <miurahr@linux.com> * fix(core): simplify error handling in YandexCloudTranslate - Remove redundant `Objects.requireNonNullElse` in error handling. - Directly use `extractErrorMessage` for improved readability. Signed-off-by: Hiroshi Miura <miurahr@linux.com> --------- Signed-off-by: Hiroshi Miura <miurahr@linux.com>
1 parent f6f6b11 commit cbb29c7

File tree

4 files changed

+24
-34
lines changed

4 files changed

+24
-34
lines changed

machinetranslators/google/src/main/java/org/omegat/machinetranslators/google/Google2Translate.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.fasterxml.jackson.databind.JsonNode;
4040
import com.fasterxml.jackson.databind.ObjectMapper;
4141
import org.jetbrains.annotations.Nullable;
42+
import org.jspecify.annotations.NullMarked;
4243
import tokyo.northside.logging.ILogger;
4344
import tokyo.northside.logging.LoggerFactory;
4445

@@ -64,6 +65,8 @@
6465
* "https://cloud.google.com/translate/docs/basic/setup-basic">Translation
6566
* API</a>
6667
*/
68+
@NullMarked
69+
@SuppressWarnings("unused")
6770
public class Google2Translate extends BaseCachedTranslate {
6871

6972
public static final String ALLOW_GOOGLE2_TRANSLATE = "allow_google2_translate";
@@ -162,14 +165,14 @@ protected int getMaxTextLength() {
162165
}
163166

164167
String googleKey = getCredential(PROPERTY_API_KEY);
165-
if (googleKey == null || googleKey.isEmpty()) {
168+
if (googleKey.isEmpty()) {
166169
if (temporaryKey == null) {
167170
throw new MachineTranslateError(BUNDLE.getString("GOOGLE_API_KEY_NOTFOUND"));
168171
}
169172
googleKey = temporaryKey;
170173
}
171174

172-
Map<String, String> params = new TreeMap<String, String>();
175+
Map<String, String> params = new TreeMap<>();
173176

174177
if (isPremium()) {
175178
params.put("model", "nmt");
@@ -180,20 +183,16 @@ protected int getMaxTextLength() {
180183
params.put("target", targetLang);
181184
params.put("q", text);
182185
// The 'text' format mangles the tags, whereas the 'html' encodes some
183-
// characters
184-
// as entities. Since it's more reliable to convert the entities back,
185-
// we are
186-
// using 'html' and convert the text with the unescapeHTML() method.
186+
// characters as entities. Since it's more reliable to convert the
187+
// entities back, we are using 'html' and convert the text with the
188+
// unescapeHTML() method.
187189
params.put("format", "html");
188190

189-
Map<String, String> headers = new TreeMap<String, String>();
191+
Map<String, String> headers = new TreeMap<>();
190192
headers.put("X-HTTP-Method-Override", "GET");
191193

192194
String v = HttpConnectionUtils.post(googleTranslateUrl, params, headers);
193195
String tr = getJsonResults(v);
194-
if (tr == null) {
195-
return null;
196-
}
197196
tr = BaseTranslate.unescapeHTML(tr);
198197
return cleanSpacesAroundTags(tr, text);
199198
}

machinetranslators/ibmwatson/src/main/java/org/omegat/machinetranslators/ibmwatson/IBMWatsonTranslate.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.fasterxml.jackson.databind.ObjectMapper;
4949

5050
import org.jetbrains.annotations.Nullable;
51+
import org.jspecify.annotations.NullMarked;
5152
import org.omegat.core.Core;
5253
import org.omegat.core.machinetranslators.BaseCachedTranslate;
5354
import org.omegat.core.machinetranslators.BaseTranslate;
@@ -70,6 +71,8 @@
7071
* "https://www.ibm.com/watson/developercloud/language-translator/api/v3/">Translation
7172
* API</a>
7273
*/
74+
@NullMarked
75+
@SuppressWarnings("unused")
7376
public class IBMWatsonTranslate extends BaseCachedTranslate {
7477
protected static final String PROPERTY_LOGIN = "ibmwatson.api.login";
7578
protected static final String PROPERTY_PASSWORD = "ibmwatson.api.password";
@@ -117,7 +120,7 @@ protected int getMaxTextBytes() {
117120
String apiLogin = getCredential(PROPERTY_LOGIN);
118121
String apiPassword = getCredential(PROPERTY_PASSWORD);
119122

120-
if (apiLogin == null || apiLogin.isEmpty()) {
123+
if (apiLogin.isEmpty()) {
121124
throw new MachineTranslateError(BUNDLE.getString("IBMWATSON_API_KEY_NOTFOUND"));
122125
}
123126

@@ -127,7 +130,7 @@ protected int getMaxTextBytes() {
127130
// is "apikey:apikey"
128131
// see
129132
// https://www.ibm.com/watson/developercloud/language-translator/api/v2/curl.html?curl#authentication
130-
if (apiPassword == null || apiPassword.isEmpty()) {
133+
if (apiPassword.isEmpty()) {
131134
apiPassword = apiLogin;
132135
apiLogin = "apikey";
133136
}
@@ -152,9 +155,6 @@ protected int getMaxTextBytes() {
152155
String v = HttpConnectionUtils.postJSON(getWatsonUrl() + "/v3/translate?version=" + WATSON_VERSION,
153156
json, headers);
154157
String tr = getJsonResults(v);
155-
if (tr == null) {
156-
return null;
157-
}
158158
tr = BaseTranslate.unescapeHTML(tr);
159159
return cleanSpacesAroundTags(tr, text);
160160
}
@@ -179,7 +179,7 @@ protected String createJsonRequest(Language sLang, Language tLang, String trText
179179
Map<String, Object> params = new TreeMap<>();
180180
params.put("text", Collections.singletonList(trText));
181181
String modelId = getModelId();
182-
if (modelId != null && !modelId.isEmpty()) {
182+
if (!modelId.isEmpty()) {
183183
params.put("model_id", modelId);
184184
}
185185
params.put("source", sLang.getLanguageCode().toUpperCase(Locale.ENGLISH));

machinetranslators/yandex/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ dependencies {
1818
// JSON parser
1919
compileOnly(libs.jackson.core)
2020
compileOnly(libs.jackson.databind)
21-
implementation(libs.jetbrains.annotations)
21+
implementation(libs.jspecify)
2222
}
2323
testImplementation(testFixtures(project.rootProject))
2424
testImplementation(libs.jackson.core)

machinetranslators/yandex/src/main/java/org/omegat/machinetranslators/yandex/YandexCloudTranslate.java

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import java.util.Collections;
3737
import java.util.List;
3838
import java.util.Map;
39-
import java.util.Objects;
4039
import java.util.ResourceBundle;
4140
import java.util.TreeMap;
4241

@@ -48,7 +47,8 @@
4847
import com.fasterxml.jackson.core.JsonProcessingException;
4948
import com.fasterxml.jackson.databind.JsonNode;
5049
import com.fasterxml.jackson.databind.ObjectMapper;
51-
import org.jetbrains.annotations.Nullable;
50+
import org.jspecify.annotations.NullMarked;
51+
import org.jspecify.annotations.Nullable;
5252
import tokyo.northside.logging.ILogger;
5353
import tokyo.northside.logging.LoggerFactory;
5454

@@ -73,6 +73,8 @@
7373
* "https://cloud.yandex.com/docs/translate/api-ref/Translation/">Translation
7474
* API</a>
7575
*/
76+
@NullMarked
77+
@SuppressWarnings("unused")
7678
public class YandexCloudTranslate extends BaseCachedTranslate {
7779

7880
public static final String ALLOW_YANDEX_CLOUD_TRANSLATE = "allow_yandex_cloud_translate";
@@ -129,12 +131,12 @@ protected int getMaxTextLength() {
129131
protected @Nullable String translate(final Language sLang, final Language tLang, final String text)
130132
throws Exception {
131133
String oAuthToken = getCredential(PROPERTY_OAUTH_TOKEN);
132-
if (oAuthToken == null || oAuthToken.isEmpty()) {
134+
if (oAuthToken.isEmpty()) {
133135
throw new Exception(BUNDLE.getString("MT_ENGINE_YANDEX_CLOUD_OAUTH_TOKEN_NOT_FOUND"));
134136
}
135137

136138
String folderId = getCredential(PROPERTY_FOLDER_ID);
137-
if (folderId == null || folderId.isEmpty()) {
139+
if (folderId.isEmpty()) {
138140
throw new Exception(BUNDLE.getString("MT_ENGINE_YANDEX_CLOUD_FOLDER_ID_NOT_FOUND"));
139141
}
140142

@@ -152,20 +154,12 @@ protected int getMaxTextLength() {
152154
try {
153155
response = HttpConnectionUtils.postJSON(TRANSLATE_URL, request, headers);
154156
} catch (HttpConnectionUtils.ResponseError e) {
155-
String errorMessage = extractErrorMessage(e.body);
156-
if (errorMessage == null) {
157-
errorMessage = BUNDLE.getString("MT_ENGINE_YANDEX_CLOUD_BAD_TRANSLATE_RESPONSE");
158-
throw new MachineTranslateError(errorMessage);
159-
}
160-
throw new MachineTranslateError(Objects.requireNonNullElse(e.getMessage(), "HTTP error: " + e.code));
157+
throw new MachineTranslateError(extractErrorMessage(e.body));
161158
}
162159
if (response == null) {
163160
return null;
164161
}
165162
String tr = extractTranslation(response);
166-
if (tr == null) {
167-
return null;
168-
}
169163
return cleanSpacesAroundTags(tr, text);
170164
}
171165

@@ -280,9 +274,6 @@ protected String extractTranslation(final String json) throws MachineTranslateEr
280274
} catch (HttpConnectionUtils.ResponseError e) {
281275
// Try to extract error message from the error body
282276
IAMErrorMessage = extractErrorMessage(e.body);
283-
if (IAMErrorMessage == null) {
284-
IAMErrorMessage = BUNDLE.getString("MT_ENGINE_YANDEX_CLOUD_BAD_IAM_RESPONSE");
285-
}
286277
return null;
287278
} catch (IOException e) {
288279
IAMErrorMessage = e.getLocalizedMessage();
@@ -309,7 +300,7 @@ protected String extractTranslation(final String json) throws MachineTranslateEr
309300
* @param glossaryTerms
310301
* glossary map.
311302
*/
312-
protected GlossaryConfig createGlossaryConfigPart(Map<String, String> glossaryTerms) {
303+
GlossaryConfig createGlossaryConfigPart(Map<String, String> glossaryTerms) {
313304
List<GlossaryPair> pairs = new ArrayList<>();
314305
for (Map.Entry<String, String> e : glossaryTerms.entrySet()) {
315306
pairs.add(new GlossaryPair(e.getKey(), e.getValue()));

0 commit comments

Comments
 (0)