diff --git a/collectiontopics/CanonicalMapping.java b/collectiontopics/CanonicalMapping.java index 7ad7eb29..c02f9132 100644 --- a/collectiontopics/CanonicalMapping.java +++ b/collectiontopics/CanonicalMapping.java @@ -19,6 +19,7 @@ class Element { return r instanceof Element && Objects.equals(ident, ((Element)r).ident); } + @SuppressWarnings("deprecation") @Override protected void finalize() { System.out.println("Finalizing " + diff --git a/collectiontopics/References.java b/collectiontopics/References.java index ca505606..5fcca940 100644 --- a/collectiontopics/References.java +++ b/collectiontopics/References.java @@ -13,6 +13,7 @@ class VeryBig { VeryBig(String id) { ident = id; } @Override public String toString() { return ident; } + @SuppressWarnings("deprecation") @Override protected void finalize() { System.out.println("Finalizing " + ident); diff --git a/collectiontopics/SetOrder.java b/collectiontopics/SetOrder.java index 543db94c..1c25be23 100644 --- a/collectiontopics/SetOrder.java +++ b/collectiontopics/SetOrder.java @@ -23,7 +23,7 @@ public class SetOrder { for(String type: sets) { System.out.format("[-> %s <-]%n", type.substring(type.lastIndexOf('.') + 1)); - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "deprecation"}) Set set = (Set) Class.forName(type).newInstance(); set.addAll(RLIST); diff --git a/generics/DynamicProxyMixin.java b/generics/DynamicProxyMixin.java index 008efb12..38431e31 100644 --- a/generics/DynamicProxyMixin.java +++ b/generics/DynamicProxyMixin.java @@ -44,6 +44,7 @@ class MixinProxy implements InvocationHandler { public class DynamicProxyMixin { public static void main(String[] args) { + @SuppressWarnings("unchecked") Object mixin = MixinProxy.newInstance( tuple(new BasicImp(), Basic.class), tuple(new TimeStampedImp(), TimeStamped.class), diff --git a/generics/InstantiateGenericType.java b/generics/InstantiateGenericType.java index 5bd42e84..9424c9a1 100644 --- a/generics/InstantiateGenericType.java +++ b/generics/InstantiateGenericType.java @@ -3,18 +3,19 @@ // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. import java.util.function.*; +import java.lang.reflect.InvocationTargetException; class ClassAsFactory implements Supplier { Class kind; ClassAsFactory(Class kind) { this.kind = kind; } + @SuppressWarnings("deprecation") @Override public T get() { try { return kind.newInstance(); - } catch(InstantiationException | - IllegalAccessException e) { + } catch(Exception e) { throw new RuntimeException(e); } } diff --git a/generics/coffee/CoffeeSupplier.java b/generics/coffee/CoffeeSupplier.java index 8973dfbc..fe5dced8 100644 --- a/generics/coffee/CoffeeSupplier.java +++ b/generics/coffee/CoffeeSupplier.java @@ -7,6 +7,7 @@ package generics.coffee; import java.util.*; import java.util.function.*; import java.util.stream.*; +import java.lang.reflect.InvocationTargetException; public class CoffeeSupplier implements Supplier, Iterable { @@ -21,10 +22,13 @@ implements Supplier, Iterable { public Coffee get() { try { return (Coffee) - types[rand.nextInt(types.length)].newInstance(); + types[rand.nextInt(types.length)] + .getConstructor().newInstance(); // Report programmer errors at run time: - } catch(InstantiationException | - IllegalAccessException e) { + } catch(InstantiationException | + NoSuchMethodException | + InvocationTargetException | + IllegalAccessException e) { throw new RuntimeException(e); } } diff --git a/housekeeping/TerminationCondition.java b/housekeeping/TerminationCondition.java index 393540ac..dc8d347b 100644 --- a/housekeeping/TerminationCondition.java +++ b/housekeeping/TerminationCondition.java @@ -14,6 +14,7 @@ class Book { void checkIn() { checkedOut = false; } + @SuppressWarnings("deprecation") @Override public void finalize() { if(checkedOut) diff --git a/onjava/BasicSupplier.java b/onjava/BasicSupplier.java index 377d74d1..d4bf3b3d 100644 --- a/onjava/BasicSupplier.java +++ b/onjava/BasicSupplier.java @@ -5,6 +5,7 @@ // Supplier from a class with a no-arg constructor package onjava; import java.util.function.*; +import java.lang.reflect.InvocationTargetException; public class BasicSupplier implements Supplier { private Class type; @@ -15,8 +16,10 @@ public class BasicSupplier implements Supplier { public T get() { try { // Assumes type is a public class: - return type.newInstance(); + return type.getConstructor().newInstance(); } catch(InstantiationException | + NoSuchMethodException | + InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); } diff --git a/onjava/atunit/AtUnit.java b/onjava/atunit/AtUnit.java index a6c62127..b1688cbd 100644 --- a/onjava/atunit/AtUnit.java +++ b/onjava/atunit/AtUnit.java @@ -162,8 +162,11 @@ public class AtUnit implements ProcessFiles.Strategy { } } else { // Use the no-arg constructor: try { - return testClass.newInstance(); + return testClass + .getConstructor().newInstance(); } catch(InstantiationException | + NoSuchMethodException | + InvocationTargetException | IllegalAccessException e) { throw new RuntimeException( "Couldn't create a test object. " + diff --git a/patterns/BoxObserver.java b/patterns/BoxObserver.java index cb7e97d4..c6f9ec65 100644 --- a/patterns/BoxObserver.java +++ b/patterns/BoxObserver.java @@ -13,6 +13,7 @@ import onjava.*; import onjava.MouseClick; // You must inherit a new type of Observable: +@SuppressWarnings("deprecation") class BoxObservable extends Observable { @Override public void notifyObservers(Object b) { @@ -22,6 +23,7 @@ class BoxObservable extends Observable { } } +@SuppressWarnings("deprecation") public class BoxObserver extends JFrame { Observable notifier = new BoxObservable(); public BoxObserver(int grid) { @@ -45,6 +47,7 @@ public class BoxObserver extends JFrame { } } +@SuppressWarnings("deprecation") class OCBox extends JPanel implements Observer { Observable notifier; int x, y; // Locations in grid diff --git a/patterns/ShapeFactory2.java b/patterns/ShapeFactory2.java index 4f2134c4..23881105 100644 --- a/patterns/ShapeFactory2.java +++ b/patterns/ShapeFactory2.java @@ -25,9 +25,7 @@ public class ShapeFactory2 implements FactoryMethod { return (Shape)factories .computeIfAbsent(id, ShapeFactory2::load) .newInstance(); - } catch(InstantiationException | - IllegalAccessException | - InvocationTargetException e) { + } catch(Exception e) { throw new BadShapeCreation(id); } } diff --git a/patterns/observer/ObservedFlower.java b/patterns/observer/ObservedFlower.java index f759a105..009da63e 100644 --- a/patterns/observer/ObservedFlower.java +++ b/patterns/observer/ObservedFlower.java @@ -7,6 +7,7 @@ package patterns.observer; import java.util.*; +@SuppressWarnings("deprecation") class Flower { private boolean isOpen; private boolean alreadyOpen; @@ -46,6 +47,7 @@ class Flower { } } +@SuppressWarnings("deprecation") class Bee { private String name; Bee(String nm) { name = nm; } @@ -61,6 +63,7 @@ class Bee { } } +@SuppressWarnings("deprecation") class Hummingbird { private String name; Hummingbird(String nm) { name = nm; } diff --git a/patterns/trash/Trash.java b/patterns/trash/Trash.java index 40be944a..7d01de8c 100644 --- a/patterns/trash/Trash.java +++ b/patterns/trash/Trash.java @@ -70,7 +70,8 @@ public abstract class Trash { trashType.getConstructor(double.class); // Call the constructor to create a // new object: - return (T)ctor.newInstance(info.data); + return + (T)ctor.newInstance(info.data); } catch(Exception e) { throw new CannotCreateTrashException(e); } diff --git a/references/ImmutableInteger.java b/references/ImmutableInteger.java index ed4594b9..906aa7ef 100644 --- a/references/ImmutableInteger.java +++ b/references/ImmutableInteger.java @@ -8,6 +8,7 @@ import java.util.stream.*; public class ImmutableInteger { public static void main(String[] args) { + @SuppressWarnings("deprecation") List v = IntStream.range(0, 10) .mapToObj(Integer::new) .collect(Collectors.toList()); diff --git a/typeinfo/DynamicSupplier.java b/typeinfo/DynamicSupplier.java index bdde8c13..271af639 100644 --- a/typeinfo/DynamicSupplier.java +++ b/typeinfo/DynamicSupplier.java @@ -5,7 +5,6 @@ import java.util.function.*; import java.util.stream.*; - class CountedInteger { private static long counter; private final long id = counter++; @@ -18,11 +17,11 @@ public class DynamicSupplier implements Supplier { public DynamicSupplier(Class type) { this.type = type; } + @SuppressWarnings("deprecation") public T get() { try { return type.newInstance(); - } catch(InstantiationException | - IllegalAccessException e) { + } catch(Exception e) { throw new RuntimeException(e); } } diff --git a/typeinfo/pets/PetCreator.java b/typeinfo/pets/PetCreator.java index 5feb0940..76909aa9 100644 --- a/typeinfo/pets/PetCreator.java +++ b/typeinfo/pets/PetCreator.java @@ -6,6 +6,7 @@ package typeinfo.pets; import java.util.*; import java.util.function.*; +import java.lang.reflect.InvocationTargetException; public abstract class PetCreator implements Supplier { @@ -15,8 +16,11 @@ class PetCreator implements Supplier { public Pet get() { // Create one random Pet int n = rand.nextInt(types().size()); try { - return types().get(n).newInstance(); + return types().get(n) + .getConstructor().newInstance(); } catch(InstantiationException | + NoSuchMethodException | + InvocationTargetException | IllegalAccessException e) { throw new RuntimeException(e); } diff --git a/typeinfo/toys/GenericToyTest.java b/typeinfo/toys/GenericToyTest.java index aece7958..7dc016cd 100644 --- a/typeinfo/toys/GenericToyTest.java +++ b/typeinfo/toys/GenericToyTest.java @@ -7,6 +7,7 @@ package typeinfo.toys; public class GenericToyTest { + @SuppressWarnings("deprecation") public static void main(String[] args) throws Exception { Class ftClass = FancyToy.class; diff --git a/typeinfo/toys/ToyTest.java b/typeinfo/toys/ToyTest.java index 39d637c7..e4e5e92d 100644 --- a/typeinfo/toys/ToyTest.java +++ b/typeinfo/toys/ToyTest.java @@ -5,6 +5,7 @@ // Testing class Class // {java typeinfo.toys.ToyTest} package typeinfo.toys; +import java.lang.reflect.InvocationTargetException; interface HasBatteries {} interface Waterproof {} @@ -31,6 +32,7 @@ public class ToyTest { System.out.println( "Canonical name : " + cc.getCanonicalName()); } + @SuppressWarnings("deprecation") public static void main(String[] args) { Class c = null; try { @@ -47,12 +49,9 @@ public class ToyTest { try { // Requires no-arg constructor: obj = up.newInstance(); - } catch(InstantiationException e) { - System.out.println("Cannot instantiate"); - System.exit(1); - } catch(IllegalAccessException e) { - System.out.println("Cannot access"); - System.exit(1); + } catch(Exception e) { + throw new + RuntimeException("Cannot instantiate"); } printInfo(obj.getClass()); }