Skip to content

Commit

Permalink
ZK-5764: Unable to call original method when using custom ViewModelAn…
Browse files Browse the repository at this point in the history
…notationResolver
  • Loading branch information
DevChu authored and jumperchen committed Aug 15, 2024
1 parent 754c7a2 commit 42f06f1
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ public static <T extends Annotation> T getAnnotation(Class clazz, Class<T> annot
* @since 9.6.0
*/
public static Method getOriginalMethod(Object base, Method method) {
Method m;
Method m = null;
List<ViewModelAnnotationResolver> resolvers = getResolvers();
for (ViewModelAnnotationResolver resolver : resolvers) {
m = resolver.getOriginalMethod(base, method);
if (m != null)
if (m != null && !method.equals(m))
break;
}
return method;
return m;
}
}
1 change: 1 addition & 0 deletions zkdoc/release-note
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ ZK 10.1.0
* Features

* Bugs
ZK-5764: Unable to call original method when using custom ViewModelAnnotationResolver

* Upgrade Notes

Expand Down
33 changes: 33 additions & 0 deletions zktest/src/main/java/org/zkoss/zktest/test2/B101_ZK_5764VM.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* B101_ZK_5764VM.java
Purpose:
Description:
History:
Fri Aug 02 12:50:22 CST 2024, Created by jameschu
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.test2;

import java.lang.reflect.Method;

import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.init.ViewModelAnnotationResolvers;
import org.zkoss.zk.ui.util.Clients;

/**
* @author jameschu
*/
public class B101_ZK_5764VM {
@Command
public void update() {
try {
Method update = ViewModelAnnotationResolvers.getOriginalMethod(this, this.getClass().getMethod("update"));
Clients.log(update.getDeclaringClass().getName());
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
B101_ZK_5764ViewModelAnnotationHandler.java
Purpose:
Description:
History:
Fri Aug 02 12:50:22 CST 2024, Created by jameschu
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.test2;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

import org.zkoss.bind.ViewModelAnnotationResolver;

/**
* @author jameschu
*/
public class B101_ZK_5764ViewModelAnnotationHandler implements ViewModelAnnotationResolver {
public <T extends Annotation> T getAnnotation(Method method, Class<T> annotationClass) {
return method.getAnnotation(annotationClass);
}

public <T extends Annotation> T getAnnotation(Class clazz, Class<T> annotationClass) {
return (T) clazz.getAnnotation(annotationClass);
}

public Method getOriginalMethod(Object base, Method method) {
try {
String targetMethodName = "update";
if (targetMethodName.equals(method.getName())) {
return this.getClass().getMethod(targetMethodName);
}
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
}
return method;
}

public void update() {
}
}
4 changes: 4 additions & 0 deletions zktest/src/main/webapp/WEB-INF/zk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -844,4 +844,8 @@ Copyright (C) 2006 Potix Corporation. All Rights Reserved.
<!-- <name>org.zkoss.bind.defaultComposer.class</name>-->
<!-- <value>org.zkoss.zktest.test2.F100_ZK_BindComposer</value>-->
<!-- </library-property>-->
<!-- for ZK-5764 -->
<!-- <listener>-->
<!-- <listener-class>org.zkoss.zktest.test2.B101_ZK_5764ViewModelAnnotationHandler</listener-class>-->
<!-- </listener>-->
</zk>
6 changes: 6 additions & 0 deletions zktest/src/main/webapp/test2/B101-ZK-5764-zk.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<zk>
<listener>
<listener-class>org.zkoss.zktest.test2.B101_ZK_5764ViewModelAnnotationHandler</listener-class>
</listener>
</zk>
26 changes: 26 additions & 0 deletions zktest/src/main/webapp/test2/B101-ZK-5764.zul
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
B101-ZK-5764.zul
Purpose:
Description:
History:
Fri Aug 02 12:50:22 CST 2024, Created by jameschu
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
-->
<zk xmlns:w="client">
<label multiline="true"><![CDATA[
1. Add the following setting in zk.xml
<listener>
<listener-class>org.zkoss.zktest.test2.B101_ZK_5764ViewModelAnnotationHandler</listener-class>
</listener>
2. Click button, should see log "org.zkoss.zktest.test2.B101_ZK_5764ViewModelAnnotationHandler"
]]></label>
<div viewModel="@id('vm') @init('org.zkoss.zktest.test2.B101_ZK_5764VM')">
<button label="call cmd update" onClick="@command('update')"/>
</div>
</zk>
3 changes: 3 additions & 0 deletions zktest/src/main/webapp/test2/config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3125,6 +3125,9 @@ B90-ZK-4431.zul=A,E,Multislider
##zats##B100-ZK-5650.zul=A,E,Fileupload,WebSocket
##zats##B100-ZK-5722.zul=A,E,XSS,Security

## B101
##zats##B101-ZK-5764.zul=A,E,MVVM,Annotation,Method

##
# Features - 3.0.x version
##
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* B101_ZK_5764Test.java
Purpose:
Description:
History:
Fri Aug 02 12:50:22 CST 2024, Created by jameschu
Copyright (C) 2024 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zktest.zats.test2;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import org.zkoss.test.webdriver.ExternalZkXml;
import org.zkoss.test.webdriver.ForkJVMTestOnly;
import org.zkoss.zats.mimic.DesktopAgent;
import org.zkoss.zktest.zats.ZATSTestCase;

/**
* @author jameschu
*/
@ForkJVMTestOnly
public class B101_ZK_5764Test extends ZATSTestCase {
@RegisterExtension
public static final ExternalZkXml CONFIG = new ExternalZkXml("/test2/B101-ZK-5764-zk.xml");

@Test
public void test() {
DesktopAgent desktopAgent = connect();
desktopAgent.query("button").click();
assertEquals("org.zkoss.zktest.test2.B101_ZK_5764ViewModelAnnotationHandler", desktopAgent.getZkLog().get(0));
}
}

0 comments on commit 42f06f1

Please sign in to comment.