Skip to content

Commit 1b6cabb

Browse files
authored
[Java] use Files.createTempFile instead (#8787)
* use Files.createTempFile * fix import * add missing import
1 parent d85f61f commit 1b6cabb

14 files changed

Lines changed: 66 additions & 48 deletions

File tree

modules/openapi-generator/src/main/resources/Java/libraries/jersey2/ApiClient.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import java.security.KeyManagementException;
3535
import java.security.NoSuchAlgorithmException;
3636
import java.security.SecureRandom;
3737
import java.nio.file.Files;
38+
import java.nio.file.Paths;
3839
import java.nio.file.StandardCopyOption;
3940
import org.glassfish.jersey.logging.LoggingFeature;
4041
import java.util.logging.Level;
@@ -1027,15 +1028,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
10271028
prefix = filename.substring(0, pos) + "-";
10281029
suffix = filename.substring(pos);
10291030
}
1030-
// File.createTempFile requires the prefix to be at least three characters long
1031+
// Files.createTempFile requires the prefix to be at least three characters long
10311032
if (prefix.length() < 3)
10321033
prefix = "download-";
10331034
}
10341035

10351036
if (tempFolderPath == null)
1036-
return File.createTempFile(prefix, suffix);
1037+
return Files.createTempFile(prefix, suffix).toFile();
10371038
else
1038-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
1039+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
10391040
}
10401041

10411042
/**

modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/ApiClient.mustache

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ import java.lang.reflect.Type;
4141
import java.net.URI;
4242
import java.net.URLConnection;
4343
import java.net.URLEncoder;
44+
import java.nio.file.Files;
45+
import java.nio.file.Paths;
4446
import java.security.GeneralSecurityException;
4547
import java.security.KeyStore;
4648
import java.security.SecureRandom;
@@ -578,7 +580,7 @@ public class ApiClient {
578580
* with file response. The default value is <code>null</code>, i.e. using
579581
* the system's default tempopary folder.
580582
*
581-
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
583+
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
582584
* @return Temporary folder path
583585
*/
584586
public String getTempFolderPath() {
@@ -1077,15 +1079,15 @@ public class ApiClient {
10771079
prefix = filename.substring(0, pos) + "-";
10781080
suffix = filename.substring(pos);
10791081
}
1080-
// File.createTempFile requires the prefix to be at least three characters long
1082+
// Files.createTempFile requires the prefix to be at least three characters long
10811083
if (prefix.length() < 3)
10821084
prefix = "download-";
10831085
}
10841086

10851087
if (tempFolderPath == null)
1086-
return File.createTempFile(prefix, suffix);
1088+
return Files.createTempFile(prefix, suffix).toFile();
10871089
else
1088-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
1090+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
10891091
}
10901092

10911093
/**

modules/openapi-generator/src/main/resources/Java/libraries/resteasy/ApiClient.mustache

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import java.io.InputStream;
88
import java.io.UnsupportedEncodingException;
99
import java.net.URLEncoder;
1010
import java.nio.file.Files;
11+
import java.nio.file.Paths;
1112
import java.text.DateFormat;
1213
import java.text.SimpleDateFormat;
1314
import java.util.ArrayList;
@@ -278,7 +279,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
278279
* the system's default tempopary folder.
279280
*
280281
* @return the temporary folder path
281-
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File)"></a>
282+
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
282283
*/
283284
public String getTempFolderPath() {
284285
return tempFolderPath;
@@ -596,15 +597,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
596597
prefix = filename.substring(0, pos) + "-";
597598
suffix = filename.substring(pos);
598599
}
599-
// File.createTempFile requires the prefix to be at least three characters long
600+
// Files.createTempFile requires the prefix to be at least three characters long
600601
if (prefix.length() < 3)
601602
prefix = "download-";
602603
}
603604

604605
if (tempFolderPath == null)
605-
return File.createTempFile(prefix, suffix);
606+
return Files.createTempFile(prefix, suffix).toFile();
606607
else
607-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
608+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
608609
}
609610

