Skip to content

Commit

Permalink
Merge pull request #933 from apache/fix/WW-5415-constructor
Browse files Browse the repository at this point in the history
WW-5415 Fixes accessing public constructors via expression
  • Loading branch information
lukaszlenart committed May 14, 2024
2 parents d147543 + 7c523ac commit 40ccc74
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.struts2.ognl.ThreadAllowlist;

import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -147,11 +148,11 @@ public boolean isAccessible(Map context, Object target, Member member, String pr
if (target != null) {
// Special case: Target is a Class object but not Class.class
if (Class.class.equals(target.getClass()) && !Class.class.equals(target)) {
if (!isStatic(member)) {
throw new IllegalArgumentException("Member expected to be static!");
if (!isStatic(member) && !Constructor.class.equals(member.getClass())) {
throw new IllegalArgumentException("Member expected to be static or constructor!");
}
if (!member.getDeclaringClass().equals(target)) {
throw new IllegalArgumentException("Target class does not match static member!");
throw new IllegalArgumentException("Target class does not match member!");
}
target = null; // This information is not useful to us and conflicts with following logic which expects target to be null or an instance containing the member
// Standard case: Member should exist on target
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.opensymphony.xwork2.conversion.impl.ConversionData;
import org.easymock.EasyMock;

import java.sql.Date;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
Expand Down Expand Up @@ -142,6 +144,15 @@ public void testCollectionValidation() throws Exception {
assertEquals(1, errors.size());
}

public void testDateValidation() throws Exception {
action.setBirthday(Date.valueOf(LocalDate.now().minusYears(20)));
action.setContext("birthday");

validate("birthday");

assertFalse(action.hasFieldErrors());
}

public void testContextIsOverriddenByContextParamInValidationXML() throws Exception {
validate("visitorValidationAlias");
assertTrue(action.hasFieldErrors());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.opensymphony.xwork2.TestBean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


Expand All @@ -37,7 +38,7 @@ public class VisitorValidatorTestAction extends ActionSupport {
private String context;
private TestBean bean = new TestBean();
private TestBean[] testBeanArray;

private Date birthday;

public VisitorValidatorTestAction() {
testBeanArray = new TestBean[5];
Expand Down Expand Up @@ -80,4 +81,12 @@ public void setTestBeanList(List<TestBean> testBeanList) {
public List<TestBean> getTestBeanList() {
return testBeanList;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,12 @@
<message>You must enter a context.</message>
</field-validator>
</field>
<field name="birthday">
<field-validator type="fieldexpression">
<param name="expression"><![CDATA[
(birthday == null || birthday.before(new java.util.Date()))
]]></param>
<message key="errors_birthday" />
</field-validator>
</field>
</validators>

0 comments on commit 40ccc74

Please sign in to comment.