Builds and runs with JDK9

This commit is contained in:
Bruce Eckel 2020-10-07 17:06:42 -06:00
parent be5962bc22
commit 185255634d
18 changed files with 46 additions and 22 deletions

View File

@ -19,6 +19,7 @@ class Element {
return r instanceof Element && return r instanceof Element &&
Objects.equals(ident, ((Element)r).ident); Objects.equals(ident, ((Element)r).ident);
} }
@SuppressWarnings("deprecation")
@Override @Override
protected void finalize() { protected void finalize() {
System.out.println("Finalizing " + System.out.println("Finalizing " +

View File

@ -13,6 +13,7 @@ class VeryBig {
VeryBig(String id) { ident = id; } VeryBig(String id) { ident = id; }
@Override @Override
public String toString() { return ident; } public String toString() { return ident; }
@SuppressWarnings("deprecation")
@Override @Override
protected void finalize() { protected void finalize() {
System.out.println("Finalizing " + ident); System.out.println("Finalizing " + ident);

View File

@ -23,7 +23,7 @@ public class SetOrder {
for(String type: sets) { for(String type: sets) {
System.out.format("[-> %s <-]%n", System.out.format("[-> %s <-]%n",
type.substring(type.lastIndexOf('.') + 1)); type.substring(type.lastIndexOf('.') + 1));
@SuppressWarnings("unchecked") @SuppressWarnings({"unchecked", "deprecation"})
Set<String> set = (Set<String>) Set<String> set = (Set<String>)
Class.forName(type).newInstance(); Class.forName(type).newInstance();
set.addAll(RLIST); set.addAll(RLIST);

View File

@ -44,6 +44,7 @@ class MixinProxy implements InvocationHandler {
public class DynamicProxyMixin { public class DynamicProxyMixin {
public static void main(String[] args) { public static void main(String[] args) {
@SuppressWarnings("unchecked")
Object mixin = MixinProxy.newInstance( Object mixin = MixinProxy.newInstance(
tuple(new BasicImp(), Basic.class), tuple(new BasicImp(), Basic.class),
tuple(new TimeStampedImp(), TimeStamped.class), tuple(new TimeStampedImp(), TimeStamped.class),

View File

@ -3,18 +3,19 @@
// We make no guarantees that this code is fit for any purpose. // We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information. // Visit http://OnJava8.com for more book information.
import java.util.function.*; import java.util.function.*;
import java.lang.reflect.InvocationTargetException;
class ClassAsFactory<T> implements Supplier<T> { class ClassAsFactory<T> implements Supplier<T> {
Class<T> kind; Class<T> kind;
ClassAsFactory(Class<T> kind) { ClassAsFactory(Class<T> kind) {
this.kind = kind; this.kind = kind;
} }
@SuppressWarnings("deprecation")
@Override @Override
public T get() { public T get() {
try { try {
return kind.newInstance(); return kind.newInstance();
} catch(InstantiationException | } catch(Exception e) {
IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

View File

@ -7,6 +7,7 @@ package generics.coffee;
import java.util.*; import java.util.*;
import java.util.function.*; import java.util.function.*;
import java.util.stream.*; import java.util.stream.*;
import java.lang.reflect.InvocationTargetException;
public class CoffeeSupplier public class CoffeeSupplier
implements Supplier<Coffee>, Iterable<Coffee> { implements Supplier<Coffee>, Iterable<Coffee> {
@ -21,9 +22,12 @@ implements Supplier<Coffee>, Iterable<Coffee> {
public Coffee get() { public Coffee get() {
try { try {
return (Coffee) return (Coffee)
types[rand.nextInt(types.length)].newInstance(); types[rand.nextInt(types.length)]
.getConstructor().newInstance();
// Report programmer errors at run time: // Report programmer errors at run time:
} catch(InstantiationException | } catch(InstantiationException |
NoSuchMethodException |
InvocationTargetException |
IllegalAccessException e) { IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -14,6 +14,7 @@ class Book {
void checkIn() { void checkIn() {
checkedOut = false; checkedOut = false;
} }
@SuppressWarnings("deprecation")
@Override @Override
public void finalize() { public void finalize() {
if(checkedOut) if(checkedOut)

View File

@ -5,6 +5,7 @@
// Supplier from a class with a no-arg constructor // Supplier from a class with a no-arg constructor
package onjava; package onjava;
import java.util.function.*; import java.util.function.*;
import java.lang.reflect.InvocationTargetException;
public class BasicSupplier<T> implements Supplier<T> { public class BasicSupplier<T> implements Supplier<T> {
private Class<T> type; private Class<T> type;
@ -15,8 +16,10 @@ public class BasicSupplier<T> implements Supplier<T> {
public T get() { public T get() {
try { try {
// Assumes type is a public class: // Assumes type is a public class:
return type.newInstance(); return type.getConstructor().newInstance();
} catch(InstantiationException | } catch(InstantiationException |
NoSuchMethodException |
InvocationTargetException |
IllegalAccessException e) { IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -162,8 +162,11 @@ public class AtUnit implements ProcessFiles.Strategy {
} }
} else { // Use the no-arg constructor: } else { // Use the no-arg constructor:
try { try {
return testClass.newInstance(); return testClass
.getConstructor().newInstance();
} catch(InstantiationException | } catch(InstantiationException |
NoSuchMethodException |
InvocationTargetException |
IllegalAccessException e) { IllegalAccessException e) {
throw new RuntimeException( throw new RuntimeException(
"Couldn't create a test object. " + "Couldn't create a test object. " +

View File

@ -13,6 +13,7 @@ import onjava.*;
import onjava.MouseClick; import onjava.MouseClick;
// You must inherit a new type of Observable: // You must inherit a new type of Observable:
@SuppressWarnings("deprecation")
class BoxObservable extends Observable { class BoxObservable extends Observable {
@Override @Override
public void notifyObservers(Object b) { public void notifyObservers(Object b) {
@ -22,6 +23,7 @@ class BoxObservable extends Observable {
} }
} }
@SuppressWarnings("deprecation")
public class BoxObserver extends JFrame { public class BoxObserver extends JFrame {
Observable notifier = new BoxObservable(); Observable notifier = new BoxObservable();
public BoxObserver(int grid) { public BoxObserver(int grid) {
@ -45,6 +47,7 @@ public class BoxObserver extends JFrame {
} }
} }
@SuppressWarnings("deprecation")
class OCBox extends JPanel implements Observer { class OCBox extends JPanel implements Observer {
Observable notifier; Observable notifier;
int x, y; // Locations in grid int x, y; // Locations in grid

View File

@ -25,9 +25,7 @@ public class ShapeFactory2 implements FactoryMethod {
return (Shape)factories return (Shape)factories
.computeIfAbsent(id, ShapeFactory2::load) .computeIfAbsent(id, ShapeFactory2::load)
.newInstance(); .newInstance();
} catch(InstantiationException | } catch(Exception e) {
IllegalAccessException |
InvocationTargetException e) {
throw new BadShapeCreation(id); throw new BadShapeCreation(id);
} }
} }

View File

@ -7,6 +7,7 @@
package patterns.observer; package patterns.observer;
import java.util.*; import java.util.*;
@SuppressWarnings("deprecation")
class Flower { class Flower {
private boolean isOpen; private boolean isOpen;
private boolean alreadyOpen; private boolean alreadyOpen;
@ -46,6 +47,7 @@ class Flower {
} }
} }
@SuppressWarnings("deprecation")
class Bee { class Bee {
private String name; private String name;
Bee(String nm) { name = nm; } Bee(String nm) { name = nm; }
@ -61,6 +63,7 @@ class Bee {
} }
} }
@SuppressWarnings("deprecation")
class Hummingbird { class Hummingbird {
private String name; private String name;
Hummingbird(String nm) { name = nm; } Hummingbird(String nm) { name = nm; }

View File

@ -70,7 +70,8 @@ public abstract class Trash {
trashType.getConstructor(double.class); trashType.getConstructor(double.class);
// Call the constructor to create a // Call the constructor to create a
// new object: // new object:
return (T)ctor.newInstance(info.data); return
(T)ctor.newInstance(info.data);
} catch(Exception e) { } catch(Exception e) {
throw new CannotCreateTrashException(e); throw new CannotCreateTrashException(e);
} }

View File

@ -8,6 +8,7 @@ import java.util.stream.*;
public class ImmutableInteger { public class ImmutableInteger {
public static void main(String[] args) { public static void main(String[] args) {
@SuppressWarnings("deprecation")
List<Integer> v = IntStream.range(0, 10) List<Integer> v = IntStream.range(0, 10)
.mapToObj(Integer::new) .mapToObj(Integer::new)
.collect(Collectors.toList()); .collect(Collectors.toList());

View File

@ -5,7 +5,6 @@
import java.util.function.*; import java.util.function.*;
import java.util.stream.*; import java.util.stream.*;
class CountedInteger { class CountedInteger {
private static long counter; private static long counter;
private final long id = counter++; private final long id = counter++;
@ -18,11 +17,11 @@ public class DynamicSupplier<T> implements Supplier<T> {
public DynamicSupplier(Class<T> type) { public DynamicSupplier(Class<T> type) {
this.type = type; this.type = type;
} }
@SuppressWarnings("deprecation")
public T get() { public T get() {
try { try {
return type.newInstance(); return type.newInstance();
} catch(InstantiationException | } catch(Exception e) {
IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

View File

@ -6,6 +6,7 @@
package typeinfo.pets; package typeinfo.pets;
import java.util.*; import java.util.*;
import java.util.function.*; import java.util.function.*;
import java.lang.reflect.InvocationTargetException;
public abstract public abstract
class PetCreator implements Supplier<Pet> { class PetCreator implements Supplier<Pet> {
@ -15,8 +16,11 @@ class PetCreator implements Supplier<Pet> {
public Pet get() { // Create one random Pet public Pet get() { // Create one random Pet
int n = rand.nextInt(types().size()); int n = rand.nextInt(types().size());
try { try {
return types().get(n).newInstance(); return types().get(n)
.getConstructor().newInstance();
} catch(InstantiationException | } catch(InstantiationException |
NoSuchMethodException |
InvocationTargetException |
IllegalAccessException e) { IllegalAccessException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }

View File

@ -7,6 +7,7 @@
package typeinfo.toys; package typeinfo.toys;
public class GenericToyTest { public class GenericToyTest {
@SuppressWarnings("deprecation")
public static void public static void
main(String[] args) throws Exception { main(String[] args) throws Exception {
Class<FancyToy> ftClass = FancyToy.class; Class<FancyToy> ftClass = FancyToy.class;

View File

@ -5,6 +5,7 @@
// Testing class Class // Testing class Class
// {java typeinfo.toys.ToyTest} // {java typeinfo.toys.ToyTest}
package typeinfo.toys; package typeinfo.toys;
import java.lang.reflect.InvocationTargetException;
interface HasBatteries {} interface HasBatteries {}
interface Waterproof {} interface Waterproof {}
@ -31,6 +32,7 @@ public class ToyTest {
System.out.println( System.out.println(
"Canonical name : " + cc.getCanonicalName()); "Canonical name : " + cc.getCanonicalName());
} }
@SuppressWarnings("deprecation")
public static void main(String[] args) { public static void main(String[] args) {
Class c = null; Class c = null;
try { try {
@ -47,12 +49,9 @@ public class ToyTest {
try { try {
// Requires no-arg constructor: // Requires no-arg constructor:
obj = up.newInstance(); obj = up.newInstance();
} catch(InstantiationException e) { } catch(Exception e) {
System.out.println("Cannot instantiate"); throw new
System.exit(1); RuntimeException("Cannot instantiate");
} catch(IllegalAccessException e) {
System.out.println("Cannot access");
System.exit(1);
} }
printInfo(obj.getClass()); printInfo(obj.getClass());
} }