Skip to content

Commit f237c5b

Browse files
authored
Lists for RRset (dnsjava#56)
* Fix or disable unchecked warnings * Use List as return types for RRset rrs/sigs
1 parent 2cf542f commit f237c5b

File tree

16 files changed

+348
-368
lines changed

16 files changed

+348
-368
lines changed

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<configuration>
6666
<source>${target.jdk}</source>
6767
<target>${target.jdk}</target>
68+
<compilerArgument>-Xlint:unchecked</compilerArgument>
6869
</configuration>
6970
</plugin>
7071

src/main/java/jnamed.java

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,13 @@ public class jnamed {
163163
return null;
164164
}
165165

166-
public RRset
166+
public <T extends Record> RRset<T>
167167
findExactMatch(Name name, int type, int dclass, boolean glue) {
168168
Zone zone = findBestZone(name);
169169
if (zone != null)
170170
return zone.findExactMatch(name, type);
171171
else {
172-
RRset [] rrsets;
172+
List<RRset<T>> rrsets;
173173
Cache cache = getCache(dclass);
174174
if (glue)
175175
rrsets = cache.findAnyRecords(name, type);
@@ -178,28 +178,24 @@ public class jnamed {
178178
if (rrsets == null)
179179
return null;
180180
else
181-
return rrsets[0]; /* not quite right */
181+
return rrsets.get(0); /* not quite right */
182182
}
183183
}
184184

185-
void
186-
addRRset(Name name, Message response, RRset rrset, int section, int flags) {
185+
<T extends Record> void
186+
addRRset(Name name, Message response, RRset<T> rrset, int section, int flags) {
187187
for (int s = 1; s <= section; s++)
188188
if (response.findRRset(name, rrset.getType(), s))
189189
return;
190190
if ((flags & FLAG_SIGONLY) == 0) {
191-
Iterator<Record> it = rrset.rrs();
192-
while (it.hasNext()) {
193-
Record r = (Record) it.next();
191+
for (Record r : rrset.rrs()) {
194192
if (r.getName().isWild() && !name.isWild())
195193
r = r.withName(name);
196194
response.addRecord(r, section);
197195
}
198196
}
199197
if ((flags & (FLAG_SIGONLY | FLAG_DNSSECOK)) != 0) {
200-
Iterator<Record> it = rrset.sigs();
201-
while (it.hasNext()) {
202-
Record r = (Record) it.next();
198+
for (Record r : rrset.sigs()) {
203199
if (r.getName().isWild() && !name.isWild())
204200
r = r.withName(name);
205201
response.addRecord(r, section);
@@ -214,7 +210,7 @@ public class jnamed {
214210

215211
private void
216212
addNS(Message response, Zone zone, int flags) {
217-
RRset nsRecords = zone.getNS();
213+
RRset<NSRecord> nsRecords = zone.getNS();
218214
addRRset(nsRecords.getName(), response, nsRecords,
219215
Section.AUTHORITY, flags);
220216
}
@@ -224,17 +220,15 @@ public class jnamed {
224220
SetResponse sr = cache.lookupRecords(name, Type.NS, Credibility.HINT);
225221
if (!sr.isDelegation())
226222
return;
227-
RRset nsRecords = sr.getNS();
228-
Iterator<Record> it = nsRecords.rrs();
229-
while (it.hasNext()) {
230-
Record r = (Record) it.next();
223+
RRset<?> nsRecords = sr.getNS();
224+
for (Record r : nsRecords.rrs()) {
231225
response.addRecord(r, Section.AUTHORITY);
232226
}
233227
}
234228

235229
private void
236230
addGlue(Message response, Name name, int flags) {
237-
RRset a = findExactMatch(name, Type.A, DClass.IN, true);
231+
RRset<ARecord> a = findExactMatch(name, Type.A, DClass.IN, true);
238232
if (a == null)
239233
return;
240234
addRRset(name, response, a, Section.ADDITIONAL, flags);
@@ -299,13 +293,13 @@ else if (sr.isNXRRSET()) {
299293
}
300294
}
301295
else if (sr.isDelegation()) {
302-
RRset nsRecords = sr.getNS();
296+
RRset<NSRecord> nsRecords = sr.getNS();
303297
addRRset(nsRecords.getName(), response, nsRecords,
304298
Section.AUTHORITY, flags);
305299
}
306300
else if (sr.isCNAME()) {
307301
CNAMERecord cname = sr.getCNAME();
308-
RRset rrset = new RRset(cname);
302+
RRset<CNAMERecord> rrset = new RRset<>(cname);
309303
addRRset(name, response, rrset, Section.ANSWER, flags);
310304
if (zone != null && iterations == 0)
311305
response.getHeader().setFlag(Flags.AA);
@@ -314,7 +308,7 @@ else if (sr.isCNAME()) {
314308
}
315309
else if (sr.isDNAME()) {
316310
DNAMERecord dname = sr.getDNAME();
317-
RRset rrset = new RRset(dname);
311+
RRset<DNAMERecord> rrset = new RRset<>(dname);
318312
addRRset(name, response, rrset, Section.ANSWER, flags);
319313
Name newname;
320314
try {
@@ -323,17 +317,19 @@ else if (sr.isDNAME()) {
323317
catch (NameTooLongException e) {
324318
return Rcode.YXDOMAIN;
325319
}
326-
rrset = new RRset(new CNAMERecord(name, dclass, 0, newname));
327-
addRRset(name, response, rrset, Section.ANSWER, flags);
320+
321+
CNAMERecord cname = new CNAMERecord(name, dclass, 0, newname);
322+
RRset<CNAMERecord> cnamerrset = new RRset<>(cname);
323+
addRRset(name, response, cnamerrset, Section.ANSWER, flags);
328324
if (zone != null && iterations == 0)
329325
response.getHeader().setFlag(Flags.AA);
330326
rcode = addAnswer(response, newname, type, dclass,
331327
iterations + 1, flags);
332328
}
333329
else if (sr.isSuccessful()) {
334-
RRset [] rrsets = sr.answers();
335-
for (RRset rrset : rrsets)
336-
addRRset(name, response, rrset, Section.ANSWER, flags);
330+
List<RRset<?>> rrsets = sr.answers();
331+
for (RRset<?> rrset : rrsets)
332+
addRRset(name, response, rrset, Section.ANSWER, flags);
337333
if (zone != null) {
338334
addNS(response, zone, flags);
339335
if (iterations == 0)
@@ -351,13 +347,13 @@ else if (sr.isSuccessful()) {
351347
boolean first = true;
352348
if (zone == null)
353349
return errorMessage(query, Rcode.REFUSED);
354-
Iterator<RRset> it = zone.AXFR();
355350
try {
356351
DataOutputStream dataOut;
357352
dataOut = new DataOutputStream(s.getOutputStream());
358353
int id = query.getHeader().getID();
354+
Iterator<RRset<?>> it = zone.AXFR();
359355
while (it.hasNext()) {
360-
RRset rrset = (RRset) it.next();
356+
RRset<?> rrset = it.next();
361357
Message response = new Message(id);
362358
Header header = response.getHeader();
363359
header.setFlag(Flags.QR);

src/main/java/org/xbill/DNS/Cache.java

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ private interface Element {
3535
return (int)expire;
3636
}
3737

38-
private static class CacheRRset extends RRset implements Element {
38+
private static class CacheRRset<T extends Record> extends RRset<T> implements Element {
3939
private static final long serialVersionUID = 5971755205903597024L;
4040

4141
int credibility;
4242
int expire;
4343

4444
public
45-
CacheRRset(Record rec, int cred, long maxttl) {
45+
CacheRRset(T rec, int cred, long maxttl) {
4646
super();
4747
this.credibility = cred;
4848
this.expire = limitExpire(rec.getTTL(), maxttl);
4949
addRR(rec);
5050
}
5151

5252
public
53-
CacheRRset(RRset rrset, int cred, long maxttl) {
53+
CacheRRset(RRset<T> rrset, int cred, long maxttl) {
5454
super(rrset);
5555
this.credibility = cred;
5656
this.expire = limitExpire(rrset.getTTL(), maxttl);
@@ -132,7 +132,7 @@ private static class NegativeElement implements Element {
132132
}
133133
}
134134

135-
private static class CacheMap extends LinkedHashMap {
135+
private static class CacheMap extends LinkedHashMap<Name, Object> {
136136
private int maxsize;
137137

138138
CacheMap(int maxsize) {
@@ -198,7 +198,7 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
198198
Master m = new Master(file);
199199
Record record;
200200
while ((record = m.nextRecord()) != null)
201-
addRecord(record, Credibility.HINT, m);
201+
addRecord(record, Credibility.HINT);
202202
}
203203

204204
private synchronized Object
@@ -214,9 +214,10 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
214214
private synchronized Element []
215215
allElements(Object types) {
216216
if (types instanceof List) {
217-
List typelist = (List) types;
217+
@SuppressWarnings("unchecked")
218+
List<Element> typelist = (List<Element>) types;
218219
int size = typelist.size();
219-
return (Element []) typelist.toArray(new Element[size]);
220+
return typelist.toArray(new Element[size]);
220221
} else {
221222
Element set = (Element) types;
222223
return new Element[] {set};
@@ -271,9 +272,10 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
271272
}
272273
int type = element.getType();
273274
if (types instanceof List) {
275+
@SuppressWarnings("unchecked")
274276
List<Element> list = (List<Element>) types;
275277
for (int i = 0; i < list.size(); i++) {
276-
Element elt = (Element) list.get(i);
278+
Element elt = list.get(i);
277279
if (elt.getType() == type) {
278280
list.set(i, element);
279281
return;
@@ -328,22 +330,22 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
328330
* Adds a record to the Cache.
329331
* @param r The record to be added
330332
* @param cred The credibility of the record
331-
* @param o The source of the record (this could be a Message, for example)
332333
* @see Record
333334
*/
334335
public synchronized void
335-
addRecord(Record r, int cred, Object o) {
336+
addRecord(Record r, int cred) {
336337
Name name = r.getName();
337338
int type = r.getRRsetType();
338339
if (!Type.isRR(type))
339340
return;
340341
Element element = findElement(name, type, cred);
341342
if (element == null) {
342-
CacheRRset crrset = new CacheRRset(r, cred, maxcache);
343+
CacheRRset<Record> crrset = new CacheRRset<>(r, cred, maxcache);
343344
addRRset(crrset, cred);
344345
} else if (element.compareCredibility(cred) == 0) {
345346
if (element instanceof CacheRRset) {
346-
CacheRRset crrset = (CacheRRset) element;
347+
@SuppressWarnings("unchecked")
348+
CacheRRset<Record> crrset = (CacheRRset<Record>) element;
347349
crrset.addRR(r);
348350
}
349351
}
@@ -355,8 +357,8 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
355357
* @param cred The credibility of these records
356358
* @see RRset
357359
*/
358-
public synchronized void
359-
addRRset(RRset rrset, int cred) {
360+
public synchronized <T extends Record> void
361+
addRRset(RRset<T> rrset, int cred) {
360362
long ttl = rrset.getTTL();
361363
Name name = rrset.getName();
362364
int type = rrset.getType();
@@ -368,11 +370,11 @@ protected boolean removeEldestEntry(Map.Entry eldest) {
368370
if (element != null && element.compareCredibility(cred) <= 0)
369371
element = null;
370372
if (element == null) {
371-
CacheRRset crrset;
373+
CacheRRset<T> crrset;
372374
if (rrset instanceof CacheRRset)
373-
crrset = (CacheRRset) rrset;
375+
crrset = (CacheRRset<T>) rrset;
374376
else
375-
crrset = new CacheRRset(rrset, cred, maxcache);
377+
crrset = new CacheRRset<>(rrset, cred, maxcache);
376378
addElement(name, crrset);
377379
}
378380
}
@@ -453,7 +455,7 @@ else if (isExact)
453455
continue;
454456
if (element.compareCredibility(minCred) < 0)
455457
continue;
456-
sr.addRRset((CacheRRset) element);
458+
sr.addRRset((CacheRRset<?>) element);
457459
added++;
458460
}
459461
/* There were positive entries */
@@ -518,11 +520,12 @@ else if (isExact)
518520
return lookup(name, type, minCred);
519521
}
520522

521-
private RRset []
523+
@SuppressWarnings("unchecked")
524+
private <T extends Record> List<RRset<T>>
522525
findRecords(Name name, int type, int minCred) {
523526
SetResponse cr = lookupRecords(name, type, minCred);
524527
if (cr.isSuccessful())
525-
return cr.answers();
528+
return (List<RRset<T>>)(List) cr.answers();
526529
else
527530
return null;
528531
}
@@ -535,7 +538,7 @@ else if (isExact)
535538
* @return An array of RRsets, or null
536539
* @see Credibility
537540
*/
538-
public RRset []
541+
public <T extends Record> List<RRset<T>>
539542
findRecords(Name name, int type) {
540543
return findRecords(name, type, Credibility.NORMAL);
541544
}
@@ -548,7 +551,7 @@ else if (isExact)
548551
* @return An array of RRsets, or null
549552
* @see Credibility
550553
*/
551-
public RRset []
554+
public <T extends Record>List<RRset<T>>
552555
findAnyRecords(Name name, int type) {
553556
return findRecords(name, type, Credibility.GLUE);
554557
}
@@ -572,14 +575,12 @@ else if (isExact)
572575
}
573576

574577
private static void
575-
markAdditional(RRset rrset, Set<Name> names) {
578+
markAdditional(RRset<?> rrset, Set<Name> names) {
576579
Record first = rrset.first();
577580
if (first.getAdditionalName() == null)
578581
return;
579582

580-
Iterator<Record> it = rrset.rrs();
581-
while (it.hasNext()) {
582-
Record r = it.next();
583+
for (Record r : rrset.rrs()) {
583584
Name name = r.getAdditionalName();
584585
if (name != null)
585586
names.add(name);
@@ -594,6 +595,7 @@ else if (isExact)
594595
* lookup, or null if nothing useful could be cached from the message.
595596
* @see Message
596597
*/
598+
@SuppressWarnings("unchecked")
597599
public SetResponse
598600
addMessage(Message in) {
599601
boolean isAuth = in.getHeader().getFlag(Flags.AA);
@@ -623,7 +625,7 @@ else if (isExact)
623625
additionalNames = new HashSet<>();
624626

625627
answers = in.getSectionRRsets(Section.ANSWER);
626-
for (RRset answer : answers) {
628+
for (RRset<?> answer : answers) {
627629
if (answer.getDClass() != qclass)
628630
continue;
629631
int type = answer.getType();
@@ -664,14 +666,15 @@ else if (isExact)
664666
}
665667

666668
auth = in.getSectionRRsets(Section.AUTHORITY);
667-
RRset soa = null, ns = null;
668-
for (RRset rset : auth) {
669+
RRset<SOARecord> soa = null;
670+
RRset<NSRecord> ns = null;
671+
for (RRset<?> rset : auth) {
669672
if (rset.getType() == Type.SOA &&
670673
curname.subdomain(rset.getName()))
671-
soa = rset;
674+
soa = (RRset<SOARecord>) rset;
672675
else if (rset.getType() == Type.NS &&
673676
curname.subdomain(rset.getName()))
674-
ns = rset;
677+
ns = (RRset<NSRecord>) rset;
675678
}
676679
if (!completed) {
677680
/* This is a negative response or a referral. */
@@ -710,7 +713,7 @@ else if (rset.getType() == Type.NS &&
710713
}
711714

712715
addl = in.getSectionRRsets(Section.ADDITIONAL);
713-
for (RRset rRset : addl) {
716+
for (RRset<?> rRset : addl) {
714717
int type = rRset.getType();
715718
if (type != Type.A && type != Type.AAAA && type != Type.A6)
716719
continue;

0 commit comments

Comments
 (0)