How can I specify a structure field starting with upper case when working with MATLAB Production Server 1.0 (R2012b) Java client library?
2 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2013-10-25
回答: MathWorks Support Team
2013-10-25
The MATLAB code running on my MPS expects a structure with two fields: "field1" and "Field2" as input. It is important that the second field is written with a capital F. I tried to create this structure by implementing the following Java class:
public class MyStruct {
private String field1;
public String getField1() {
return field1;
}
public void setField1(String field1) {
this.field1 = field1;
}
private double Field2;
public double getField2() {
return Field2;
}
public void setField2(double field2) {
Field2 = field2;
}
}
But I notice that on the MATLAB side both fields in the stuct start with a lower case f.
采纳的回答
MathWorks Support Team
2013-10-25
When working with Java classes to represent MATLAB structures, it is good to know that the Java Beans Introspector (<http://docs.oracle.com/javase/6/docs/api/java/beans/Introspector.html>) is used to map properties to fields and its default naming conventions are used.
This means that by default its decapitalize method is used:
http://docs.oracle.com/javase/6/docs/api/java/beans/Introspector.html#decapitalize(java.lang.String)
And thus it would not be possible to define a property which will map to a structure field which starts with an upper case.
However this behavior can be overriden by implementing a BeanInfo class with a custom getPropertyDescriptors() method. For example:
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.beans.SimpleBeanInfo;
public class MyStructBeanInfo extends SimpleBeanInfo {
@Override
public PropertyDescriptor[] getPropertyDescriptors() {
PropertyDescriptor[] props = new PropertyDescriptor[2];
try {
// field1 uses default naming conventions so we do not need to explicitly specify the accessor names.
props[0] = new PropertyDescriptor("field1",MyStruct.class);
// Field2 uses a custom naming convention so we do need to explicitly specify the accessor names.
props[1] = new PropertyDescriptor("Field2",MyStruct.class,"getField2","setField2");
return props;
} catch (IntrospectionException e) {
e.printStackTrace();
}
return null;
}
}
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Java Client Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!