Frage zu java.lang.reflect.Proxy

Status
Nicht offen für weitere Antworten.

cn1h

Mitglied
hi, jetzt lerne ich Java mit dem Buch "CoreJava", kurz vor habe ich ein Programm gemacht, um java.lang.reflect.Proxy zu üben. Aber es gibt einige komische Fehler...

Gucken sie bitte folgende Code


Code:
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Arrays;
import java.util.Calendar;
import java.util.GregorianCalendar;

public class MainClass {
	
	public static void main(String[] arg){

		new MainClass();
	}
	
	public MainClass(){
		
		Object[] r = new Object[3];
		r[0] = Proxy.newProxyInstance(null, Rechnung.class.getInterfaces(), 
				new Detect(new Rechnung(new GregorianCalendar(2006, 9, 3), "Brot", 99.69)));
		r[1] = Proxy.newProxyInstance(null, Rechnung.class.getInterfaces(), 
				new Detect(new Rechnung(new GregorianCalendar(2006, 9, 1), "Bier", 1.26)));
		r[2] = Proxy.newProxyInstance(null, Rechnung.class.getInterfaces(), 
				new Detect(new Rechnung(new GregorianCalendar(2006, 9, 4), "Gebuhr", 120)));
		
		Arrays.sort(r);    [b]// Hier ist das Fehler.[/b]
	}
	
	private class Detect implements InvocationHandler{

		public Detect(Object target){
			this.target = target;
		}
		
		public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
			
			System.out.println(this.target.getClass().toString()+": "+method.getName());
			return method.invoke(this.target, args);
		}		
		private Object target;		
	}
	
}



class Rechnung implements Cloneable, Comparable<Rechnung>{
	
	public Rechnung(GregorianCalendar data, String name, double preis){
		this.data = data;
		this.name = name;
		this.preis = preis;
	}
	
	@Override public String toString(){
		return String.format("Die Rechnung ist: Date %d, Name %s, Preis %f",
				new Object[]{this.data.get(Calendar.DAY_OF_MONTH), this.name, this.preis});
		
	}
	
	@Override public Rechnung clone() throws CloneNotSupportedException{
		
		Rechnung result = (Rechnung)super.clone();
		return result;
	}
	
	public int compareTo(Rechnung aRechnung){
		
		if (this.data.before(aRechnung.data))
			return -1;
		if (this.data.after(aRechnung.data))
			return 1;
		return 0;
	}
	

	private GregorianCalendar data;
	private String name;
	private double preis;
}


Hier ist die Anzeige in Console:

class Rechnung: compareTo
Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
at $Proxy0.compareTo(Unknown Source)
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at MainClass.<init>(MainClass.java:26)
at MainClass.main(MainClass.java:13)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at MainClass$Detect.invoke(MainClass.java:41)
... 5 more
Caused by: java.lang.ClassCastException: $Proxy0
at Rechnung.compareTo(MainClass.java:1)
... 10 more

Konnten sie bitte mich sagen, wie zu korrigieren? Danke schön!
 
hi, also die api sagt dazu folgendes:


Thrown by a method invocation on a proxy instance if its invocation handler's invoke method throws a checked exception (a Throwable that is not assignable to RuntimeException or Error) that is not assignable to any of the exception types declared in the throws clause of the method that was invoked on the proxy instance and dispatched to the invocation handler.

An UndeclaredThrowableException instance contains the undeclared checked exception that was thrown by the invocation handler, and it can be retrieved with the getUndeclaredThrowable() method. UndeclaredThrowableException extends RuntimeException, so it is an unchecked exception that wraps a checked exception.

As of release 1.4, this exception has been retrofitted to conform to the general purpose exception-chaining mechanism. The "undeclared checked exception that was thrown by the invocation handler" that may be provided at construction time and accessed via the getUndeclaredThrowable() method is now known as the cause, and may be accessed via the Throwable.getCause() method, as well as the aforementioned "legacy method."


vlt. hilft dir das erstmal weiter.

gruesse
 
Status
Nicht offen für weitere Antworten.

Zurück
Oben