610611
/**

modules/openapi-generator/src/main/resources/Java/libraries/retrofit2/play26/ApiClient.mustache

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import java.io.IOException;
55
import java.lang.annotation.Annotation;
66
import java.lang.reflect.Type;
77
import java.nio.file.Files;
8+
import java.nio.file.Path;
89
import java.nio.file.Paths;
910
import java.util.*;
1011

@@ -202,9 +203,9 @@ public class ApiClient {
202203
@Override
203204
public File convert(ResponseBody value) throws IOException {
204205
205-
File file = File.createTempFile("retrofit-file", ".tmp");
206-
Files.write(Paths.get(file.getPath()), value.bytes());
207-
return file;
206+
Path path = Files.createTempFile("retrofit-file", ".tmp");
207+
Files.write(path, value.bytes());
208+
return path.toFile();
208209
}
209210
};
210211
}

samples/client/petstore/java/jersey2-java8-localdatetime/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.security.NoSuchAlgorithmException;
3434
import java.security.SecureRandom;
3535
import java.nio.file.Files;
36+
import java.nio.file.Paths;
3637
import java.nio.file.StandardCopyOption;
3738
import org.glassfish.jersey.logging.LoggingFeature;
3839
import java.util.logging.Level;
@@ -943,15 +944,15 @@ public File prepareDownloadFile(Response response) throws IOException {
943944
prefix = filename.substring(0, pos) + "-";
944945
suffix = filename.substring(pos);
945946
}
946-
// File.createTempFile requires the prefix to be at least three characters long
947+
// Files.createTempFile requires the prefix to be at least three characters long
947948
if (prefix.length() < 3)
948949
prefix = "download-";
949950
}
950951

951952
if (tempFolderPath == null)
952-
return File.createTempFile(prefix, suffix);
953+
return Files.createTempFile(prefix, suffix).toFile();
953954
else
954-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
955+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
955956
}
956957

957958
/**

samples/client/petstore/java/jersey2-java8/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.security.NoSuchAlgorithmException;
3434
import java.security.SecureRandom;
3535
import java.nio.file.Files;
36+
import java.nio.file.Paths;
3637
import java.nio.file.StandardCopyOption;
3738
import org.glassfish.jersey.logging.LoggingFeature;
3839
import java.util.logging.Level;
@@ -943,15 +944,15 @@ public File prepareDownloadFile(Response response) throws IOException {
943944
prefix = filename.substring(0, pos) + "-";
944945
suffix = filename.substring(pos);
945946
}
946-
// File.createTempFile requires the prefix to be at least three characters long
947+
// Files.createTempFile requires the prefix to be at least three characters long
947948
if (prefix.length() < 3)
948949
prefix = "download-";
949950
}
950951

951952
if (tempFolderPath == null)
952-
return File.createTempFile(prefix, suffix);
953+
return Files.createTempFile(prefix, suffix).toFile();
953954
else
954-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
955+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
955956
}
956957

957958
/**

samples/client/petstore/java/okhttp-gson-dynamicOperations/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
import java.net.URI;
4242
import java.net.URLConnection;
4343
import java.net.URLEncoder;
44+
import java.nio.file.Files;
45+
import java.nio.file.Paths;
4446
import java.security.GeneralSecurityException;
4547
import java.security.KeyStore;
4648
import java.security.SecureRandom;
@@ -524,7 +526,7 @@ public ApiClient setDebugging(boolean debugging) {
524526
* with file response. The default value is <code>null</code>, i.e. using
525527
* the system's default tempopary folder.
526528
*
527-
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
529+
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
528530
* @return Temporary folder path
529531
*/
530532
public String getTempFolderPath() {
@@ -968,15 +970,15 @@ public File prepareDownloadFile(Response response) throws IOException {
968970
prefix = filename.substring(0, pos) + "-";
969971
suffix = filename.substring(pos);
970972
}
971-
// File.createTempFile requires the prefix to be at least three characters long
973+
// Files.createTempFile requires the prefix to be at least three characters long
972974
if (prefix.length() < 3)
973975
prefix = "download-";
974976
}
975977

976978
if (tempFolderPath == null)
977-
return File.createTempFile(prefix, suffix);
979+
return Files.createTempFile(prefix, suffix).toFile();
978980
else
979-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
981+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
980982
}
981983

