diff options
Diffstat (limited to 'server/src/com')
-rw-r--r-- | server/src/com/vaadin/server/JsonCodec.java | 149 | ||||
-rw-r--r-- | server/src/com/vaadin/server/communication/ClientRpcWriter.java | 6 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/DateField.java | 3 | ||||
-rw-r--r-- | server/src/com/vaadin/ui/GridLayout.java | 141 |
4 files changed, 136 insertions, 163 deletions
diff --git a/server/src/com/vaadin/server/JsonCodec.java b/server/src/com/vaadin/server/JsonCodec.java index 08345714fd..93074abcdb 100644 --- a/server/src/com/vaadin/server/JsonCodec.java +++ b/server/src/com/vaadin/server/JsonCodec.java @@ -27,7 +27,6 @@ import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.lang.reflect.WildcardType; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -435,21 +434,6 @@ public class JsonCodec implements Serializable { return new UidlValue(decodedValue); } - private static boolean transportTypesCompatible( - String encodedTransportType, String transportType) { - if (encodedTransportType == null) { - return false; - } - if (encodedTransportType.equals(transportType)) { - return true; - } - if (encodedTransportType.equals(JsonConstants.VTYPE_NULL)) { - return true; - } - - return false; - } - private static Map<Object, Object> decodeMap(Type targetType, boolean restrictToInternalTypes, Object jsonMap, ConnectorTracker connectorTracker) throws JSONException { @@ -588,7 +572,8 @@ public class JsonCodec implements Serializable { private static Object[] decodeObjectArray(Type targetType, JSONArray jsonArray, ConnectorTracker connectorTracker) throws JSONException { - List list = decodeList(List.class, true, jsonArray, connectorTracker); + List<Object> list = decodeList(List.class, true, jsonArray, + connectorTracker); return list.toArray(new Object[list.size()]); } @@ -644,80 +629,59 @@ public class JsonCodec implements Serializable { Type valueType, ConnectorTracker connectorTracker) throws JSONException { - if (valueType == null) { - throw new IllegalArgumentException("type must be defined"); - } - - if (valueType instanceof WildcardType) { - throw new IllegalStateException( - "Can not serialize type with wildcard: " + valueType); - } - if (null == value) { return ENCODE_RESULT_NULL; } - if (value instanceof String[]) { - String[] array = (String[]) value; - JSONArray jsonArray = new JSONArray(); - for (int i = 0; i < array.length; ++i) { - jsonArray.put(array[i]); - } - return new EncodeResult(jsonArray); - } else if (value instanceof String) { - return new EncodeResult(value); - } else if (value instanceof Boolean) { - return new EncodeResult(value); - } else if (value instanceof Number) { - return new EncodeResult(value); - } else if (value instanceof Character) { - // Character is not a Number - return new EncodeResult(value); + // Storing a single reference and only returning the EncodeResult at the + // end the method is much shorter in bytecode which allows inlining + Object toReturn; + + if (value instanceof String || value instanceof Boolean + || value instanceof Number || value instanceof Character + || value instanceof JSONArray || value instanceof JSONObject) { + // all JSON compatible types are returned as is. + toReturn = value; + } else if (value instanceof String[]) { + toReturn = toJsonArray((String[]) value); } else if (value instanceof Collection) { - Collection<?> collection = (Collection<?>) value; - JSONArray jsonArray = encodeCollection(valueType, collection, - connectorTracker); - return new EncodeResult(jsonArray); - } else if (valueType instanceof Class<?> - && ((Class<?>) valueType).isArray()) { - JSONArray jsonArray = encodeArrayContents( - ((Class<?>) valueType).getComponentType(), value, - connectorTracker); - return new EncodeResult(jsonArray); - } else if (valueType instanceof GenericArrayType) { - Type componentType = ((GenericArrayType) valueType) - .getGenericComponentType(); - JSONArray jsonArray = encodeArrayContents(componentType, value, + toReturn = encodeCollection(valueType, (Collection<?>) value, connectorTracker); - return new EncodeResult(jsonArray); } else if (value instanceof Map) { - Object jsonMap = encodeMap(valueType, (Map<?, ?>) value, - connectorTracker); - return new EncodeResult(jsonMap); + toReturn = encodeMap(valueType, (Map<?, ?>) value, connectorTracker); } else if (value instanceof Connector) { - Connector connector = (Connector) value; if (value instanceof Component && !(LegacyCommunicationManager .isComponentVisibleToClient((Component) value))) { + // an encoded null is cached, return it directly. return ENCODE_RESULT_NULL; } - return new EncodeResult(connector.getConnectorId()); + // Connectors are simply serialized as ID. + toReturn = ((Connector) value).getConnectorId(); } else if (value instanceof Enum) { - return encodeEnum((Enum<?>) value, connectorTracker); - } else if (value instanceof JSONArray || value instanceof JSONObject) { - return new EncodeResult(value); + toReturn = ((Enum<?>) value).name(); } else if (customSerializers.containsKey(value.getClass())) { - JSONSerializer serializer = customSerializers.get(value.getClass()); - return new EncodeResult(serializer.serialize(value, - connectorTracker)); + toReturn = serializeJson(value, connectorTracker); + } else if (valueType instanceof GenericArrayType) { + toReturn = encodeArrayContents( + ((GenericArrayType) valueType).getGenericComponentType(), + value, connectorTracker); } else if (valueType instanceof Class<?>) { - // Any object that we do not know how to encode we encode by looping - // through fields - return encodeObject(value, (Class<?>) valueType, - (JSONObject) diffState, connectorTracker); + if (((Class<?>) valueType).isArray()) { + toReturn = encodeArrayContents( + ((Class<?>) valueType).getComponentType(), value, + connectorTracker); + } else { + // encodeObject returns an EncodeResult with a diff, thus it + // needs to return it directly rather than assigning it to + // toReturn. + return encodeObject(value, (Class<?>) valueType, + (JSONObject) diffState, connectorTracker); + } } else { - throw new JSONException("Can not encode " + valueType); + throw new JSONException("Can not encode type " + valueType); } + return new EncodeResult(toReturn); } public static Collection<BeanProperty> getProperties(Class<?> type) @@ -737,6 +701,9 @@ public class JsonCodec implements Serializable { return properties; } + /* + * Loops through the fields of value and encodes them. + */ private static EncodeResult encodeObject(Object value, Class<?> valueType, JSONObject referenceValue, ConnectorTracker connectorTracker) throws JSONException { @@ -812,11 +779,6 @@ public class JsonCodec implements Serializable { } } - private static EncodeResult encodeEnum(Enum<?> e, - ConnectorTracker connectorTracker) throws JSONException { - return new EncodeResult(e.name()); - } - private static JSONArray encodeArrayContents(Type componentType, Object array, ConnectorTracker connectorTracker) throws JSONException { @@ -830,7 +792,7 @@ public class JsonCodec implements Serializable { } private static JSONArray encodeCollection(Type targetType, - Collection collection, ConnectorTracker connectorTracker) + Collection<?> collection, ConnectorTracker connectorTracker) throws JSONException { JSONArray jsonArray = new JSONArray(); for (Object o : collection) { @@ -898,6 +860,9 @@ public class JsonCodec implements Serializable { return new JSONArray(Arrays.asList(keys, values)); } + /* + * Encodes a connector map. Invisible connectors are skipped. + */ private static JSONObject encodeConnectorMap(Type valueType, Map<?, ?> map, ConnectorTracker connectorTracker) throws JSONException { JSONObject jsonMap = new JSONObject(); @@ -929,21 +894,27 @@ public class JsonCodec implements Serializable { return jsonMap; } - /** - * Gets the transport type for the given class. Returns null if no transport - * type can be found. - * - * @param valueType - * The type that should be transported - * @return - * @throws JSONException + /* + * These methods looks good to inline, but are on a cold path of the + * otherwise hot encode method, which needed to be shorted to allow inlining + * of the hot part. */ private static String getInternalTransportType(Type valueType) { return typeToTransportType.get(getClassForType(valueType)); } - private static String getCustomTransportType(Class<?> targetType) { - return targetType.getName(); + private static Object serializeJson(Object value, + ConnectorTracker connectorTracker) { + JSONSerializer serializer = customSerializers.get(value.getClass()); + return serializer.serialize(value, connectorTracker); + } + + private static JSONArray toJsonArray(String[] array) { + JSONArray jsonArray = new JSONArray(); + for (int i = 0; i < array.length; ++i) { + jsonArray.put(array[i]); + } + return jsonArray; } } diff --git a/server/src/com/vaadin/server/communication/ClientRpcWriter.java b/server/src/com/vaadin/server/communication/ClientRpcWriter.java index 1090fdbab9..181bfbb882 100644 --- a/server/src/com/vaadin/server/communication/ClientRpcWriter.java +++ b/server/src/com/vaadin/server/communication/ClientRpcWriter.java @@ -81,9 +81,9 @@ public class ClientRpcWriter implements Serializable { // + parameterType.getName()); // } // } - EncodeResult encodeResult = JsonCodec.encode( - invocation.getParameters()[i], referenceParameter, - parameterType, ui.getConnectorTracker()); + EncodeResult encodeResult = JsonCodec.encode(invocation.getParameters()[i], + referenceParameter, parameterType, + ui.getConnectorTracker()); paramJson.put(encodeResult.getEncodedValue()); } invocationJson.put(paramJson); diff --git a/server/src/com/vaadin/ui/DateField.java b/server/src/com/vaadin/ui/DateField.java index 7ab7732079..e98b1e1b31 100644 --- a/server/src/com/vaadin/ui/DateField.java +++ b/server/src/com/vaadin/ui/DateField.java @@ -152,7 +152,8 @@ public class DateField extends AbstractField<Date> implements private String dateOutOfRangeMessage = "Date is out of allowed range"; private DateRangeValidator currentRangeValidator; - { + + static { variableNameForResolution.put(Resolution.SECOND, "sec"); variableNameForResolution.put(Resolution.MINUTE, "min"); variableNameForResolution.put(Resolution.HOUR, "hour"); diff --git a/server/src/com/vaadin/ui/GridLayout.java b/server/src/com/vaadin/ui/GridLayout.java index 00e50aafc4..989f5efdea 100644 --- a/server/src/com/vaadin/ui/GridLayout.java +++ b/server/src/com/vaadin/ui/GridLayout.java @@ -1,12 +1,12 @@ /* * 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 @@ -39,12 +39,12 @@ import com.vaadin.shared.ui.gridlayout.GridLayoutState.ChildComponentData; /** * A layout where the components are laid out on a grid using cell coordinates. - * + * * <p> * The GridLayout also maintains a cursor for adding components in * left-to-right, top-to-bottom order. * </p> - * + * * <p> * Each component in a <code>GridLayout</code> uses a defined * {@link GridLayout.Area area} (column1,row1,column2,row2) from the grid. The @@ -52,12 +52,12 @@ import com.vaadin.shared.ui.gridlayout.GridLayoutState.ChildComponentData; * you will get an {@link OverlapsException}. Adding a component with cursor * automatically extends the grid by increasing the grid height. * </p> - * + * * <p> * The grid coordinates, which are specified by a row and column index, always * start from 0 for the topmost row and the leftmost column. * </p> - * + * * @author Vaadin Ltd. * @since 3.0 */ @@ -96,10 +96,10 @@ public class GridLayout extends AbstractLayout implements /** * Constructor for a grid of given size (number of columns and rows). - * + * * The grid may grow or shrink later. Grid grows automatically if you add * components outside its area. - * + * * @param columns * Number of columns in the grid. * @param rows @@ -121,9 +121,9 @@ public class GridLayout extends AbstractLayout implements /** * Constructs a GridLayout of given size (number of columns and rows) and * adds the given components in order to the grid. - * + * * @see #addComponents(Component...) - * + * * @param columns * Number of columns in the grid. * @param rows @@ -147,13 +147,13 @@ public class GridLayout extends AbstractLayout implements * by specifying the upper left corner (column1, row1) and the lower right * corner (column2, row2) of the area. The coordinates are zero-based. * </p> - * + * * <p> * If the area overlaps with any of the existing components already present * in the grid, the operation will fail and an {@link OverlapsException} is * thrown. * </p> - * + * * @param component * the component to be added, not <code>null</code>. * @param column1 @@ -257,7 +257,7 @@ public class GridLayout extends AbstractLayout implements /** * Tests if the given area overlaps with any of the items already on the * grid. - * + * * @param area * the Area to be checked for overlapping. * @throws OverlapsException @@ -279,7 +279,7 @@ public class GridLayout extends AbstractLayout implements * the area.) End coordinates (SouthEast corner of the area) are the same as * column1,row1. The coordinates are zero-based. Component width and height * is 1. - * + * * @param component * the component to be added, not <code>null</code>. * @param column @@ -299,16 +299,16 @@ public class GridLayout extends AbstractLayout implements /** * Forces the next component to be added at the beginning of the next line. - * + * * <p> * Sets the cursor column to 0 and increments the cursor row by one. * </p> - * + * * <p> * By calling this function you can ensure that no more components are added * right of the previous component. * </p> - * + * * @see #space() */ public void newLine() { @@ -319,7 +319,7 @@ public class GridLayout extends AbstractLayout implements /** * Moves the cursor forward by one. If the cursor goes out of the right grid * border, it is moved to the first column of the next row. - * + * * @see #newLine() */ public void space() { @@ -335,7 +335,7 @@ public class GridLayout extends AbstractLayout implements * cursor position is already occupied, the cursor is moved forwards to find * free position. If the cursor goes out from the bottom of the grid, the * grid is automatically extended. - * + * * @param component * the component to be added, not <code>null</code>. */ @@ -371,7 +371,7 @@ public class GridLayout extends AbstractLayout implements /** * Removes the specified component from the layout. - * + * * @param component * the component to be removed. */ @@ -391,7 +391,7 @@ public class GridLayout extends AbstractLayout implements /** * Removes the component specified by its cell coordinates. - * + * * @param column * the component's column, starting from 0. * @param row @@ -414,7 +414,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets an Iterator for the components contained in the layout. By using the * Iterator it is possible to step through the contents of the layout. - * + * * @return the Iterator of the components inside the layout. */ @Override @@ -425,7 +425,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the number of components contained in the layout. Consistent with * the iterator returned by {@link #getComponentIterator()}. - * + * * @return the number of contained components */ @Override @@ -440,7 +440,7 @@ public class GridLayout extends AbstractLayout implements /** * Paints the contents of this component. - * + * * @param target * the Paint Event. * @throws PaintException @@ -497,7 +497,6 @@ public class GridLayout extends AbstractLayout implements if (columnExpandRatioArray.length > 0) { columnExpandRatioArray[0] -= realColExpandRatioSum - 1000; } - target.addAttribute("colExpand", columnExpandRatioArray); target.addAttribute("rowExpand", rowExpandRatioArray); @@ -532,17 +531,17 @@ public class GridLayout extends AbstractLayout implements /** * Defines a rectangular area of cells in a GridLayout. - * + * * <p> * Also maintains a reference to the component contained in the area. * </p> - * + * * <p> * The area is specified by the cell coordinates of its upper left corner * (column1,row1) and lower right corner (column2,row2). As otherwise with * GridLayout, the column and row coordinates start from zero. * </p> - * + * * @author Vaadin Ltd. * @since 3.0 */ @@ -554,7 +553,7 @@ public class GridLayout extends AbstractLayout implements * <p> * Construct a new area on a grid. * </p> - * + * * @param component * the component connected to the area. * @param column1 @@ -588,7 +587,7 @@ public class GridLayout extends AbstractLayout implements /** * Tests if this Area overlaps with another Area. - * + * * @param other * the other Area that is to be tested for overlap with this * area @@ -601,7 +600,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the component connected to the area. - * + * * @return the Component. */ public Component getComponent() { @@ -610,7 +609,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the column of the top-left corner cell. - * + * * @return the column of the top-left corner cell. */ public int getColumn1() { @@ -619,7 +618,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the column of the bottom-right corner cell. - * + * * @return the column of the bottom-right corner cell. */ public int getColumn2() { @@ -628,7 +627,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the row of the top-left corner cell. - * + * * @return the row of the top-left corner cell. */ public int getRow1() { @@ -637,7 +636,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the row of the bottom-right corner cell. - * + * * @return the row of the bottom-right corner cell. */ public int getRow2() { @@ -656,7 +655,7 @@ public class GridLayout extends AbstractLayout implements * Gridlayout does not support laying components on top of each other. An * <code>OverlapsException</code> is thrown when a component already exists * (even partly) at the same space on a grid with the new component. - * + * * @author Vaadin Ltd. * @since 3.0 */ @@ -666,7 +665,7 @@ public class GridLayout extends AbstractLayout implements /** * Constructs an <code>OverlapsException</code>. - * + * * @param existingArea */ public OverlapsException(Area existingArea) { @@ -701,7 +700,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the area . - * + * * @return the existing area. */ public Area getArea() { @@ -712,7 +711,7 @@ public class GridLayout extends AbstractLayout implements /** * An <code>Exception</code> object which is thrown when an area exceeds the * bounds of the grid. - * + * * @author Vaadin Ltd. * @since 3.0 */ @@ -723,7 +722,7 @@ public class GridLayout extends AbstractLayout implements /** * Constructs an <code>OoutOfBoundsException</code> with the specified * detail message. - * + * * @param areaOutOfBounds */ public OutOfBoundsException(Area areaOutOfBounds) { @@ -732,7 +731,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the area that is out of bounds. - * + * * @return the area out of Bound. */ public Area getArea() { @@ -743,7 +742,7 @@ public class GridLayout extends AbstractLayout implements /** * Sets the number of columns in the grid. The column count can not be * reduced if there are any areas that would be outside of the shrunk grid. - * + * * @param columns * the new number of columns in the grid. */ @@ -777,7 +776,7 @@ public class GridLayout extends AbstractLayout implements /** * Get the number of columns in the grid. - * + * * @return the number of columns in the grid. */ public int getColumns() { @@ -787,7 +786,7 @@ public class GridLayout extends AbstractLayout implements /** * Sets the number of rows in the grid. The number of rows can not be * reduced if there are any areas that would be outside of the shrunk grid. - * + * * @param rows * the new number of rows in the grid. */ @@ -821,7 +820,7 @@ public class GridLayout extends AbstractLayout implements /** * Get the number of rows in the grid. - * + * * @return the number of rows in the grid. */ public int getRows() { @@ -830,14 +829,14 @@ public class GridLayout extends AbstractLayout implements /** * Gets the current x-position (column) of the cursor. - * + * * <p> * The cursor position points the position for the next component that is * added without specifying its coordinates (grid cell). When the cursor * position is occupied, the next component will be added to first free * position after the cursor. * </p> - * + * * @return the grid column the cursor is on, starting from 0. */ public int getCursorX() { @@ -847,7 +846,7 @@ public class GridLayout extends AbstractLayout implements /** * Sets the current cursor x-position. This is usually handled automatically * by GridLayout. - * + * * @param cursorX */ public void setCursorX(int cursorX) { @@ -856,14 +855,14 @@ public class GridLayout extends AbstractLayout implements /** * Gets the current y-position (row) of the cursor. - * + * * <p> * The cursor position points the position for the next component that is * added without specifying its coordinates (grid cell). When the cursor * position is occupied, the next component will be added to the first free * position after the cursor. * </p> - * + * * @return the grid row the Cursor is on. */ public int getCursorY() { @@ -873,7 +872,7 @@ public class GridLayout extends AbstractLayout implements /** * Sets the current y-coordinate (row) of the cursor. This is usually * handled automatically by GridLayout. - * + * * @param cursorY * the row number, starting from 0 for the topmost row. */ @@ -957,7 +956,7 @@ public class GridLayout extends AbstractLayout implements /** * Inserts an empty row at the specified position in the grid. - * + * * @param row * Index of the row before which the new row will be inserted. * The leftmost row has index 0. @@ -991,18 +990,18 @@ public class GridLayout extends AbstractLayout implements /** * Removes a row and all the components in the row. - * + * * <p> * Components which span over several rows are removed if the selected row * is on the first row of such a component. * </p> - * + * * <p> * If the last row is removed then all remaining components will be removed * and the grid will be reduced to one row. The cursor will be moved to the * upper left cell of the grid. * </p> - * + * * @param row * Index of the row to remove. The leftmost row has index 0. */ @@ -1049,33 +1048,34 @@ public class GridLayout extends AbstractLayout implements /** * Sets the expand ratio of given column. - * + * * <p> * The expand ratio defines how excess space is distributed among columns. * Excess space means space that is left over from components that are not * sized relatively. By default, the excess space is distributed evenly. * </p> - * + * * <p> * Note that the component width of the GridLayout must be defined (fixed or * relative, as opposed to undefined) for this method to have any effect. * </p> - * + * * @see #setWidth(float, int) - * + * * @param columnIndex * @param ratio */ public void setColumnExpandRatio(int columnIndex, float ratio) { columnExpandRatio.put(columnIndex, ratio); + getState().explicitColRatios.add(columnIndex); markAsDirty(); } /** * Returns the expand ratio of given column - * + * * @see #setColumnExpandRatio(int, float) - * + * * @param columnIndex * @return the expand ratio, 0.0f by default */ @@ -1086,34 +1086,35 @@ public class GridLayout extends AbstractLayout implements /** * Sets the expand ratio of given row. - * + * * <p> * Expand ratio defines how excess space is distributed among rows. Excess * space means the space left over from components that are not sized * relatively. By default, the excess space is distributed evenly. * </p> - * + * * <p> * Note, that height needs to be defined (fixed or relative, as opposed to * undefined height) for this method to have any effect. * </p> - * + * * @see #setHeight(float, int) - * + * * @param rowIndex * The row index, starting from 0 for the topmost row. * @param ratio */ public void setRowExpandRatio(int rowIndex, float ratio) { rowExpandRatio.put(rowIndex, ratio); + getState().explicitRowRatios.add(rowIndex); markAsDirty(); } /** * Returns the expand ratio of given row. - * + * * @see #setRowExpandRatio(int, float) - * + * * @param rowIndex * The row index, starting from 0 for the topmost row. * @return the expand ratio, 0.0f by default @@ -1125,7 +1126,7 @@ public class GridLayout extends AbstractLayout implements /** * Gets the Component at given index. - * + * * @param x * The column index, starting from 0 for the leftmost column. * @param y @@ -1147,7 +1148,7 @@ public class GridLayout extends AbstractLayout implements /** * Returns information about the area where given component is laid in the * GridLayout. - * + * * @param component * the component whose area information is requested. * @return an Area object that contains information how component is laid in |