Browse Source

Do not use getters/setters when mapping designer fields (#17388)

Change-Id: I5c03beca2b5f00c6efb32980e5e5ddf36cc4f16a
tags/7.6.0.alpha7
Artur Signell 8 years ago
parent
commit
6d47320640

+ 12
- 11
server/src/com/vaadin/ui/declarative/FieldBinder.java View File

@@ -18,7 +18,6 @@ package com.vaadin.ui.declarative;
import java.beans.IntrospectionException;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@@ -28,7 +27,6 @@ import java.util.Map;
import java.util.logging.Logger;

import com.vaadin.ui.Component;
import com.vaadin.util.ReflectTools;

/**
* Binder utility that binds member fields of a design class instance to given
@@ -84,7 +82,7 @@ public class FieldBinder implements Serializable {
List<String> unboundFields = new ArrayList<String>();
for (Field f : fieldMap.values()) {
try {
Object value = ReflectTools.getJavaFieldValue(bindTarget, f);
Object value = getFieldValue(bindTarget, f);
if (value == null) {
unboundFields.add(f.getName());
}
@@ -92,8 +90,6 @@ public class FieldBinder implements Serializable {
throw new FieldBindingException("Could not get field value", e);
} catch (IllegalAccessException e) {
throw new FieldBindingException("Could not get field value", e);
} catch (InvocationTargetException e) {
throw new FieldBindingException("Could not get field value", e);
}
}
if (unboundFields.size() > 0) {
@@ -192,15 +188,14 @@ public class FieldBinder implements Serializable {
return false;
}
// validate that the field is not set
Object fieldValue = ReflectTools.getJavaFieldValue(bindTarget,
field);
Object fieldValue = getFieldValue(bindTarget, field);
if (fieldValue != null) {
getLogger().fine(
"The field \"" + fieldName
+ "\" was already mapped. Ignoring.");
} else {
// set the field value
ReflectTools.setJavaFieldValue(bindTarget, field, instance);
field.set(bindTarget, instance);
}
return true;
} catch (IllegalAccessException e) {
@@ -209,12 +204,18 @@ public class FieldBinder implements Serializable {
} catch (IllegalArgumentException e) {
throw new FieldBindingException("Field binding failed for "
+ identifier, e);
} catch (InvocationTargetException e) {
throw new FieldBindingException("Field binding failed for "
+ identifier, e);
}
}

private Object getFieldValue(Object object, Field field)
throws IllegalArgumentException, IllegalAccessException {
if (!field.isAccessible()) {
field.setAccessible(true);
}

return field.get(object);
}

/**
* Converts the given identifier to a valid field name by stripping away
* illegal character and setting the first letter of the name to lower case.

+ 82
- 0
server/tests/src/com/vaadin/tests/design/FieldNameWhichConflictsWithGettersTest.java View File

@@ -0,0 +1,82 @@
/*
* Copyright 2000-2014 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.tests.design;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.junit.Assert;
import org.junit.Test;

import com.vaadin.annotations.DesignRoot;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.declarative.Design;
import com.vaadin.ui.declarative.DesignContext;

public class FieldNameWhichConflictsWithGettersTest {

@DesignRoot("MyVerticalLayout.html")
public static class MyVerticalLayout extends VerticalLayout {
private Label caption;
private TextField description;

public MyVerticalLayout() {
Design.read(this);
}
}

@Test
public void readWithConflictingFields() {
MyVerticalLayout v = new MyVerticalLayout();
Assert.assertNotNull(v.caption);
Assert.assertNotNull(v.description);
}

@Test
public void writeWithConflictingFields() throws IOException {
VerticalLayout v = new VerticalLayout();
Label l = new Label();
l.setId("caption");
TextField tf = new TextField();
v.addComponents(l, tf);

ByteArrayOutputStream baos = new ByteArrayOutputStream();

DesignContext context = new DesignContext();
context.setComponentLocalId(tf, "description");
context.setRootComponent(v);

Design.write(context, baos);
String str = baos.toString("UTF-8");

Document doc = Jsoup.parse(str);
Element body = doc.body();
Element captionElement = body.getElementById("caption");
Assert.assertNotNull(captionElement);
Assert.assertEquals("vaadin-label", captionElement.tagName());

Element descriptionElement = captionElement.nextElementSibling();
Assert.assertNotNull(descriptionElement);
Assert.assertEquals("vaadin-text-field", descriptionElement.tagName());
Assert.assertEquals("description", descriptionElement.attr("_id"));

}
}

+ 4
- 0
server/tests/src/com/vaadin/tests/design/MyVerticalLayout.html View File

@@ -0,0 +1,4 @@
<v-vertical-layout>
<v-label _id="caption" />
<v-text-field id="description" />
</v-vertical-layout>

Loading…
Cancel
Save