X-Received: by 2002:a37:a093:: with SMTP id j141mr18183016qke.21.1552310821959; Mon, 11 Mar 2019 06:27:01 -0700 (PDT) X-Received: by 2002:a81:65c2:: with SMTP id z185mr25334808ywb.430.1552310821696; Mon, 11 Mar 2019 06:27:01 -0700 (PDT) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!m21no2428437qta.0!news-out.google.com!d8ni4983qtr.1!nntp.google.com!m21no2428429qta.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Mon, 11 Mar 2019 06:27:01 -0700 (PDT) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.78.95.177; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H NNTP-Posting-Host: 50.78.95.177 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: Subject: Re: it's Closeable, but I don't want to close() it yet. From: Eric Douglas Injection-Date: Mon, 11 Mar 2019 13:27:01 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Lines: 39 Xref: csiph.com comp.lang.java.programmer:38814 On Wednesday, February 27, 2019 at 10:52:01 AM UTC-5, Andreas Leitgeb wrote= : > Everything runs fine, except eclipse warns me about spots > where the entity is requested, used, and then dropped. > Eclipse thinks it might need to be close()d. >=20 > Apart from ignoring the warning per eclipse settings or > adding @SuppressWarnings, is there maybe a way to tell > the compiler that a certain ref is not meant to be close()d? > Letting it know that - despite the entity's ultimate fate of > being eventually closed - this is not yet the time&place for it? I have just been ignoring that compile warning. I changed that to warn me = and tried to refactor my code to not throw warnings. I found that I have n= ot been closing HttpClient which apparently runs fine, I'm guessing they ge= t closed on cleanup. So I'm calling org.apache.http.impl.client.HttpClients.createDefault() whic= h returns a org.apache.http.impl.client.CloseableHttpClient, so Eclipse rec= ognizes it as Closeable even if you return it into the org.apache.http.clie= nt.HttpClient which is not Closeable. Then you call execute() which returns a org.apache.http.client.methods.Clos= eableHttpResponse. This also implements an interface which does not refere= nce close. So I've found there are 2 solutions to this. 1) You can fool this compiler, because it doesn't warn about variables whic= h are passed in or out of methods, assuming the method calling these method= s will take care of closing them. You can do this in 3 methods. One metho= d calls createDefault and returns the interface, one method calls execute a= nd returns the interface, and one method retrieves the value from the HttpR= esponse. 2) I try to do it all in one method and close the client and response after= getting the value, but if it's closed, how do I pass out the value? HttpR= esponse has multiple methods. I may need to getEntity, getStatusLine, getH= eaders. Do I assume I only want the one value, or pass everything out in a= new custom object? In this case I do only have one method using the HttpResponse which is gett= ing the headers to look for a charset value, defaulting to UTF-8 if it does= n't find one, and passing out a String value from IOUtils.toString(ent.getC= ontent(), charset) but to say that's all you should want to do with the Htt= pResponse doesn't seem like a safe assumption.