Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZK-5764: Unable to call original method when using custom ViewModelAn… #3189

Merged
merged 2 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading