From f5048f21bf12a291c6e6e537f82553049bfd67d5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Pekka=20Hyv=C3=B6nen?= Date: Thu, 3 Nov 2016 07:29:32 +0000 Subject: [PATCH] Revert "Convert to for-each loop, Stream and switch-over-strings" This reverts commit 73078c7560d40db80dae6a6fb8047c29a92dd8d0. Change-Id: I2757232d1312ae5162550d9fafb26c2d657c1b7f --- .../java/com/vaadin/event/EventRouter.java | 18 ++++--- .../java/com/vaadin/event/ListenerMethod.java | 38 +++++++------- .../vaadin/server/ComponentSizeValidator.java | 4 +- .../java/com/vaadin/server/JsonCodec.java | 48 ++++++++---------- .../widgetsetutils/ClassPathExplorer.java | 50 +++++++++++-------- .../java/com/vaadin/ui/AbsoluteLayout.java | 35 ++++++------- .../src/main/java/com/vaadin/ui/Upload.java | 4 +- .../declarative/DesignAttributeHandler.java | 4 +- .../data/BinderConverterValidatorTest.java | 17 +++---- .../AbstractComponentDeclarativeTest.java | 6 ++- 10 files changed, 115 insertions(+), 109 deletions(-) diff --git a/server/src/main/java/com/vaadin/event/EventRouter.java b/server/src/main/java/com/vaadin/event/EventRouter.java index 6b96e91f25..c5237d9aca 100644 --- a/server/src/main/java/com/vaadin/event/EventRouter.java +++ b/server/src/main/java/com/vaadin/event/EventRouter.java @@ -28,7 +28,6 @@ import java.util.logging.Logger; import com.vaadin.server.ErrorEvent; import com.vaadin.server.ErrorHandler; -import java.util.stream.Stream; /** * EventRouter class implementing the inheritable event listening @@ -125,10 +124,15 @@ public class EventRouter implements MethodEventSource { // Find the correct method final Method[] methods = target.getClass().getMethods(); - - final Method method = Stream.of(methods) - .filter(m -> methodName.equals(m.getName())) - .findAny().orElseThrow(IllegalArgumentException::new); + Method method = null; + for (int i = 0; i < methods.length; i++) { + if (methods[i].getName().equals(methodName)) { + method = methods[i]; + } + } + if (method == null) { + throw new IllegalArgumentException(); + } // Remove the listeners if (listenerList != null) { @@ -187,8 +191,8 @@ public class EventRouter implements MethodEventSource { // Send the event to all listeners. The listeners themselves // will filter out unwanted events. final Object[] listeners = listenerList.toArray(); - for (Object listener : listeners) { - ListenerMethod listenerMethod = (ListenerMethod) listener; + for (int i = 0; i < listeners.length; i++) { + ListenerMethod listenerMethod = (ListenerMethod) listeners[i]; if (null != errorHandler) { try { listenerMethod.receiveEvent(event); diff --git a/server/src/main/java/com/vaadin/event/ListenerMethod.java b/server/src/main/java/com/vaadin/event/ListenerMethod.java index bf80f1e74b..f20a14a034 100644 --- a/server/src/main/java/com/vaadin/event/ListenerMethod.java +++ b/server/src/main/java/com/vaadin/event/ListenerMethod.java @@ -25,7 +25,6 @@ import java.util.EventListener; import java.util.EventObject; import java.util.logging.Level; import java.util.logging.Logger; -import java.util.stream.Stream; /** *

@@ -119,8 +118,9 @@ public class ListenerMethod implements EventListener, Serializable { private static Method findHighestMethod(Class cls, String method, Class[] paramTypes) { Class[] ifaces = cls.getInterfaces(); - for (Class iface : ifaces) { - Method ifaceMethod = findHighestMethod(iface, method, paramTypes); + for (int i = 0; i < ifaces.length; i++) { + Method ifaceMethod = findHighestMethod(ifaces[i], method, + paramTypes); if (ifaceMethod != null) { return ifaceMethod; } @@ -132,12 +132,14 @@ public class ListenerMethod implements EventListener, Serializable { return parentMethod; } } - - // we ignore parameter types for now - you need to add this - return Stream.of(cls.getMethods()) - .filter(m -> method.equals(m.getName())) - .findAny().orElse(null); - + Method[] methods = cls.getMethods(); + for (int i = 0; i < methods.length; i++) { + // we ignore parameter types for now - you need to add this + if (methods[i].getName().equals(method)) { + return methods[i]; + } + } + return null; } /** @@ -245,9 +247,9 @@ public class ListenerMethod implements EventListener, Serializable { // Finds the correct method final Method[] methods = target.getClass().getMethods(); Method method = null; - for (Method method1 : methods) { - if (method1.getName().equals(methodName)) { - method = method1; + for (int i = 0; i < methods.length; i++) { + if (methods[i].getName().equals(methodName)) { + method = methods[i]; } } if (method == null) { @@ -357,9 +359,9 @@ public class ListenerMethod implements EventListener, Serializable { // Find the correct method final Method[] methods = target.getClass().getMethods(); Method method = null; - for (Method method1 : methods) { - if (method1.getName().equals(methodName)) { - method = method1; + for (int i = 0; i < methods.length; i++) { + if (methods[i].getName().equals(methodName)) { + method = methods[i]; } } if (method == null) { @@ -459,9 +461,9 @@ public class ListenerMethod implements EventListener, Serializable { // Finds the correct method final Method[] methods = target.getClass().getMethods(); Method method = null; - for (Method method1 : methods) { - if (method1.getName().equals(methodName)) { - method = method1; + for (int i = 0; i < methods.length; i++) { + if (methods[i].getName().equals(methodName)) { + method = methods[i]; } } if (method == null) { diff --git a/server/src/main/java/com/vaadin/server/ComponentSizeValidator.java b/server/src/main/java/com/vaadin/server/ComponentSizeValidator.java index c7c6541c9e..e68fbb5903 100644 --- a/server/src/main/java/com/vaadin/server/ComponentSizeValidator.java +++ b/server/src/main/java/com/vaadin/server/ComponentSizeValidator.java @@ -92,7 +92,9 @@ public class ComponentSizeValidator implements Serializable { } } else if (isForm(component)) { HasComponents form = (HasComponents) component; - for (Component child : form) { + for (Iterator iterator = form.iterator(); iterator + .hasNext();) { + Component child = iterator.next(); errors = validateComponentRelativeSizes(child, errors, parent); } } diff --git a/server/src/main/java/com/vaadin/server/JsonCodec.java b/server/src/main/java/com/vaadin/server/JsonCodec.java index a85dd5c0e9..588443d55b 100644 --- a/server/src/main/java/com/vaadin/server/JsonCodec.java +++ b/server/src/main/java/com/vaadin/server/JsonCodec.java @@ -398,19 +398,16 @@ public class JsonCodec implements Serializable { connectorTracker); } - if (null != transportType) // Collections - switch (transportType) { - case JsonConstants.VTYPE_LIST: - return decodeList(targetType, restrictToInternalTypes, + // Collections + if (JsonConstants.VTYPE_LIST.equals(transportType)) { + return decodeList(targetType, restrictToInternalTypes, (JsonArray) encodedJsonValue, connectorTracker); - case JsonConstants.VTYPE_SET: - return decodeSet(targetType, restrictToInternalTypes, + } else if (JsonConstants.VTYPE_SET.equals(transportType)) { + return decodeSet(targetType, restrictToInternalTypes, (JsonArray) encodedJsonValue, connectorTracker); - case JsonConstants.VTYPE_MAP: - return decodeMap(targetType, restrictToInternalTypes, + } else if (JsonConstants.VTYPE_MAP.equals(transportType)) { + return decodeMap(targetType, restrictToInternalTypes, encodedJsonValue, connectorTracker); - default: - break; } // Arrays @@ -430,23 +427,20 @@ public class JsonCodec implements Serializable { return connectorTracker.getConnector(encodedJsonValue.asString()); } - - if (null != transportType) // Legacy types - switch (transportType) { - case JsonConstants.VTYPE_STRING: - return encodedJsonValue.asString(); - case JsonConstants.VTYPE_INTEGER: - return (int) encodedJsonValue.asNumber(); - case JsonConstants.VTYPE_LONG: - return (long) encodedJsonValue.asNumber(); - case JsonConstants.VTYPE_FLOAT: - return (float) encodedJsonValue.asNumber(); - case JsonConstants.VTYPE_DOUBLE: - return encodedJsonValue.asNumber(); - case JsonConstants.VTYPE_BOOLEAN: - return encodedJsonValue.asBoolean(); - default: - break; + // Legacy types + + if (JsonConstants.VTYPE_STRING.equals(transportType)) { + return encodedJsonValue.asString(); + } else if (JsonConstants.VTYPE_INTEGER.equals(transportType)) { + return (int) encodedJsonValue.asNumber(); + } else if (JsonConstants.VTYPE_LONG.equals(transportType)) { + return (long) encodedJsonValue.asNumber(); + } else if (JsonConstants.VTYPE_FLOAT.equals(transportType)) { + return (float) encodedJsonValue.asNumber(); + } else if (JsonConstants.VTYPE_DOUBLE.equals(transportType)) { + return encodedJsonValue.asNumber(); + } else if (JsonConstants.VTYPE_BOOLEAN.equals(transportType)) { + return encodedJsonValue.asBoolean(); } throw new JsonException("Unknown type " + transportType); diff --git a/server/src/main/java/com/vaadin/server/widgetsetutils/ClassPathExplorer.java b/server/src/main/java/com/vaadin/server/widgetsetutils/ClassPathExplorer.java index 9bc01e7ce2..6903a17ddd 100644 --- a/server/src/main/java/com/vaadin/server/widgetsetutils/ClassPathExplorer.java +++ b/server/src/main/java/com/vaadin/server/widgetsetutils/ClassPathExplorer.java @@ -203,42 +203,45 @@ public class ClassPathExplorer { if (directory.exists() && !directory.isHidden()) { // Get the list of the files contained in the directory String[] files = directory.list(); - for (String file : files) { + for (int i = 0; i < files.length; i++) { // we are only interested in .gwt.xml files - if (!file.endsWith(".gwt.xml")) { + if (!files[i].endsWith(".gwt.xml")) { continue; } + // remove the .gwt.xml extension - String classname = file.substring(0, file.length() - 8); + String classname = files[i].substring(0, files[i].length() - 8); String packageName = locationString .substring(locationString.lastIndexOf('/') + 1); classname = packageName + "." + classname; + if (!WidgetSetBuilder.isWidgetset(classname)) { // Only return widgetsets and not GWT modules to avoid // comparing modules and widgetsets continue; } + if (!widgetsets.containsKey(classname)) { String packagePath = packageName.replaceAll("\\.", "/"); String basePath = location.getFile(); if (basePath.endsWith("/" + packagePath)) { basePath = basePath.replaceAll("/" + packagePath + "$", - ""); + ""); } else if (basePath.endsWith("/" + packagePath + "/")) { basePath = basePath.replaceAll("/" + packagePath + "/$", - ""); + ""); } else { throw new IllegalStateException( - "Error trying to find base path, location (" - + location.getFile() - + ") does not end in expected '/" - + packagePath + "'"); + "Error trying to find base path, location (" + + location.getFile() + + ") does not end in expected '/" + + packagePath + "'"); } try { URL url = new URL(location.getProtocol(), - location.getHost(), location.getPort(), - basePath); + location.getHost(), location.getPort(), + basePath); widgetsets.put(classname, url); } catch (MalformedURLException e) { // should never happen as based on an existing URL, @@ -270,8 +273,8 @@ public class ClassPathExplorer { .getValue("Vaadin-Widgetsets"); if (value != null) { String[] widgetsetNames = value.split(","); - for (String widgetsetName : widgetsetNames) { - String widgetsetname = widgetsetName.trim(); + for (int i = 0; i < widgetsetNames.length; i++) { + String widgetsetname = widgetsetNames[i].trim(); if (!widgetsetname.equals("")) { widgetsets.put(widgetsetname, location); } @@ -283,8 +286,8 @@ public class ClassPathExplorer { .getValue("Vaadin-Stylesheets"); if (value != null) { String[] stylesheets = value.split(","); - for (String untrimmedStylesheet : stylesheets) { - String stylesheet = untrimmedStylesheet.trim(); + for (int i = 0; i < stylesheets.length; i++) { + String stylesheet = stylesheets[i].trim(); if (!stylesheet.equals("")) { addonStyles.put(stylesheet, location); } @@ -323,7 +326,8 @@ public class ClassPathExplorer { debug("Classpath: " + classpath); String[] split = classpath.split(pathSep); - for (String classpathEntry : split) { + for (int i = 0; i < split.length; i++) { + String classpathEntry = split[i]; if (acceptClassPathEntry(classpathEntry)) { locations.add(classpathEntry); } @@ -455,18 +459,20 @@ public class ClassPathExplorer { // add all directories recursively File[] dirs = file.listFiles(DIRECTORIES_ONLY); - for (File dir : dirs) { + for (int i = 0; i < dirs.length; i++) { try { // add the present directory - if (!dir.isHidden() && !dir.getPath().contains(File.separator + ".")) { - String key = dir.getCanonicalPath() + "/" + name + dir.getName(); - URL url = dir.getCanonicalFile().toURI().toURL(); + if (!dirs[i].isHidden() + && !dirs[i].getPath().contains(File.separator + ".")) { + String key = dirs[i].getCanonicalPath() + "/" + name + + dirs[i].getName(); + URL url = dirs[i].getCanonicalFile().toURI().toURL(); locations.put(key, url); } - }catch (Exception ioe) { + } catch (Exception ioe) { return; } - include(name + dir.getName(), dir, locations); + include(name + dirs[i].getName(), dirs[i], locations); } } diff --git a/server/src/main/java/com/vaadin/ui/AbsoluteLayout.java b/server/src/main/java/com/vaadin/ui/AbsoluteLayout.java index cb0e1445ab..5aa008e829 100644 --- a/server/src/main/java/com/vaadin/ui/AbsoluteLayout.java +++ b/server/src/main/java/com/vaadin/ui/AbsoluteLayout.java @@ -301,8 +301,8 @@ public class AbsoluteLayout extends AbstractLayout } String[] cssProperties = css.split(";"); - for (String cssProperty : cssProperties) { - String[] keyValuePair = cssProperty.split(":"); + for (int i = 0; i < cssProperties.length; i++) { + String[] keyValuePair = cssProperties[i].split(":"); String key = keyValuePair[0].trim(); if (key.equals("")) { continue; @@ -319,27 +319,22 @@ public class AbsoluteLayout extends AbstractLayout String symbol = value.replaceAll("[0-9\\.\\-]+", ""); if (!symbol.equals("")) { value = value.substring(0, value.indexOf(symbol)) - .trim(); + .trim(); } float v = Float.parseFloat(value); Unit unit = Unit.getUnitFromSymbol(symbol); - switch (key) { - case "top": - topValue = v; - topUnits = unit; - break; - case "right": - rightValue = v; - rightUnits = unit; - break; - case "bottom": - bottomValue = v; - bottomUnits = unit; - break; - case "left": - leftValue = v; - leftUnits = unit; - break; + if (key.equals("top")) { + topValue = v; + topUnits = unit; + } else if (key.equals("right")) { + rightValue = v; + rightUnits = unit; + } else if (key.equals("bottom")) { + bottomValue = v; + bottomUnits = unit; + } else if (key.equals("left")) { + leftValue = v; + leftUnits = unit; } } } diff --git a/server/src/main/java/com/vaadin/ui/Upload.java b/server/src/main/java/com/vaadin/ui/Upload.java index 155bdfb811..a08fce099e 100644 --- a/server/src/main/java/com/vaadin/ui/Upload.java +++ b/server/src/main/java/com/vaadin/ui/Upload.java @@ -840,7 +840,9 @@ public class Upload extends AbstractComponent // this is implemented differently than other listeners to maintain // backwards compatibility if (progressListeners != null) { - for (ProgressListener l : progressListeners) { + for (Iterator it = progressListeners + .iterator(); it.hasNext();) { + ProgressListener l = it.next(); l.updateProgress(totalBytes, contentLength); } } diff --git a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java index 2fad5fb14f..44248d095b 100644 --- a/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java +++ b/server/src/main/java/com/vaadin/ui/declarative/DesignAttributeHandler.java @@ -318,11 +318,11 @@ public class DesignAttributeHandler implements Serializable { propertyName = removeSubsequentUppercase(propertyName); String[] words = propertyName.split("(? 0) { builder.append("-"); } - builder.append(word.toLowerCase()); + builder.append(words[i].toLowerCase()); } return builder.toString(); } diff --git a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java index ac530c266f..0711ae24bf 100644 --- a/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java +++ b/server/src/test/java/com/vaadin/data/BinderConverterValidatorTest.java @@ -235,15 +235,14 @@ public class BinderConverterValidatorTest throw new IllegalArgumentException( "Value must be OK or NOTOK"); }, model -> { - switch (model) { - case "1": - return "OK"; - case "2": - return "NOTOK"; - default: - throw new IllegalArgumentException( - "Value in model must be 1 or 2"); - } + if (model.equals("1")) { + return "OK"; + } else if (model.equals("2")) { + return "NOTOK"; + } else { + throw new IllegalArgumentException( + "Value in model must be 1 or 2"); + } }); binding.bind(StatusBean::getStatus, StatusBean::setStatus); binder.setBean(bean); diff --git a/server/src/test/java/com/vaadin/tests/server/component/abstractcomponent/AbstractComponentDeclarativeTest.java b/server/src/test/java/com/vaadin/tests/server/component/abstractcomponent/AbstractComponentDeclarativeTest.java index b8875c34fd..b13499983e 100644 --- a/server/src/test/java/com/vaadin/tests/server/component/abstractcomponent/AbstractComponentDeclarativeTest.java +++ b/server/src/test/java/com/vaadin/tests/server/component/abstractcomponent/AbstractComponentDeclarativeTest.java @@ -97,8 +97,10 @@ public class AbstractComponentDeclarativeTest Boolean[] explicitImmediate = { null, Boolean.FALSE, Boolean.TRUE, Boolean.TRUE }; boolean[] immediate = { true, false, true, true }; - for (String designElements : design) { - component = (AbstractComponent) Design.read(new ByteArrayInputStream(designElements.getBytes(Charset.forName("UTF-8")))); + for (int i = 0; i < design.length; i++) { + component = (AbstractComponent) Design + .read(new ByteArrayInputStream( + design[i].getBytes(Charset.forName("UTF-8")))); } } -- 2.39.5