Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1852556

Browse files
committed
Builds and runs with JDK9
1 parent be5962b commit 1852556

18 files changed

+46
-22
lines changed

collectiontopics/CanonicalMapping.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public boolean equals(Object r) {
1919
return r instanceof Element &&
2020
Objects.equals(ident, ((Element)r).ident);
2121
}
22+
@SuppressWarnings("deprecation")
2223
@Override
2324
protected void finalize() {
2425
System.out.println("Finalizing " +

collectiontopics/References.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class VeryBig {
1313
VeryBig(String id) { ident = id; }
1414
@Override
1515
public String toString() { return ident; }
16+
@SuppressWarnings("deprecation")
1617
@Override
1718
protected void finalize() {
1819
System.out.println("Finalizing " + ident);

collectiontopics/SetOrder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class SetOrder {
2323
for(String type: sets) {
2424
System.out.format("[-> %s <-]%n",
2525
type.substring(type.lastIndexOf('.') + 1));
26-
@SuppressWarnings("unchecked")
26+
@SuppressWarnings({"unchecked", "deprecation"})
2727
Set<String> set = (Set<String>)
2828
Class.forName(type).newInstance();
2929
set.addAll(RLIST);

generics/DynamicProxyMixin.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public static Object newInstance(Tuple2... pairs) {
4444

4545
public class DynamicProxyMixin {
4646
public static void main(String[] args) {
47+
@SuppressWarnings("unchecked")
4748
Object mixin = MixinProxy.newInstance(
4849
tuple(new BasicImp(), Basic.class),
4950
tuple(new TimeStampedImp(), TimeStamped.class),

generics/InstantiateGenericType.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
55
import java.util.function.*;
6+
import java.lang.reflect.InvocationTargetException;
67

78
class ClassAsFactory<T> implements Supplier<T> {
89
Class<T> kind;
910
ClassAsFactory(Class<T> kind) {
1011
this.kind = kind;
1112
}
13+
@SuppressWarnings("deprecation")
1214
@Override
1315
public T get() {
1416
try {
1517
return kind.newInstance();
16-
} catch(InstantiationException |
17-
IllegalAccessException e) {
18+
} catch(Exception e) {
1819
throw new RuntimeException(e);
1920
}
2021
}

generics/coffee/CoffeeSupplier.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.*;
88
import java.util.function.*;
99
import java.util.stream.*;
10+
import java.lang.reflect.InvocationTargetException;
1011

1112
public class CoffeeSupplier
1213
implements Supplier<Coffee>, Iterable<Coffee> {
@@ -21,10 +22,13 @@ public CoffeeSupplier() {}
2122
public Coffee get() {
2223
try {
2324
return (Coffee)
24-
types[rand.nextInt(types.length)].newInstance();
25+
types[rand.nextInt(types.length)]
26+
.getConstructor().newInstance();
2527
// Report programmer errors at run time:
26-
} catch(InstantiationException |
27-
IllegalAccessException e) {
28+
} catch(InstantiationException |
29+
NoSuchMethodException |
30+
InvocationTargetException |
31+
IllegalAccessException e) {
2832
throw new RuntimeException(e);
2933
}
3034
}

housekeeping/TerminationCondition.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Book {
1414
void checkIn() {
1515
checkedOut = false;
1616
}
17+
@SuppressWarnings("deprecation")
1718
@Override
1819
public void finalize() {
1920
if(checkedOut)

onjava/BasicSupplier.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Supplier from a class with a no-arg constructor
66
package onjava;
77
import java.util.function.*;
8+
import java.lang.reflect.InvocationTargetException;
89

910
public class BasicSupplier<T> implements Supplier<T> {
1011
private Class<T> type;
@@ -15,8 +16,10 @@ public BasicSupplier(Class<T> type) {
1516
public T get() {
1617
try {
1718
// Assumes type is a public class:
18-
return type.newInstance();
19+
return type.getConstructor().newInstance();
1920
} catch(InstantiationException |
21+
NoSuchMethodException |
22+
InvocationTargetException |
2023
IllegalAccessException e) {
2124
throw new RuntimeException(e);
2225
}

onjava/atunit/AtUnit.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,11 @@ Method checkForCleanupMethod(Method m) {
162162
}
163163
} else { // Use the no-arg constructor:
164164
try {
165-
return testClass.newInstance();
165+
return testClass
166+
.getConstructor().newInstance();
166167
} catch(InstantiationException |
168+
NoSuchMethodException |
169+
InvocationTargetException |
167170
IllegalAccessException e) {
168171
throw new RuntimeException(
169172
"Couldn't create a test object. " +

patterns/BoxObserver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import onjava.MouseClick;
1414

1515
// You must inherit a new type of Observable:
16+
@SuppressWarnings("deprecation")
1617
class BoxObservable extends Observable {
1718
@Override
1819
public void notifyObservers(Object b) {
@@ -22,6 +23,7 @@ public void notifyObservers(Object b) {
2223
}
2324
}
2425

26+
@SuppressWarnings("deprecation")
2527
public class BoxObserver extends JFrame {
2628
Observable notifier = new BoxObservable();
2729
public BoxObserver(int grid) {
@@ -45,6 +47,7 @@ public static void main(String[] args) {
4547
}
4648
}
4749

50+
@SuppressWarnings("deprecation")
4851
class OCBox extends JPanel implements Observer {
4952
Observable notifier;
5053
int x, y; // Locations in grid

patterns/ShapeFactory2.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ public Shape create(String id) {
2525
return (Shape)factories
2626
.computeIfAbsent(id, ShapeFactory2::load)
2727
.newInstance();
28-
} catch(InstantiationException |
29-
IllegalAccessException |
30-
InvocationTargetException e) {
28+
} catch(Exception e) {
3129
throw new BadShapeCreation(id);
3230
}
3331
}

patterns/observer/ObservedFlower.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package patterns.observer;
88
import java.util.*;
99

10+
@SuppressWarnings("deprecation")
1011
class Flower {
1112
private boolean isOpen;
1213
private boolean alreadyOpen;
@@ -46,6 +47,7 @@ public void notifyObservers() {
4647
}
4748
}
4849

50+
@SuppressWarnings("deprecation")
4951
class Bee {
5052
private String name;
5153
Bee(String nm) { name = nm; }
@@ -61,6 +63,7 @@ public Observer closeObserver() {
6163
}
6264
}
6365

66+
@SuppressWarnings("deprecation")
6467
class Hummingbird {
6568
private String name;
6669
Hummingbird(String nm) { name = nm; }

patterns/trash/Trash.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ public static <T extends Trash> T factory(Info info) {
7070
trashType.getConstructor(double.class);
7171
// Call the constructor to create a
7272
// new object:
73-
return (T)ctor.newInstance(info.data);
73+
return
74+
(T)ctor.newInstance(info.data);
7475
} catch(Exception e) {
7576
throw new CannotCreateTrashException(e);
7677
}

references/ImmutableInteger.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
public class ImmutableInteger {
1010
public static void main(String[] args) {
11+
@SuppressWarnings("deprecation")
1112
List<Integer> v = IntStream.range(0, 10)
1213
.mapToObj(Integer::new)
1314
.collect(Collectors.toList());

typeinfo/DynamicSupplier.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import java.util.function.*;
66
import java.util.stream.*;
77

8-
98
class CountedInteger {
109
private static long counter;
1110
private final long id = counter++;
@@ -18,11 +17,11 @@ public class DynamicSupplier<T> implements Supplier<T> {
1817
public DynamicSupplier(Class<T> type) {
1918
this.type = type;
2019
}
20+
@SuppressWarnings("deprecation")
2121
public T get() {
2222
try {
2323
return type.newInstance();
24-
} catch(InstantiationException |
25-
IllegalAccessException e) {
24+
} catch(Exception e) {
2625
throw new RuntimeException(e);
2726
}
2827
}

typeinfo/pets/PetCreator.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package typeinfo.pets;
77
import java.util.*;
88
import java.util.function.*;
9+
import java.lang.reflect.InvocationTargetException;
910

1011
public abstract
1112
class PetCreator implements Supplier<Pet> {
@@ -15,8 +16,11 @@ class PetCreator implements Supplier<Pet> {
1516
public Pet get() { // Create one random Pet
1617
int n = rand.nextInt(types().size());
1718
try {
18-
return types().get(n).newInstance();
19+
return types().get(n)
20+
.getConstructor().newInstance();
1921
} catch(InstantiationException |
22+
NoSuchMethodException |
23+
InvocationTargetException |
2024
IllegalAccessException e) {
2125
throw new RuntimeException(e);
2226
}

typeinfo/toys/GenericToyTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package typeinfo.toys;
88

99
public class GenericToyTest {
10+
@SuppressWarnings("deprecation")
1011
public static void
1112
main(String[] args) throws Exception {
1213
Class<FancyToy> ftClass = FancyToy.class;

typeinfo/toys/ToyTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Testing class Class
66
// {java typeinfo.toys.ToyTest}
77
package typeinfo.toys;
8+
import java.lang.reflect.InvocationTargetException;
89

910
interface HasBatteries {}
1011
interface Waterproof {}
@@ -31,6 +32,7 @@ static void printInfo(Class cc) {
3132
System.out.println(
3233
"Canonical name : " + cc.getCanonicalName());
3334
}
35+
@SuppressWarnings("deprecation")
3436
public static void main(String[] args) {
3537
Class c = null;
3638
try {
@@ -47,12 +49,9 @@ public static void main(String[] args) {
4749
try {
4850
// Requires no-arg constructor:
4951
obj = up.newInstance();
50-
} catch(InstantiationException e) {
51-
System.out.println("Cannot instantiate");
52-
System.exit(1);
53-
} catch(IllegalAccessException e) {
54-
System.out.println("Cannot access");
55-
System.exit(1);
52+
} catch(Exception e) {
53+
throw new
54+
RuntimeException("Cannot instantiate");
5655
}
5756
printInfo(obj.getClass());
5857
}

0 commit comments

Comments
 (0)