982984
/**

samples/client/petstore/java/okhttp-gson-parcelableModel/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.net.URI;
3636
import java.net.URLConnection;
3737
import java.net.URLEncoder;
38+
import java.nio.file.Files;
39+
import java.nio.file.Paths;
3840
import java.security.GeneralSecurityException;
3941
import java.security.KeyStore;
4042
import java.security.SecureRandom;
@@ -513,7 +515,7 @@ public ApiClient setDebugging(boolean debugging) {
513515
* with file response. The default value is <code>null</code>, i.e. using
514516
* the system's default tempopary folder.
515517
*
516-
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
518+
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
517519
* @return Temporary folder path
518520
*/
519521
public String getTempFolderPath() {
@@ -969,15 +971,15 @@ public File prepareDownloadFile(Response response) throws IOException {
969971
prefix = filename.substring(0, pos) + "-";
970972
suffix = filename.substring(pos);
971973
}
972-
// File.createTempFile requires the prefix to be at least three characters long
974+
// Files.createTempFile requires the prefix to be at least three characters long
973975
if (prefix.length() < 3)
974976
prefix = "download-";
975977
}
976978

977979
if (tempFolderPath == null)
978-
return File.createTempFile(prefix, suffix);
980+
return Files.createTempFile(prefix, suffix).toFile();
979981
else
980-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
982+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
981983
}
982984

983985
/**

samples/client/petstore/java/okhttp-gson/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
import java.net.URI;
3636
import java.net.URLConnection;
3737
import java.net.URLEncoder;
38+
import java.nio.file.Files;
39+
import java.nio.file.Paths;
3840
import java.security.GeneralSecurityException;
3941
import java.security.KeyStore;
4042
import java.security.SecureRandom;
@@ -513,7 +515,7 @@ public ApiClient setDebugging(boolean debugging) {
513515
* with file response. The default value is <code>null</code>, i.e. using
514516
* the system's default tempopary folder.
515517
*
516-
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile">createTempFile</a>
518+
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
517519
* @return Temporary folder path
518520
*/
519521
public String getTempFolderPath() {
@@ -969,15 +971,15 @@ public File prepareDownloadFile(Response response) throws IOException {
969971
prefix = filename.substring(0, pos) + "-";
970972
suffix = filename.substring(pos);
971973
}
972-
// File.createTempFile requires the prefix to be at least three characters long
974+
// Files.createTempFile requires the prefix to be at least three characters long
973975
if (prefix.length() < 3)
974976
prefix = "download-";
975977
}
976978

977979
if (tempFolderPath == null)
978-
return File.createTempFile(prefix, suffix);
980+
return Files.createTempFile(prefix, suffix).toFile();
979981
else
980-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
982+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
981983
}
982984

983985
/**

samples/client/petstore/java/resteasy/src/main/java/org/openapitools/client/ApiClient.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.io.UnsupportedEncodingException;
99
import java.net.URLEncoder;
1010
import java.nio.file.Files;
11+
import java.nio.file.Paths;
1112
import java.text.DateFormat;
1213
import java.text.SimpleDateFormat;
1314
import java.util.ArrayList;
@@ -267,7 +268,7 @@ public ApiClient setDebugging(boolean debugging) {
267268
* the system's default tempopary folder.
268269
*
269270
* @return the temporary folder path
270-
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File)"></a>
271+
* @see <a href="https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createTempFile(java.lang.String,%20java.lang.String,%20java.nio.file.attribute.FileAttribute...)">createTempFile</a>
271272
*/
272273
public String getTempFolderPath() {
273274
return tempFolderPath;
@@ -585,15 +586,15 @@ public File prepareDownloadFile(Response response) throws IOException {
585586
prefix = filename.substring(0, pos) + "-";
586587
suffix = filename.substring(pos);
587588
}
588-
// File.createTempFile requires the prefix to be at least three characters long
589+
// Files.createTempFile requires the prefix to be at least three characters long
589590
if (prefix.length() < 3)
590591
prefix = "download-";
591592
}
592593

593594
if (tempFolderPath == null)
594-
return File.createTempFile(prefix, suffix);
595+
return Files.createTempFile(prefix, suffix).toFile();
595596
else
596-
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
597+
return Files.createTempFile(Paths.get(tempFolderPath), prefix, suffix).toFile();
597598
}
598599

599600
/**

0 commit comments

Comments
 (0)