@Test public void testNoise() { Noise noise1 = (Noise) NameParser.createFunction("noise(0.0, 10.0, 1.0)"); Noise noise2 = (Noise) NameParser.createFunction("noise"); assertThat(noise1.nextValue().getAlarmName(), notNullValue()); assertThat(noise2.nextValue().getAlarmName(), notNullValue()); }
static Simulation<?> createFunction(String string) { List<Object> parameters = parseFunction(string); StringBuilder className = new StringBuilder("org.diirt.datasource.sim."); int firstCharPosition = className.length(); className.append((String) parameters.get(0)); className.setCharAt(firstCharPosition, Character.toUpperCase(className.charAt(firstCharPosition))); try { @SuppressWarnings("unchecked") Class<SimFunction<?>> clazz = (Class<SimFunction<?>>) Class.forName(className.toString()); Object[] constructorParams = parameters.subList(1, parameters.size()).toArray(); Class[] types = new Class[constructorParams.length]; for (int i = 0; i < types.length; i++) { types[i] = constructorParams[i].getClass(); } return clazz.getConstructor(types).newInstance(constructorParams); } catch (ClassNotFoundException ex) { throw new RuntimeException("Simulation channel " + parameters.get(0) + " is not defined"); } catch (NoClassDefFoundError ex) { if (ex.getMessage().contains("wrong name") && ex.getMessage().lastIndexOf("/") != -1) { String suggestedName = ex.getMessage().substring(ex.getMessage().lastIndexOf("/") + 1, ex.getMessage().length() - 1); throw new RuntimeException("Function " + parameters.get(0) + " is not defined (Looking for " + suggestedName + "?)"); } throw new RuntimeException("Function " + parameters.get(0) + " is not defined"); } catch (NoSuchMethodException ex) { throw new RuntimeException("Wrong parameter number for function " + parameters.get(0)); } catch (SecurityException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (InstantiationException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " failed", ex); } catch (IllegalAccessException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (IllegalArgumentException ex) { throw new RuntimeException("Wrong parameter type for function " + parameters.get(0)); } catch (InvocationTargetException ex) { throw new RuntimeException(ex.getCause().getMessage(), ex); } }
NameParser { static Simulation<?> createFunction(String string) { List<Object> parameters = parseFunction(string); StringBuilder className = new StringBuilder("org.diirt.datasource.sim."); int firstCharPosition = className.length(); className.append((String) parameters.get(0)); className.setCharAt(firstCharPosition, Character.toUpperCase(className.charAt(firstCharPosition))); try { @SuppressWarnings("unchecked") Class<SimFunction<?>> clazz = (Class<SimFunction<?>>) Class.forName(className.toString()); Object[] constructorParams = parameters.subList(1, parameters.size()).toArray(); Class[] types = new Class[constructorParams.length]; for (int i = 0; i < types.length; i++) { types[i] = constructorParams[i].getClass(); } return clazz.getConstructor(types).newInstance(constructorParams); } catch (ClassNotFoundException ex) { throw new RuntimeException("Simulation channel " + parameters.get(0) + " is not defined"); } catch (NoClassDefFoundError ex) { if (ex.getMessage().contains("wrong name") && ex.getMessage().lastIndexOf("/") != -1) { String suggestedName = ex.getMessage().substring(ex.getMessage().lastIndexOf("/") + 1, ex.getMessage().length() - 1); throw new RuntimeException("Function " + parameters.get(0) + " is not defined (Looking for " + suggestedName + "?)"); } throw new RuntimeException("Function " + parameters.get(0) + " is not defined"); } catch (NoSuchMethodException ex) { throw new RuntimeException("Wrong parameter number for function " + parameters.get(0)); } catch (SecurityException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (InstantiationException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " failed", ex); } catch (IllegalAccessException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (IllegalArgumentException ex) { throw new RuntimeException("Wrong parameter type for function " + parameters.get(0)); } catch (InvocationTargetException ex) { throw new RuntimeException(ex.getCause().getMessage(), ex); } } }
NameParser { static Simulation<?> createFunction(String string) { List<Object> parameters = parseFunction(string); StringBuilder className = new StringBuilder("org.diirt.datasource.sim."); int firstCharPosition = className.length(); className.append((String) parameters.get(0)); className.setCharAt(firstCharPosition, Character.toUpperCase(className.charAt(firstCharPosition))); try { @SuppressWarnings("unchecked") Class<SimFunction<?>> clazz = (Class<SimFunction<?>>) Class.forName(className.toString()); Object[] constructorParams = parameters.subList(1, parameters.size()).toArray(); Class[] types = new Class[constructorParams.length]; for (int i = 0; i < types.length; i++) { types[i] = constructorParams[i].getClass(); } return clazz.getConstructor(types).newInstance(constructorParams); } catch (ClassNotFoundException ex) { throw new RuntimeException("Simulation channel " + parameters.get(0) + " is not defined"); } catch (NoClassDefFoundError ex) { if (ex.getMessage().contains("wrong name") && ex.getMessage().lastIndexOf("/") != -1) { String suggestedName = ex.getMessage().substring(ex.getMessage().lastIndexOf("/") + 1, ex.getMessage().length() - 1); throw new RuntimeException("Function " + parameters.get(0) + " is not defined (Looking for " + suggestedName + "?)"); } throw new RuntimeException("Function " + parameters.get(0) + " is not defined"); } catch (NoSuchMethodException ex) { throw new RuntimeException("Wrong parameter number for function " + parameters.get(0)); } catch (SecurityException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (InstantiationException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " failed", ex); } catch (IllegalAccessException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (IllegalArgumentException ex) { throw new RuntimeException("Wrong parameter type for function " + parameters.get(0)); } catch (InvocationTargetException ex) { throw new RuntimeException(ex.getCause().getMessage(), ex); } }  }
NameParser { static Simulation<?> createFunction(String string) { List<Object> parameters = parseFunction(string); StringBuilder className = new StringBuilder("org.diirt.datasource.sim."); int firstCharPosition = className.length(); className.append((String) parameters.get(0)); className.setCharAt(firstCharPosition, Character.toUpperCase(className.charAt(firstCharPosition))); try { @SuppressWarnings("unchecked") Class<SimFunction<?>> clazz = (Class<SimFunction<?>>) Class.forName(className.toString()); Object[] constructorParams = parameters.subList(1, parameters.size()).toArray(); Class[] types = new Class[constructorParams.length]; for (int i = 0; i < types.length; i++) { types[i] = constructorParams[i].getClass(); } return clazz.getConstructor(types).newInstance(constructorParams); } catch (ClassNotFoundException ex) { throw new RuntimeException("Simulation channel " + parameters.get(0) + " is not defined"); } catch (NoClassDefFoundError ex) { if (ex.getMessage().contains("wrong name") && ex.getMessage().lastIndexOf("/") != -1) { String suggestedName = ex.getMessage().substring(ex.getMessage().lastIndexOf("/") + 1, ex.getMessage().length() - 1); throw new RuntimeException("Function " + parameters.get(0) + " is not defined (Looking for " + suggestedName + "?)"); } throw new RuntimeException("Function " + parameters.get(0) + " is not defined"); } catch (NoSuchMethodException ex) { throw new RuntimeException("Wrong parameter number for function " + parameters.get(0)); } catch (SecurityException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (InstantiationException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " failed", ex); } catch (IllegalAccessException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (IllegalArgumentException ex) { throw new RuntimeException("Wrong parameter type for function " + parameters.get(0)); } catch (InvocationTargetException ex) { throw new RuntimeException(ex.getCause().getMessage(), ex); } }   }
NameParser { static Simulation<?> createFunction(String string) { List<Object> parameters = parseFunction(string); StringBuilder className = new StringBuilder("org.diirt.datasource.sim."); int firstCharPosition = className.length(); className.append((String) parameters.get(0)); className.setCharAt(firstCharPosition, Character.toUpperCase(className.charAt(firstCharPosition))); try { @SuppressWarnings("unchecked") Class<SimFunction<?>> clazz = (Class<SimFunction<?>>) Class.forName(className.toString()); Object[] constructorParams = parameters.subList(1, parameters.size()).toArray(); Class[] types = new Class[constructorParams.length]; for (int i = 0; i < types.length; i++) { types[i] = constructorParams[i].getClass(); } return clazz.getConstructor(types).newInstance(constructorParams); } catch (ClassNotFoundException ex) { throw new RuntimeException("Simulation channel " + parameters.get(0) + " is not defined"); } catch (NoClassDefFoundError ex) { if (ex.getMessage().contains("wrong name") && ex.getMessage().lastIndexOf("/") != -1) { String suggestedName = ex.getMessage().substring(ex.getMessage().lastIndexOf("/") + 1, ex.getMessage().length() - 1); throw new RuntimeException("Function " + parameters.get(0) + " is not defined (Looking for " + suggestedName + "?)"); } throw new RuntimeException("Function " + parameters.get(0) + " is not defined"); } catch (NoSuchMethodException ex) { throw new RuntimeException("Wrong parameter number for function " + parameters.get(0)); } catch (SecurityException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (InstantiationException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " failed", ex); } catch (IllegalAccessException ex) { throw new RuntimeException("Constructor for " + parameters.get(0) + " should be at least package private"); } catch (IllegalArgumentException ex) { throw new RuntimeException("Wrong parameter type for function " + parameters.get(0)); } catch (InvocationTargetException ex) { throw new RuntimeException(ex.getCause().getMessage(), ex); } }    }