Builds and runs with JDK9
This commit is contained in:
parent
be5962bc22
commit
185255634d
@ -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 " +
|
||||
|
@ -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);
|
||||
|
@ -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<String> set = (Set<String>)
|
||||
Class.forName(type).newInstance();
|
||||
set.addAll(RLIST);
|
||||
|
@ -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),
|
||||
|
@ -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<T> implements Supplier<T> {
|
||||
Class<T> kind;
|
||||
ClassAsFactory(Class<T> 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);
|
||||
}
|
||||
}
|
||||
|
@ -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<Coffee>, Iterable<Coffee> {
|
||||
@ -21,9 +22,12 @@ implements Supplier<Coffee>, Iterable<Coffee> {
|
||||
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 |
|
||||
NoSuchMethodException |
|
||||
InvocationTargetException |
|
||||
IllegalAccessException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ class Book {
|
||||
void checkIn() {
|
||||
checkedOut = false;
|
||||
}
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
public void finalize() {
|
||||
if(checkedOut)
|
||||
|
@ -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<T> implements Supplier<T> {
|
||||
private Class<T> type;
|
||||
@ -15,8 +16,10 @@ public class BasicSupplier<T> implements Supplier<T> {
|
||||
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);
|
||||
}
|
||||
|
@ -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. " +
|
||||
|
@ -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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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; }
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -8,6 +8,7 @@ import java.util.stream.*;
|
||||
|
||||
public class ImmutableInteger {
|
||||
public static void main(String[] args) {
|
||||
@SuppressWarnings("deprecation")
|
||||
List<Integer> v = IntStream.range(0, 10)
|
||||
.mapToObj(Integer::new)
|
||||
.collect(Collectors.toList());
|
||||
|
@ -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<T> implements Supplier<T> {
|
||||
public DynamicSupplier(Class<T> type) {
|
||||
this.type = type;
|
||||
}
|
||||
@SuppressWarnings("deprecation")
|
||||
public T get() {
|
||||
try {
|
||||
return type.newInstance();
|
||||
} catch(InstantiationException |
|
||||
IllegalAccessException e) {
|
||||
} catch(Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
@ -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<Pet> {
|
||||
@ -15,8 +16,11 @@ class PetCreator implements Supplier<Pet> {
|
||||
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);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
package typeinfo.toys;
|
||||
|
||||
public class GenericToyTest {
|
||||
@SuppressWarnings("deprecation")
|
||||
public static void
|
||||
main(String[] args) throws Exception {
|
||||
Class<FancyToy> ftClass = FancyToy.class;
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user