Skip to content

Commit

Permalink
use some try-with resource (#672)
Browse files Browse the repository at this point in the history
reduce ecj warnings
  • Loading branch information
jukzi authored Jul 18, 2023
1 parent 0be5972 commit 6c6af29
Show file tree
Hide file tree
Showing 22 changed files with 130 additions and 355 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@
import org.eclipse.jdt.core.IJavaModelStatusConstants;
import org.eclipse.jdt.core.JavaModelException;

import org.eclipse.jdt.internal.corext.util.Messages;

import org.eclipse.jdt.internal.core.manipulation.util.BasicElementLabels;
import org.eclipse.jdt.internal.corext.util.Messages;

public class CreateFileChange extends ResourceChange {

Expand Down Expand Up @@ -149,8 +148,6 @@ public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {

@Override
public Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {

InputStream is= null;
try {
pm.beginTask(NLSChangesMessages.createFile_creating_resource, 3);

Expand All @@ -164,8 +161,7 @@ public Change perform(IProgressMonitor pm) throws CoreException, OperationCancel
pm.worked(1);
return composite.perform(new SubProgressMonitor(pm, 1));
} else { */
try {
is= new ByteArrayInputStream(fSource.getBytes(fEncoding));
try (InputStream is= new ByteArrayInputStream(fSource.getBytes(fEncoding))) {
file.create(is, false, new SubProgressMonitor(pm, 1));
if (fStampToRestore != IResource.NULL_STAMP) {
file.revertModificationStamp(fStampToRestore);
Expand All @@ -179,17 +175,12 @@ public Change perform(IProgressMonitor pm) throws CoreException, OperationCancel
} catch (UnsupportedEncodingException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
}
} finally {
try {
if (is != null)
is.close();
} catch (IOException ioe) {
} catch (IOException ioe) {
throw new JavaModelException(ioe, IJavaModelStatusConstants.IO_EXCEPTION);
} finally {
pm.done();
}
} finally {
pm.done();
}
}
}

protected IFile getOldFile(IProgressMonitor pm) throws OperationCanceledException {
pm.beginTask("", 1); //$NON-NLS-1$
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,14 @@ public String getTextType() {

public String getCurrentContent() throws JavaModelException {
IFile file= getOldFile(new NullProgressMonitor());
if (! file.exists())
if (!file.exists())
return ""; //$NON-NLS-1$
InputStream stream= null;
try{
stream= file.getContents();
try (InputStream stream= file.getContents()) {
String encoding= file.getCharset();
String c= NLSUtil.readString(stream, encoding);
return (c == null) ? "": c; //$NON-NLS-1$
} catch (CoreException e){
return (c == null) ? "" : c; //$NON-NLS-1$
} catch (IOException | CoreException e) {
throw new JavaModelException(e, IJavaModelStatusConstants.CORE_EXCEPTION);
} finally {
try {
if (stream != null)
stream.close();
} catch (IOException x) {
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import org.eclipse.jdt.junit.ITestRunListener;
import org.eclipse.jdt.junit.TestRunListener;

import org.eclipse.core.runtime.Assert;
Expand All @@ -65,7 +66,6 @@
import org.eclipse.jdt.internal.junit.Messages;
import org.eclipse.jdt.internal.junit.launcher.JUnitLaunchConfigurationConstants;
import org.eclipse.jdt.internal.junit.model.TestElement.Status;
import org.eclipse.jdt.junit.ITestRunListener;

/**
* Central registry for JUnit test runs.
Expand Down Expand Up @@ -495,23 +495,12 @@ public static void importIntoTestRunSession(File swapFile, TestRunSession testRu
* @throws CoreException if an error occurred
*/
public static void exportTestRunSession(TestRunSession testRunSession, File file) throws CoreException {
FileOutputStream out= null;
try {
out= new FileOutputStream(file);
exportTestRunSession(testRunSession, out);

try (FileOutputStream out= new FileOutputStream(file)) {
exportTestRunSession(testRunSession, out);
} catch (IOException | TransformerConfigurationException e) {
throwExportError(file, e);
} catch (TransformerException e) {
throwExportError(file, e);
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e2) {
JUnitCorePlugin.log(e2);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,10 @@ public static void unzip(ZipFile zipFile, File dstDir) throws IOException {
String entryName = entry.getName();
File file = new File(dstDir, changeSeparator(entryName, '/', File.separatorChar));
file.getParentFile().mkdirs();
InputStream src = null;
OutputStream dst = null;
try {
src = zipFile.getInputStream(entry);
dst = new FileOutputStream(file);
try (InputStream src= zipFile.getInputStream(entry);
OutputStream dst= new FileOutputStream(file)) {
transferData(src, dst);
} finally {
if(dst != null){
try {
dst.close();
} catch(IOException e){
}
}
if(src != null){
try {
src.close();
} catch(IOException e){
}
}
} catch (IOException e) {
}
}
} finally {
Expand Down Expand Up @@ -115,25 +100,9 @@ public static String changeSeparator(String path, char oldSeparator, char newSep
*/
public static void transferData(File source, File destination) throws IOException {
destination.getParentFile().mkdirs();
InputStream is = null;
OutputStream os = null;
try {
is = new FileInputStream(source);
os = new FileOutputStream(destination);
try (InputStream is = new FileInputStream(source);
OutputStream os = new FileOutputStream(destination)) {
transferData(is, os);
} finally {
if(os != null){
try {
os.close();
} catch(IOException e){
}
}
if(is != null){
try {
is.close();
} catch(IOException e){
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -61,7 +62,7 @@ protected void tearDown() throws Exception {
}
static boolean DEBUG= false;

private static final SimpleDateFormat DATE_FORMAT= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS Z", Locale.US);
private static final DateTimeFormatter DATE_FORMAT= DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS Z", Locale.US).withZone(ZoneId.systemDefault());


/** containing plug-in id */
Expand Down Expand Up @@ -127,7 +128,7 @@ protected void setUp() throws Exception {
EditorsUI.getPreferenceStore().putValue(SpellingService.PREFERENCE_SPELLING_ENABLED, IPreferenceStore.FALSE);

if (DEBUG)
System.out.println(DATE_FORMAT.format(new Date()) + ": " + getClass().getName() + "." + getName());
System.out.println(DATE_FORMAT.format(Instant.now()) + ": " + getClass().getName() + "." + getName());
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1400,14 +1400,11 @@ private void helperQualifiedName(String oldName, String newName, String textFile

ICompilationUnit newcu= getPackageP().getCompilationUnit(newName + ".java");
assertEqualLines("invalid renaming", getFileContents(getOutputTestFileName(newName)), newcu.getSource());
InputStreamReader reader= new InputStreamReader(file.getContents(true));
StringBuilder newContent= new StringBuilder();
try {
try (InputStreamReader reader= new InputStreamReader(file.getContents(true))) {
int ch;
while((ch= reader.read()) != -1)
newContent.append((char)ch);
} finally {
reader.close();
while ((ch= reader.read()) != -1)
newContent.append((char) ch);
}
String definedContent= getFileContents(getTestPath() + getName() + TEST_OUTPUT_INFIX + textFileName);
assertEqualLines("invalid updating", definedContent, newContent.toString());
Expand Down Expand Up @@ -1526,14 +1523,11 @@ public void testSimilarElements05() throws Exception {

helper3("SomeClass", "SomeDifferentClass", true, true, true, "test.html");

InputStreamReader reader= new InputStreamReader(file.getContents(true));
StringBuilder newContent= new StringBuilder();
try {
try (InputStreamReader reader= new InputStreamReader(file.getContents(true))) {
int ch;
while((ch= reader.read()) != -1)
newContent.append((char)ch);
} finally {
reader.close();
}
String definedContent= getFileContents(getTestPath() + "testSimilarElements05/out/test.html");
assertEqualLines("invalid updating test.html", newContent.toString(), definedContent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,22 +87,10 @@ public TemplateSet(String templateTag, ContextTypeRegistry registry) {
* @see #addFromStream(InputStream, boolean)
*/
public void addFromFile(File file, boolean allowDuplicates) throws CoreException {
InputStream stream= null;

try {
stream= new FileInputStream(file);
try (InputStream stream= new FileInputStream(file)) {
addFromStream(stream, allowDuplicates);

} catch (IOException e) {
throwReadException(e);

} finally {
try {
if (stream != null)
stream.close();
} catch (IOException e) {
// just exit
}
}
}

Expand Down Expand Up @@ -202,22 +190,10 @@ private String getAttributeValue(NamedNodeMap attributes, String name) {
* @see #saveToStream(OutputStream)
*/
public void saveToFile(File file) throws CoreException {
OutputStream stream= null;

try {
stream= new FileOutputStream(file);
try (OutputStream stream= new FileOutputStream(file)) {
saveToStream(stream);

} catch (IOException e) {
throwWriteException(e);

} finally {
try {
if (stream != null)
stream.close();
} catch (IOException e) {
// just exit
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,42 +168,23 @@ public synchronized void load() {
IPath stateLocation= JavaPlugin.getDefault().getStateLocation().append(fFileName);
File file= stateLocation.toFile();
if (file.exists()) {
InputStreamReader reader= null;
try {
reader = new InputStreamReader(new FileInputStream(file), "utf-8");//$NON-NLS-1$
try (InputStreamReader reader= new InputStreamReader(new FileInputStream(file), "utf-8")) {//$NON-NLS-1$
load(new InputSource(reader));
} catch (IOException | CoreException e) {
JavaPlugin.log(e);
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException e) {
JavaPlugin.log(e);
}
}
}
}

public synchronized void save() {
IPath stateLocation= JavaPlugin.getDefault().getStateLocation().append(fFileName);
File file= stateLocation.toFile();
OutputStream out= null;
try {
out= new FileOutputStream(file);
try (OutputStream out= new FileOutputStream(file)) {
save(out);
} catch (IOException | CoreException | TransformerFactoryConfigurationError e) {
// The XML library can be misconficgured (e.g. via
// -Djava.endorsed.dirs=C:\notExisting\xerces-2_7_1)
JavaPlugin.log(e);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
JavaPlugin.log(e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,23 +155,21 @@ private static String getFileContents(IFile file) throws CoreException {

private static int getFileLength(IFile file) throws CoreException {
// Cannot use file buffers here, since they are not yet in sync at this point.
InputStream contents= file.getContents();
InputStreamReader reader;
try {
reader= new InputStreamReader(contents, file.getCharset());
} catch (UnsupportedEncodingException e) {
JavaPlugin.log(e);
reader= new InputStreamReader(contents);
}
try {
return (int) reader.skip(Integer.MAX_VALUE);
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Corext.getPluginId(), e.getMessage(), e));
} finally {
try (InputStream contents= file.getContents()) {
InputStreamReader reader;
try {
reader= new InputStreamReader(contents, file.getCharset());
} catch (UnsupportedEncodingException e) {
JavaPlugin.log(e);
reader= new InputStreamReader(contents);
}
try {
return (int) reader.skip(Integer.MAX_VALUE);
} finally {
reader.close();
} catch (IOException e) {
}
} catch (IOException e) {
throw new CoreException(new Status(IStatus.ERROR, Corext.getPluginId(), e.getMessage(), e));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,8 @@ protected void javaToNative(Object data, TransferData transferData) {
* (String) handle identifier
*/

try {
ByteArrayOutputStream out= new ByteArrayOutputStream();
DataOutputStream dataOut= new DataOutputStream(out);
try (ByteArrayOutputStream out= new ByteArrayOutputStream();
DataOutputStream dataOut= new DataOutputStream(out)) {

//write the number of elements
dataOut.writeInt(javaElements.length);
Expand All @@ -86,8 +85,6 @@ protected void javaToNative(Object data, TransferData transferData) {
}

//cleanup
dataOut.close();
out.close();
byte[] bytes= out.toByteArray();
super.javaToNative(bytes, transferData);
} catch (IOException e) {
Expand Down
Loading

0 comments on commit 6c6af29

Please sign in to comment.