IllegalArgumentException in class: ****, setter method of property

博客提及类型格式转化错误,重点在于判断JavaBean类里对应表的字段的属性是否一致,涉及Java开发与数据库表字段匹配问题。

类型格式转化错误 判断javabean类里对应表的字段的属性是否一致。

 

package com.example.kucun2.entity.data; import android.util.Log; import androidx.annotation.NonNull; import com.google.gson.Gson; import java.io.IOException; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Proxy; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.Map; import java.util.Set; import okhttp3.Call; import okhttp3.Callback; import okhttp3.MediaType; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; /** * 支持数据变更自动同步的泛型集合类 * @param <T> 继承自 SynchronizableEntity 的实体类型 */ public class SynchronizedList<T extends SynchronizableEntity> implements List<T> { private final List<T> list = new ArrayList<>(); private final OkHttpClient client = new OkHttpClient(); private static final Gson gson = new Gson(); private static final MediaType JSON = MediaType.get("application/json; charset=utf-8"); private final Class<T> entityClass; // 类型标记 // 添加批量操作模式标志 private boolean batchMode = false; // 属性变更监听器 private final List<PropertyChangeListener<T>> listeners = new ArrayList<>(); // 直接接受Class参数的构造函数 public SynchronizedList(Class<T> entityClass) { this.entityClass = entityClass; } // 安全的类型转换方法 private T safeCast(Object o) { if (o != null && entityClass.isInstance(o)) { return entityClass.cast(o); } return null; } public interface PropertyChangeListener<T> { void onPropertyChange(T entity, String propertyName, Object oldValue, Object newValue); } public void addPropertyChangeListener(PropertyChangeListener<T> listener) { listeners.add(listener); } public void removePropertyChangeListener(PropertyChangeListener<T> listener) { listeners.remove(listener); } // 添加方法来获取原始对象 @SuppressWarnings("unchecked") public T getOriginal(T proxy) { if (proxy == null) return null; // 检查是否为代理对象 if (java.lang.reflect.Proxy.isProxyClass(proxy.getClass())) { try { java.lang.reflect.InvocationHandler handler = java.lang.reflect.Proxy.getInvocationHandler(proxy); if (handler instanceof IProxyHandler) { Object original = ((IProxyHandler) handler).getOriginal(); // 安全类型检查 if (entityClass.isInstance(original)) { return (T) original; } } } catch (Exception e) { Log.e("SynchronizedList", "Failed to get proxy handler", e); } } return proxy; } /** * 同步实体到后端 * * @param entity 要同步的实体 */ private void syncEntity(T entity) { if (batchMode) return; String operation = "add"; T originalEntity = getOriginal(entity); String endpoint = originalEntity.getEndpoint(operation); String json = gson.toJson(originalEntity); RequestBody body = RequestBody.create(json, JSON); Request request = new Request.Builder() .url(endpoint) .method(getHttpMethod(operation), body) .build(); client.newCall(request).enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException e) { Log.e("SynchronizedList", "同步失败: " + entity.getClass().getSimpleName(), e); } @Override public void onResponse(@NonNull Call call, @NonNull Response response) { if (!response.isSuccessful()) { Log.e("SynchronizedList", "同步失败: " + response.code() + " - " + response.message()); } response.close(); } }); } // 添加用于批量操作的方法 public void beginBatch() { this.batchMode = true; } public void endBatch() { this.batchMode = false; // 批量结束后触发一次全量同步 syncAllEntities(); } private void syncAllEntities() { for (T entity : list) { syncEntity(entity); } } private String getHttpMethod(String operation) { switch (operation) { case "add": return "POST"; case "update": return "PUT"; case "delete": return "DELETE"; default: return "POST"; } } // 获取类实现的所有接口(包括父类接口) private Class<?>[] getInterfaces(Class<?> clazz) { Set<Class<?>> interfaces = new HashSet<>(); while (clazz != null) { interfaces.addAll(Arrays.asList(clazz.getInterfaces())); clazz = clazz.getSuperclass(); } return interfaces.toArray(new Class<?>[0]); } /** * 创建代理对象,用于监听属性变更 */ // 优化 createProxy 方法 private T createProxy(T original) { if (original == null) return null; // 确保代理对象实现原始对象的所有接口+目标接口 Class<?>[] interfaces = new Class<?>[]{ SynchronizableEntity.class, // 强制实现目标接口 entityClass // 实体类或接口 }; return (T) Proxy.newProxyInstance( original.getClass().getClassLoader(), interfaces, new EntityProxyHandler(original) ); // if (!entityClass.isInstance(original)) { // throw new IllegalArgumentException("Invalid entity type"); // } // // 这里使用动态代理模式监听setter方法调用 // return (T) java.lang.reflect.Proxy.newProxyInstance( // original.getClass().getClassLoader(), // original.getClass().getInterfaces(), // (proxy, method, args) -> { // // 只拦截setter方法 // if (method.getName().startsWith("set") && args.length == 1) { // String propertyName = method.getName().substring(3); // propertyName = propertyName.substring(0, 1).toLowerCase() + propertyName.substring(1); // // // 获取旧值 // Object oldValue = original.getClass() // .getMethod("get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1)) // .invoke(original); // // // 调用原始方法 // Object result = method.invoke(original, args); // // // 触发监听器 // for (PropertyChangeListener<T> listener : listeners) { // listener.onPropertyChange(original, propertyName, oldValue, args[0]); // } // // // 自动同步到后端 // syncEntity(original); // return result; // } // return method.invoke(original, args); // } // ); } // 以下是List接口的实现 @Override public int size() { return list.size(); } @Override public boolean isEmpty() { return list.isEmpty(); } @Override public boolean contains(Object o) { return list.contains(o); } @NonNull @Override public Iterator<T> iterator() { return list.iterator(); } @NonNull @Override public Object[] toArray() { return list.toArray(); } @NonNull @Override public <T1> T1[] toArray(@NonNull T1[] a) { return list.toArray(a); } @Override public boolean add(T t) { T proxy = createProxy(t); boolean result = list.add(proxy); if (!batchMode) { syncEntity(proxy); } return result; // 添加时创建代理 } @Override public boolean remove(Object o) { T entity = safeCast(o); if (entity != null) { syncEntity((T) entity); // 删除前同步 } return list.remove(o); } @Override public boolean containsAll(@NonNull Collection<?> c) { return list.containsAll(c); } @Override public boolean addAll(@NonNull Collection<? extends T> c) { boolean modified = false; for (T t : c) { modified |= add(t); } return modified; } @Override public boolean addAll(int index, @NonNull Collection<? extends T> c) { for (T t : c) { add(index++, t); } return true; } @Override public boolean removeAll(@NonNull Collection<?> c) { for (Object o : c) { T entity = safeCast(o); if (entity != null) { syncEntity(entity); } } return list.removeAll(c); } @Override public boolean retainAll(@NonNull Collection<?> c) { List<T> toRemove = new ArrayList<>(); for (T t : list) { if (!c.contains(t)) { toRemove.add(t); } } // 使用安全转换 for (T entity : toRemove) { syncEntity(entity); } return list.retainAll(c); } @Override public void clear() { for (T t : list) { syncEntity(t); // 清空前同步 } list.clear(); } @Override public T get(int index) { return list.get(index); } @Override public T set(int index, T element) { T old = list.set(index, createProxy(element)); if (old != null) { syncEntity(old); // 替换旧元素前同步 } return old; } @Override public void add(int index, T element) { list.add(index, createProxy(element)); } @Override public T remove(int index) { T removed = list.remove(index); if (removed != null) { syncEntity(removed); // 删除前同步 } return removed; } @Override public int indexOf(Object o) { return list.indexOf(o); } @Override public int lastIndexOf(Object o) { return list.lastIndexOf(o); } @NonNull @Override public ListIterator<T> listIterator() { return list.listIterator(); } @NonNull @Override public ListIterator<T> listIterator(int index) { return list.listIterator(index); } @NonNull @Override public List<T> subList(int fromIndex, int toIndex) { return list.subList(fromIndex, toIndex); } // 触发属性变更通知 // 添加类型安全的属性监听 private void firePropertyChange(T entity, String propertyName, Object oldValue, Object newValue) { if (oldValue != null && newValue != null && oldValue.equals(newValue)) { return; // 值未变化时不触发监听 } for (PropertyChangeListener<T> listener : listeners) { try { listener.onPropertyChange(entity, propertyName, oldValue, newValue); } catch (Exception e) { Log.e("SynchronizedList", "Listener error", e); } } } // 修改代理处理器以支持获取原始对象 /* 完整的 EntityProxyHandler 实现 */ private class EntityProxyHandler implements IProxyHandler, java.lang.reflect.InvocationHandler { private final T original; private final Map<String, Object> propertyValues = new HashMap<>(); public EntityProxyHandler(T original) { this.original = original; // 初始化属性缓存 for (Method method : original.getClass().getMethods()) { if (method.getName().startsWith("get") && method.getParameterCount() == 0) { try { String propName = method.getName().substring(3); propName = Character.toLowerCase(propName.charAt(0)) + propName.substring(1); propertyValues.put(propName, method.invoke(original)); } catch (Exception e) { Log.e("ProxyHandler", "Failed to cache property", e); } } } } @Override public T getOriginal() { return original; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); // 处理 setter 方法 if (methodName.startsWith("set") && args != null && args.length == 1) { String propertyName = methodName.substring(3); propertyName = Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1); Object oldValue = propertyValues.get(propertyName); Object newValue = args[0]; // 调用原始方法 Object result = method.invoke(original, args); propertyValues.put(propertyName, newValue); // 触发监听 firePropertyChange(original, propertyName, oldValue, newValue); // 非批量模式下自动同步 if (!batchMode) { syncEntity(original); } return result; } // 特殊处理 getOriginal 方法 if ("getOriginal".equals(methodName)) { return original; } // 处理 getter 方法(使用缓存) if (methodName.startsWith("get") && method.getParameterCount() == 0) { String propertyName = methodName.substring(3); propertyName = Character.toLowerCase(propertyName.charAt(0)) + propertyName.substring(1); return propertyValues.getOrDefault(propertyName, method.invoke(original)); } return method.invoke(original, args); } private Object getPropertyValue(String propertyName) { String getterName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); try { Method getter = original.getClass().getMethod(getterName); return getter.invoke(original); } catch (Exception e) { Log.e("ProxyHandler", "Failed to get property value: " + propertyName, e); return null; } } } // 在 SynchronizedList 类中添加 private interface IProxyHandler { Object getOriginal(); } } E FATAL EXCEPTION: main Process: com.example.kucun2, PID: 22195 java.lang.IllegalArgumentException: com.example.kucun2.entity.data.SynchronizableEntity is not an interface at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:635) at java.lang.reflect.Proxy$ProxyClassFactory.apply(Proxy.java:602) at java.lang.reflect.WeakCache$Factory.get(WeakCache.java:230) at java.lang.reflect.WeakCache.get(WeakCache.java:127) at java.lang.reflect.Proxy.getProxyClass0(Proxy.java:438) at java.lang.reflect.Proxy.newProxyInstance(Proxy.java:873) at com.example.kucun2.entity.data.SynchronizedList.createProxy(SynchronizedList.java:186) at com.example.kucun2.entity.data.SynchronizedList.add(SynchronizedList.java:263) at com.example.kucun2.ui.jinhuo.AddInventoryFragment.createAndSaveDingdan(AddInventoryFragment.java:445) at com.example.kucun2.ui.jinhuo.AddInventoryFragment.lambda$showNewDingdanDialog$8(AddInventoryFragment.java:423) at com.example.kucun2.ui.jinhuo.AddInventoryFragment.$r8$lambda$viLBw5kNmkDSVG17JoXzuH5G85E(Unknown Source:0) at com.example.kucun2.ui.jinhuo.AddInventoryFragment$$ExternalSyntheticLambda20.onClick(D8$$SyntheticClass:0) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:250) at android.os.Handler.dispatchMessage(Handler.java:109) at android.os.Looper.loopOnce(Looper.java:250) at android.os.Looper.loop(Looper.java:340) at android.app.ActivityThread.main(ActivityThread.java:9913) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:621) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:957)
06-10
Part 2: Java You are going to make a system that represents the seats in a venue, such as a theatre or a cinema, and a process for booking seats at events that are held in that venue. The tasks will get harder as you progress, and build on each other, so try to ensure that you complete each stage before moving on. We have provided initial versions of Seat.java and SeatType.java which you should use to complete the tasks. You can add any additional fields, methods, or modifiers to these classes as needed, and can also write and submit any additional classes that are needed, but please do not change the provided packages, class names, or method names. Here are the initial versions of the two files -- you should download these and use them to create your solution. SeatType.java Seat.java All classes you create should be in the boxOffice package. Java Task 2a: Representing individual seats (8 Marks) For our purposes, a seat has four core properties: The row, which must be a single character representing a capital letter A through Z The seat number, which must be a positive integer The seat type, which is represented by the provided SeatType enumerated type The availability, which is a Boolean value indicating whether the seat has been reserved 1. Write code to represent individual seats using the above properties. The full details of the implementation are up to you, but you must use the provided Seat.java file as a starting point for representing the core properties. A seat should be initially available when it is created, while the other properties should be set in the constructor. [4 marks] 2. Be sure that your constructor validates that the values of all core properties are valid: that is, the row and seat number are in range. If an attempt is made to create a seat object with invalid values, your constructor should throw an IllegalArgumentException with a descriptive message. [2 marks] 3. Include getter methods for all core properties, as well as a setter method for the availability property only. [1 mark] Java Task 2b: Representing a venue (8 Marks) A venue is a location such as a theatre which has a number of rows, each of which has a number of seats. The row letters always start at A, and the seat numbers always start at 1. Note that the number and distribution of seats may be different in each row. There can be at most 26 rows. For example, the following might be the seat configuration in a very small venue: Row/Seat 1 2 3 4 5 6 A Standard Deluxe Deluxe Standard B Deluxe Deluxe Deluxe C Standard Standard Standard Standard Standard Standard A venue configuration can be given by a string of the following format: - First line: number of rows - Remaining lines: seat configuration in each row, separated by spaces For example, the above venue would be specified by the following string: 3 S D D S D D D S S S S S S In Java, you could specify this string like this: “3\nS D D S\nD D D\nS S S S S S” 1. Create a class called Venue to represent the seats in a venue as described above, using your Seat class from Task 2a. You can use whatever internal representation you choose. Your class should have one constructor which should take a string like above and create the necessary Seat objects. You can assume that the string is well-formed and do not need to do any error checking. [4 marks] 2. Add a method to the Venue class with the following signature: public Seat getSeat (char row, int seatNum) This method should return the Seat object corresponding to the given row and seat number. If the row or seat number is invalid, this method should throw an IllegalArgumentException with a descriptive message. [2 marks] 3. Add a method to the Venue class with the following signature: public void printDetails() This method should print out the details of all seats in all rows of the venue in a human-readable form. The details are up to you, but simply using toString() on the internal representation will not be sufficient for full marks. [2 marks] Java Task 2c: Representing an event (8 Marks) An Event is a particular show that takes place at a venue. The details of an event include: The venue where the event will take place A price structure for tickets – that is, the price in pounds of each ticket type. For example, this might be: Standard tickets: £10 Deluxe tickets: £20 An event also allows seats to be reserved – a customer can request one or more seats of a given type (Standard or Deluxe). If a customer is booking more than one seat, the seats must be adjacent – that is, the seats must have consecutive numbers and be in the same row. Once a seat has been reserved by a customer, it cannot be reserved by another unless the seat is returned. 1. Create an Event class to represent the details of an event, using the classes from the previous tasks. You can assume that all ticket prices can be represented as integers. [2 marks] 2. Add a method reserveSeats to the Event class with the following signature: public int reserveSeats (int numSeats, SeatType seatType) This method should attempt to find the indicated number of adjacent seats of the given type that are available. If enough seats are found, they should all be reserved, and the total price of the seats returned. If enough seats are not found, no seats should be reserved and the method should return -1. [4 marks] 3. Add a method returnSeat to the Event class with the following signature: public void returnSeat (char row, intSeatNum) If the indicated seat is reserved, it should be made available again. If the indicated seat is already available, or if the row and seat number are invalid, this method should throw an IllegalArgumentException with appropriate information. [2 marks] Java submission Be sure that your classes are named as above, and that all classes are in the boxOffice package. You must submit all the .java files you wrote, including the final versions of Seat.java and SeatType.java. 我的seattype是package boxOffice; public enum SeatType { STANDARD, DELUXE; } 按照题目中给的变量和参数执行
最新发布
12-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值