List of usage examples for java.lang Byte parseByte
public static byte parseByte(String s) throws NumberFormatException
From source file:springapp.web.controller.theme.ProcessThemeController.java
public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { HttpSession session = httpServletRequest.getSession(); String templateIdString = httpServletRequest.getParameter(ApplicationConstants.TEMPLATE_ID); byte templateId = 0; int oldColor = 0; int newColor = 0; int oldHeaderColor = 0; int newHeaderColor = 0; int oldBgColor = 0; int newBgColor = 0; int oldFontColor = 0; int newFontColor = 0; int oldHeaderFontColor = 0; int newHeaderFontColor = 0; int oldBorderColor = 0; int newBorderColor = 0; int oldTransp = 0; int newTransp = 0; byte sizeHeaderFontDiff = 11; byte sizeFontDiff = 11; ThemeParametersHolder themeParametersHolder = null; String toolsetName = httpServletRequest.getParameter(ApplicationConstants.TOOLSET_NAME); String versionString = httpServletRequest.getParameter(ApplicationConstants.VERSION); String version = (null == versionString) ? ApplicationConstants.DEFAULT_EXTJS_VERSION : versionString; String familyHeaderFont = httpServletRequest.getParameter(ApplicationConstants.FAMILY_HEADER_FONT); String familyFont = httpServletRequest.getParameter(ApplicationConstants.FAMILY_FONT); String weightHeaderFont = httpServletRequest.getParameter(ApplicationConstants.WEIGHT_HEADER_FONT); String weightFont = httpServletRequest.getParameter(ApplicationConstants.WEIGHT_FONT); try {// www. j av a 2s. c o m templateId = null != templateIdString ? Byte.parseByte(templateIdString) : 0; String oldColorString = (0 == templateId) ? "#DFE8F6" : "#F1F1F1"; String newColorString = httpServletRequest.getParameter(ApplicationConstants.NEW_COLOR); String oldHeaderColorString = (0 == templateId) ? "#CDDEF3" : "#D7D7D7"; String newHeaderColorString = httpServletRequest.getParameter(ApplicationConstants.NEW_HEADER_COLOR); String oldBgColorString = "#FFFFFF"; String newBgColorString = httpServletRequest.getParameter(ApplicationConstants.NEW_BG_COLOR); String oldFontColorString = "#000000"; String newFontColorString = httpServletRequest.getParameter(ApplicationConstants.NEW_FONT_COLOR); String oldHeaderFontColorString = (0 == templateId) ? "#15428B" : "#222222"; String newHeaderFontColorString = httpServletRequest .getParameter(ApplicationConstants.NEW_HEADER_FONT_COLOR); String sizeHeaderFontString = httpServletRequest.getParameter(ApplicationConstants.SIZE_HEADER_FONT); String sizeFontString = httpServletRequest.getParameter(ApplicationConstants.SIZE_FONT); String oldBorderColorString = (0 == templateId) ? "#99BBE8" : "#D0D0D0"; String newBorderColorString = httpServletRequest.getParameter(ApplicationConstants.NEW_BORDER_COLOR); String oldTranspString = "255"; String newTranspString = httpServletRequest.getParameter(ApplicationConstants.NEW_TRANSP); themeParametersHolder = new ThemeParametersHolder(templateIdString, newColorString, newHeaderColorString, newBgColorString, newFontColorString, newHeaderFontColorString, familyHeaderFont, weightHeaderFont, sizeHeaderFontString, familyFont, weightFont, sizeFontString, newBorderColorString, newTranspString, toolsetName, version); oldColor = null != oldColorString ? Integer.parseInt(oldColorString.replaceFirst("#", ""), 16) : 0; newColor = null != newColorString ? Integer.parseInt(newColorString.replaceFirst("#", ""), 16) : 0; oldHeaderColor = null != oldHeaderColorString ? Integer.parseInt(oldHeaderColorString.replaceFirst("#", ""), 16) : 0; newHeaderColor = null != newHeaderColorString ? Integer.parseInt(newHeaderColorString.replaceFirst("#", ""), 16) : 0; oldBgColor = null != oldBgColorString ? Integer.parseInt(oldBgColorString.replaceFirst("#", ""), 16) : 0; newBgColor = null != newBgColorString ? Integer.parseInt(newBgColorString.replaceFirst("#", ""), 16) : 0; oldFontColor = null != oldFontColorString ? Integer.parseInt(oldFontColorString.replaceFirst("#", ""), 16) : 0; newFontColor = null != newFontColorString ? Integer.parseInt(newFontColorString.replaceFirst("#", ""), 16) : 0; oldHeaderFontColor = null != oldHeaderFontColorString ? Integer.parseInt(oldHeaderFontColorString.replaceFirst("#", ""), 16) : 0; newHeaderFontColor = null != newHeaderFontColorString ? Integer.parseInt(newHeaderFontColorString.replaceFirst("#", ""), 16) : 0; oldBorderColor = null != oldBorderColorString ? Integer.parseInt(oldBorderColorString.replaceFirst("#", ""), 16) : 0; newBorderColor = null != newBorderColorString ? Integer.parseInt(newBorderColorString.replaceFirst("#", ""), 16) : 0; oldTransp = null != oldTranspString ? Integer.parseInt(oldTranspString) : 0; newTransp = null != newTranspString ? Integer.parseInt(newTranspString) : 0; sizeHeaderFontDiff = (byte) ((null != sizeHeaderFontString ? Byte.parseByte(sizeHeaderFontString) : 11) - 11); sizeFontDiff = (byte) ((null != sizeFontString ? Byte.parseByte(sizeFontString) : 11) - 11); } catch (Exception e) { logger.error(e); } int oldR = (oldColor >> 16) & 0xff; int oldG = (oldColor >> 8) & 0xff; int oldB = oldColor & 0xff; int newR = (newColor >> 16) & 0xff; int newG = (newColor >> 8) & 0xff; int newB = newColor & 0xff; int oldHeaderR = (oldHeaderColor >> 16) & 0xff; int oldHeaderG = (oldHeaderColor >> 8) & 0xff; int oldHeaderB = oldHeaderColor & 0xff; int newHeaderR = (newHeaderColor >> 16) & 0xff; int newHeaderG = (newHeaderColor >> 8) & 0xff; int newHeaderB = newHeaderColor & 0xff; int oldBgR = (oldBgColor >> 16) & 0xff; int oldBgG = (oldBgColor >> 8) & 0xff; int oldBgB = oldBgColor & 0xff; int newBgR = (newBgColor >> 16) & 0xff; int newBgG = (newBgColor >> 8) & 0xff; int newBgB = newBgColor & 0xff; int oldFontR = (oldFontColor >> 16) & 0xff; int oldFontG = (oldFontColor >> 8) & 0xff; int oldFontB = oldFontColor & 0xff; int newFontR = (newFontColor >> 16) & 0xff; int newFontG = (newFontColor >> 8) & 0xff; int newFontB = newFontColor & 0xff; int oldHeaderFontR = (oldHeaderFontColor >> 16) & 0xff; int oldHeaderFontG = (oldHeaderFontColor >> 8) & 0xff; int oldHeaderFontB = oldHeaderFontColor & 0xff; int newHeaderFontR = (newHeaderFontColor >> 16) & 0xff; int newHeaderFontG = (newHeaderFontColor >> 8) & 0xff; int newHeaderFontB = newHeaderFontColor & 0xff; int oldBorderR = (oldBorderColor >> 16) & 0xff; int oldBorderG = (oldBorderColor >> 8) & 0xff; int oldBorderB = oldBorderColor & 0xff; int newBorderR = (newBorderColor >> 16) & 0xff; int newBorderG = (newBorderColor >> 8) & 0xff; int newBorderB = newBorderColor & 0xff; ServletContext servletContext = session.getServletContext(); WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); ResourcesProcessorFactory resourcesProcessorFactory = (ResourcesProcessorFactory) context .getBean("processorFactory"); ResourcesHolder schemaHolder; ResourcesProcessor processor; /* schemaHolder = getResourcesHolderForProcessing(templateId, ""); processor = resourcesProcessorFactory.getResourcesProcessor(schemaHolder, context);*/ //operation definition //hints HashMap hints = new HashMap(); hints.put(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY); hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); hints.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); hints.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); hints.put(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY); RenderingHints renderingHints = new RenderingHints(hints); int rDiff = newR - oldR; int gDiff = newG - oldG; int bDiff = newB - oldB; int rHeaderDiff = newHeaderR - oldHeaderR; int gHeaderDiff = newHeaderG - oldHeaderG; int bHeaderDiff = newHeaderB - oldHeaderB; int rBgDiff = newBgR - oldBgR; int gBgDiff = newBgG - oldBgG; int bBgDiff = newBgB - oldBgB; int rFontDiff = newFontR - oldFontR; int gFontDiff = newFontG - oldFontG; int bFontDiff = newFontB - oldFontB; int rHeaderFontDiff = newHeaderFontR - oldHeaderFontR; int gHeaderFontDiff = newHeaderFontG - oldHeaderFontG; int bHeaderFontDiff = newHeaderFontB - oldHeaderFontB; int rBorderDiff = newBorderR - oldBorderR; int gBorderDiff = newBorderG - oldBorderG; int bBorderDiff = newBorderB - oldBorderB; float transparencyDiff = newTransp - oldTransp; float[] offsets = { rDiff, gDiff, bDiff, 0f }; float[] offsetsHeader = { rHeaderDiff, gHeaderDiff, bHeaderDiff, 0f }; float[] offsetsBg = { rBgDiff, gBgDiff, bBgDiff, 0f }; float[] offsetsFont = { rFontDiff, gFontDiff, bFontDiff, 0f }; float[] offsetsHeaderFont = { rHeaderFontDiff, gHeaderFontDiff, bHeaderFontDiff, 0f }; float[] offsetsBorder = { rBorderDiff, gBorderDiff, bBorderDiff, 0f }; float[] offsetsTranceparency = (0 == transparencyDiff) ? null : new float[] { 0, 0, 0, transparencyDiff }; float[] offsetsShadowTransparency = { 0, 0, 0, transparencyDiff / 5 }; float liteDivider = 2.5f; float[] liteoffsets = { rDiff / liteDivider, gDiff / liteDivider, bDiff / liteDivider, 0f }; float[] scaleFactors = { 1.0f, 1.0f, 1.0f, 1.0f }; ExtJSRescaleOp brightenOp = new ExtJSRescaleOp(scaleFactors, // 1 ,1 ,1 offsets, renderingHints); ExtJSRescaleOp headerOp = new ExtJSRescaleOp(scaleFactors, // 1 ,1 ,1 offsetsHeader, renderingHints); ExtJSRescaleOp liteOp = new ExtJSRescaleOp(scaleFactors, // 1 ,1 ,1 liteoffsets, renderingHints); ExtJSRescaleOp bgOp = new ExtJSRescaleOp(scaleFactors, // 1 ,1 ,1 offsetsBg, renderingHints); ExtJSRescaleOp fontOp = new ExtJSRescaleOp(scaleFactors, // 1 ,1 ,1 offsetsFont, renderingHints); ExtJSRescaleOp headerFontOp = new ExtJSRescaleOp(scaleFactors, // 1 ,1 ,1 offsetsHeaderFont, renderingHints); ExtJSRescaleOp borderOp = new ExtJSRescaleOp(scaleFactors, // 1 ,1 ,1 offsetsBorder, renderingHints); ExtJSRescaleOp transparencyOp = (0 == transparencyDiff) ? null : new ExtJSRescaleOp(scaleFactors, // 1 ,1 ,1 offsetsTranceparency, renderingHints); ExtJSRescaleOp shadowTransparencyOp = null/*new ExtJSRescaleOp( scaleFactors,// 1 ,1 ,1 offsetsShadowTransparency, renderingHints)*/; /* ShiftOp shiftOp = new ShiftOp(new float[]{1, 1, 1, 1} , offsets, renderingHints, true); int csRGB = ColorSpace.CS_sRGB; int csGRAY = ColorSpace.CS_GRAY; ColorSpace srcCS = ColorSpace.getInstance(csRGB); ColorSpace destCS = ColorSpace.getInstance(csGRAY); */ //operation with inversion of color #for heading font color ForegroundShiftOp foregroundOp = new ForegroundShiftOp(newR, newG, newB); AffineTransformOp affineTransformOp = null/*new AffineTransformOp( AffineTransform.getScaleInstance(2,2), AffineTransformOp.TYPE_BICUBIC)*/; //end operation definition //get toolset holder ResourceHolderPool holderToolsetPool = this.holderToolsetPool; ResourcesHolder toolsetSchemaHolder = holderToolsetPool.checkOut(); // //get drawable holder ResourceHolderPool holderDrawablePool = this.holderDrawablePool; ResourcesHolder drawableSchemaHolder = holderDrawablePool.checkOut(); // /* process(processor, templateId, schemaHolder, brightenOp, foregroundOp, liteOp, bgOp, fontOp, transparencyOp, session, borderOp, affineTransformOp, headerFontOp, shadowTransparencyOp, headerOp, toolsetSchemaHolder, toolsetName, familyHeaderFont, weightHeaderFont, sizeHeaderFontDiff, familyFont, weightFont, sizeFontDiff, drawableSchemaHolder, "3.2"*//*version*//*); if (!"3.2".equals(version)){*/ schemaHolder = getResourcesHolderForProcessing(templateId, version); processor = resourcesProcessorFactory.getResourcesProcessor(schemaHolder, context); process(processor, themeParametersHolder, templateId, schemaHolder, brightenOp, foregroundOp, liteOp, bgOp, fontOp, transparencyOp, session, borderOp, affineTransformOp, headerFontOp, shadowTransparencyOp, headerOp, toolsetSchemaHolder, toolsetName, familyHeaderFont, weightHeaderFont, sizeHeaderFontDiff, familyFont, weightFont, sizeFontDiff, drawableSchemaHolder, version); /* }*/ logger.info("ProcessThemeController ! IP=" + httpServletRequest.getRemoteAddr()); return new ModelAndView("json/success"); }
From source file:com.aoindustries.website.clientarea.accounting.AddCreditCardCompletedAction.java
@Override public ActionForward executePermissionGranted(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, SiteSettings siteSettings, Locale locale, Skin skin, AOServConnector aoConn) throws Exception { AddCreditCardForm addCreditCardForm = (AddCreditCardForm) form; String accounting = addCreditCardForm.getAccounting(); if (GenericValidator.isBlankOrNull(accounting)) { // Redirect back to credit-card-manager it no accounting selected return mapping.findForward("credit-card-manager"); }//from w ww.j a va 2s .c o m // Validation ActionMessages errors = addCreditCardForm.validate(mapping, request); if (errors != null && !errors.isEmpty()) { saveErrors(request, errors); // Init request values before showing input initRequestAttributes(request, getServlet().getServletContext()); return mapping.findForward("input"); } // Get the credit card processor for the root connector of this website AOServConnector rootConn = siteSettings.getRootAOServConnector(); CreditCardProcessor creditCardProcessor = CreditCardProcessorFactory.getCreditCardProcessor(rootConn); if (creditCardProcessor == null) throw new SQLException("Unable to find enabled CreditCardProcessor for root connector"); // Add card if (!creditCardProcessor.canStoreCreditCards()) throw new SQLException("CreditCardProcessor indicates it does not support storing credit cards."); creditCardProcessor.storeCreditCard( new AOServConnectorPrincipal(rootConn, aoConn.getThisBusinessAdministrator().getUsername().getUsername().toString()), new BusinessGroup(aoConn.getBusinesses().get(AccountingCode.valueOf(accounting)), accounting), new CreditCard(null, // persistenceUniqueId null, // principalName null, // groupName null, // providerId null, // providerUniqueId addCreditCardForm.getCardNumber(), null, // maskedCardNumber Byte.parseByte(addCreditCardForm.getExpirationMonth()), Short.parseShort(addCreditCardForm.getExpirationYear()), addCreditCardForm.getCardCode(), addCreditCardForm.getFirstName(), addCreditCardForm.getLastName(), addCreditCardForm.getCompanyName(), null, null, null, null, null, addCreditCardForm.getStreetAddress1(), addCreditCardForm.getStreetAddress2(), addCreditCardForm.getCity(), addCreditCardForm.getState(), addCreditCardForm.getPostalCode(), addCreditCardForm.getCountryCode(), addCreditCardForm.getDescription())); request.setAttribute("cardNumber", CreditCard.maskCreditCardNumber(addCreditCardForm.getCardNumber())); return mapping.findForward("success"); }
From source file:org.catechis.Transformer.java
/** *Takes a string of encoded text and return its bytes in a string to avoid *the new strin from being encoded./*from ww w .j av a 2 s .c o m*/ */ public static String getStringFromBytesString(String bytes_text) { String reg_exp = "-"; String strings[] = bytes_text.split(reg_exp); int s_size = strings.length; int i = 0; StringBuffer str_buf = new StringBuffer(); byte[] bytes; bytes = new byte[s_size]; while (i < s_size) { if (i == 0) { // for some reason the first part of the array is only a dash, // like this: --20-120-104-19-107-103 // followed by another dash and byte, so we stip of the first dash this way. // Otherwise we get a nfe (see below) } else { try { bytes[i] = Byte.parseByte("-" + strings[i]); } catch (java.lang.NumberFormatException nfe) { // this happens if we dont ignore the girst byte return new String("nfe"); } } i++; } String result = new String(bytes); return result; }
From source file:org.dashbuilder.config.ConfigReader.java
public @Produces @Config byte readPrimitiveByte(InjectionPoint p) { String val = readConfig(p); return Byte.parseByte(val); }
From source file:org.sonar.api.checks.checkers.AnnotationCheckerFactory.java
private void configureField(Object checker, Field field, Map.Entry<String, String> parameter) throws IllegalAccessException { field.setAccessible(true);// w w w . j a va 2s . co m if (field.getType().equals(String.class)) { field.set(checker, parameter.getValue()); } else if (field.getType().getSimpleName().equals("int")) { field.setInt(checker, Integer.parseInt(parameter.getValue())); } else if (field.getType().getSimpleName().equals("short")) { field.setShort(checker, Short.parseShort(parameter.getValue())); } else if (field.getType().getSimpleName().equals("long")) { field.setLong(checker, Long.parseLong(parameter.getValue())); } else if (field.getType().getSimpleName().equals("double")) { field.setDouble(checker, Double.parseDouble(parameter.getValue())); } else if (field.getType().getSimpleName().equals("boolean")) { field.setBoolean(checker, Boolean.parseBoolean(parameter.getValue())); } else if (field.getType().getSimpleName().equals("byte")) { field.setByte(checker, Byte.parseByte(parameter.getValue())); } else if (field.getType().equals(Integer.class)) { field.set(checker, new Integer(Integer.parseInt(parameter.getValue()))); } else if (field.getType().equals(Long.class)) { field.set(checker, new Long(Long.parseLong(parameter.getValue()))); } else if (field.getType().equals(Double.class)) { field.set(checker, new Double(Double.parseDouble(parameter.getValue()))); } else if (field.getType().equals(Boolean.class)) { field.set(checker, Boolean.valueOf(Boolean.parseBoolean(parameter.getValue()))); } else { throw new UnvalidCheckerException( "The type of the field " + field + " is not supported: " + field.getType()); } }
From source file:org.apache.sysml.runtime.instructions.mr.TernaryInstruction.java
@Override public byte[] getAllIndexes() { return ArrayUtils.toPrimitive(Arrays.stream(new CPOperand[] { input1, input2, input3, output }) .filter(in -> in.isMatrix()).map(in -> Byte.parseByte(in.getName())).toArray(Byte[]::new)); }
From source file:se.crafted.chrisb.ecoCreature.rewards.models.ItemDrop.java
private static Byte parseData(String dropString) { String[] dropParts = dropString.split(":"); String[] itemParts = dropParts[0].split(","); String[] itemSubParts = itemParts[0].split("\\."); return itemSubParts.length > 1 && !itemSubParts[1].isEmpty() ? Byte.parseByte(itemSubParts[1]) : null; }
From source file:org.icelib.beans.ObjectMapping.java
@SuppressWarnings({ "unchecked", "restriction", "rawtypes" }) public static <V> V value(Object value, Class<V> targetClass, Object... constructorArgs) { Transformer<Object, V> t = (Transformer<Object, V>) objectConverters .get(new ClassKey(value.getClass(), targetClass)); if (t != null) { return (V) (t.transform(value)); }//from w w w . j a v a 2 s. c o m if (value instanceof Number) { if (targetClass.equals(Long.class) || targetClass.equals(long.class)) { return (V) ((Long) (((Number) value).longValue())); } else if (targetClass.equals(Double.class) || targetClass.equals(double.class)) { return (V) ((Double) (((Number) value).doubleValue())); } else if (targetClass.equals(Integer.class) || targetClass.equals(int.class)) { return (V) ((Integer) (((Number) value).intValue())); } else if (targetClass.equals(Short.class) || targetClass.equals(short.class)) { return (V) ((Short) (((Number) value).shortValue())); } else if (targetClass.equals(Float.class) || targetClass.equals(float.class)) { return (V) ((Float) (((Number) value).floatValue())); } else if (targetClass.equals(Byte.class) || targetClass.equals(byte.class)) { return (V) ((Byte) (((Number) value).byteValue())); } else if (targetClass.equals(Boolean.class) || targetClass.equals(boolean.class)) { return (V) ((Boolean) (((Number) value).intValue() != 0)); } else if (targetClass.equals(String.class)) { return (V) (value.toString()); } else { throw new IllegalArgumentException( String.format("Cannot convert number %s to %s", value, targetClass)); } } else if (value instanceof Boolean) { return (V) (((Boolean) value)); } else if (value instanceof String) { if (targetClass.equals(Long.class)) { return (V) ((Long) Long.parseLong((String) value)); } else if (targetClass.equals(Double.class)) { return (V) ((Double) Double.parseDouble((String) value)); } else if (targetClass.equals(Integer.class)) { return (V) ((Integer) Integer.parseInt((String) value)); } else if (targetClass.equals(Short.class)) { return (V) ((Short) Short.parseShort((String) value)); } else if (targetClass.equals(Float.class)) { return (V) ((Float) Float.parseFloat((String) value)); } else if (targetClass.equals(Byte.class)) { return (V) ((Byte) Byte.parseByte((String) value)); } else if (targetClass.equals(Boolean.class)) { return (V) ((Boolean) Boolean.parseBoolean((String) value)); } else if (targetClass.equals(String.class)) { return (V) (value.toString()); } else if (targetClass.isEnum()) { for (V ev : targetClass.getEnumConstants()) { if (ev.toString().toLowerCase().equalsIgnoreCase(value.toString())) { return ev; } } throw new IllegalArgumentException(String.format("Unknown enum %s in %s", value, targetClass)); } else { // Maybe the target has a constructor that takes a string try { Constructor<V> con = targetClass.getConstructor(String.class); return con.newInstance((String) value); } catch (NoSuchMethodException nsme) { // That's it, give up throw new IllegalArgumentException( String.format("Cannot convert string %s to %s", value, targetClass)); } catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } } } else if (value instanceof Map) { /* * This covers ScriptObjectMirror from Nashorn which is a list AND a * map describing an object or an array. It also covers ordinary map * and lists */ if (isScriptNativeArray(value)) { Collection<?> c = ((jdk.nashorn.api.scripting.ScriptObjectMirror) value).values(); if (c instanceof List && MappedList.class.isAssignableFrom(targetClass)) { try { V v = getTargetInstance(value, targetClass, constructorArgs); for (Object o : ((List) c)) { ((List) v).add(o); } return v; } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format( "No zero-length constructor for class %s and no valid constructor args provided.", targetClass)); } catch (InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(String.format("Could not construct %s", targetClass), e); } } else if ((List.class.isAssignableFrom(targetClass) && c instanceof List) || (Collection.class.isAssignableFrom(targetClass) && c instanceof Collection)) { return (V) c; } else if (Set.class.isAssignableFrom(targetClass)) { return (V) new LinkedHashSet<Object>(c); } else if (Collection.class.isAssignableFrom(targetClass)) { return (V) new ArrayList<Object>(c); } else if (targetClass.isArray()) { return (V) c.toArray((Object[]) Array.newInstance(targetClass.getComponentType(), c.size())); } } else if (value instanceof List && List.class.isAssignableFrom(targetClass)) { return (V) ((List<?>) value); } else if (!isScriptNativeObject(value) && Map.class.isAssignableFrom(targetClass)) { return (V) ((Map<?, ?>) value); } else { try { V v = getTargetInstance(value, targetClass, constructorArgs); ObjectMapper<?> m = new ObjectMapper<>(v); try { m.map((Map<String, Object>) value); } catch (Exception e) { throw new RuntimeException(String.format("Failed to map %s.", targetClass), e); } return v; } catch (NoSuchMethodException e) { throw new IllegalArgumentException(String.format( "No zero-length constructor for class %s and no valid constructor args provided.", targetClass)); } catch (InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(String.format("Could not construct %s", targetClass), e); } } } else if (targetClass.isAssignableFrom(value.getClass())) { return (V) value; } else if (value instanceof Collection && List.class.isAssignableFrom(targetClass)) { try { V v = getTargetInstance(value, targetClass, constructorArgs); ((List) v).addAll((Collection) value); return v; } catch (InstantiationException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(String.format("Could not construct %s", targetClass), e); } catch (NoSuchMethodException nse) { } } throw new IllegalArgumentException( String.format("Cannot convert %s (%s) to %s", value, value.getClass(), targetClass)); }
From source file:org.sonar.api.checks.AnnotationCheckFactory.java
private void configureField(Object check, Field field, String value) { try {//from w ww. ja v a 2s.c o m field.setAccessible(true); if (field.getType().equals(String.class)) { field.set(check, value); } else if ("int".equals(field.getType().getSimpleName())) { field.setInt(check, Integer.parseInt(value)); } else if ("short".equals(field.getType().getSimpleName())) { field.setShort(check, Short.parseShort(value)); } else if ("long".equals(field.getType().getSimpleName())) { field.setLong(check, Long.parseLong(value)); } else if ("double".equals(field.getType().getSimpleName())) { field.setDouble(check, Double.parseDouble(value)); } else if ("boolean".equals(field.getType().getSimpleName())) { field.setBoolean(check, Boolean.parseBoolean(value)); } else if ("byte".equals(field.getType().getSimpleName())) { field.setByte(check, Byte.parseByte(value)); } else if (field.getType().equals(Integer.class)) { field.set(check, Integer.parseInt(value)); } else if (field.getType().equals(Long.class)) { field.set(check, Long.parseLong(value)); } else if (field.getType().equals(Double.class)) { field.set(check, Double.parseDouble(value)); } else if (field.getType().equals(Boolean.class)) { field.set(check, Boolean.parseBoolean(value)); } else { throw new SonarException( "The type of the field " + field + " is not supported: " + field.getType()); } } catch (IllegalAccessException e) { throw new SonarException( "Can not set the value of the field " + field + " in the class: " + check.getClass().getName(), e); } }
From source file:com.l2jfree.gameserver.instancemanager.DimensionalRiftManager.java
public void load() { int countGood = 0, countBad = 0; try {//w w w . j av a2s .c o m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setIgnoringComments(true); File file = new File(Config.DATAPACK_ROOT, "data/dimensionalRift.xml"); if (!file.exists()) throw new IOException(); Document doc = factory.newDocumentBuilder().parse(file); NamedNodeMap attrs; byte type, roomId; int mobId, x, y, z, delay, count; L2Spawn spawnDat; L2NpcTemplate template; int xMin = 0, xMax = 0, yMin = 0, yMax = 0, zMin = 0, zMax = 0, xT = 0, yT = 0, zT = 0; boolean isBossRoom; for (Node rift = doc.getFirstChild(); rift != null; rift = rift.getNextSibling()) { if ("rift".equalsIgnoreCase(rift.getNodeName())) { for (Node area = rift.getFirstChild(); area != null; area = area.getNextSibling()) { if ("area".equalsIgnoreCase(area.getNodeName())) { attrs = area.getAttributes(); type = Byte.parseByte(attrs.getNamedItem("type").getNodeValue()); for (Node room = area.getFirstChild(); room != null; room = room.getNextSibling()) { if ("room".equalsIgnoreCase(room.getNodeName())) { attrs = room.getAttributes(); roomId = Byte.parseByte(attrs.getNamedItem("id").getNodeValue()); Node boss = attrs.getNamedItem("isBossRoom"); isBossRoom = boss != null && Boolean.parseBoolean(boss.getNodeValue()); for (Node coord = room.getFirstChild(); coord != null; coord = coord .getNextSibling()) { if ("teleport".equalsIgnoreCase(coord.getNodeName())) { attrs = coord.getAttributes(); xT = Integer.parseInt(attrs.getNamedItem("x").getNodeValue()); yT = Integer.parseInt(attrs.getNamedItem("y").getNodeValue()); zT = Integer.parseInt(attrs.getNamedItem("z").getNodeValue()); } else if ("zone".equalsIgnoreCase(coord.getNodeName())) { attrs = coord.getAttributes(); xMin = Integer.parseInt(attrs.getNamedItem("xMin").getNodeValue()); xMax = Integer.parseInt(attrs.getNamedItem("xMax").getNodeValue()); yMin = Integer.parseInt(attrs.getNamedItem("yMin").getNodeValue()); yMax = Integer.parseInt(attrs.getNamedItem("yMax").getNodeValue()); zMin = Integer.parseInt(attrs.getNamedItem("zMin").getNodeValue()); zMax = Integer.parseInt(attrs.getNamedItem("zMax").getNodeValue()); } } if (!_rooms.containsKey(type)) _rooms.put(type, new FastMap<Byte, DimensionalRiftRoom>()); _rooms.get(type).put(roomId, new DimensionalRiftRoom(type, roomId, xMin, xMax, yMin, yMax, zMin, zMax, xT, yT, zT, isBossRoom)); for (Node spawn = room.getFirstChild(); spawn != null; spawn = spawn .getNextSibling()) { if ("spawn".equalsIgnoreCase(spawn.getNodeName())) { attrs = spawn.getAttributes(); mobId = Integer.parseInt(attrs.getNamedItem("mobId").getNodeValue()); delay = Integer.parseInt(attrs.getNamedItem("delay").getNodeValue()); count = Integer.parseInt(attrs.getNamedItem("count").getNodeValue()); template = NpcTable.getInstance().getTemplate(mobId); if (template == null) { _log.warn("Template " + mobId + " not found!"); } if (!_rooms.containsKey(type)) { _log.warn("Type " + type + " not found!"); } else if (!_rooms.get(type).containsKey(roomId)) { _log.warn("Room " + roomId + " in Type " + type + " not found!"); } for (int i = 0; i < count; i++) { DimensionalRiftRoom riftRoom = _rooms.get(type).get(roomId); x = riftRoom.getRandomX(); y = riftRoom.getRandomY(); z = riftRoom.getTeleportCoords()[2]; if (template != null && _rooms.containsKey(type) && _rooms.get(type).containsKey(roomId)) { spawnDat = new L2Spawn(template); spawnDat.setAmount(1); spawnDat.setLocx(x); spawnDat.setLocy(y); spawnDat.setLocz(z); spawnDat.setHeading(-1); spawnDat.setRespawnDelay(delay); SpawnTable.getInstance().addNewSpawn(spawnDat, false); _rooms.get(type).get(roomId).getSpawns().add(spawnDat); countGood++; } else { countBad++; } } } } } } } } } } } catch (Exception e) { _log.warn("Error on loading dimensional rift spawns: ", e); } int typeSize = _rooms.keySet().size(); int roomSize = 0; for (Byte b : _rooms.keySet()) roomSize += _rooms.get(b).keySet().size(); _log.info("DimensionalRiftManager: Loaded " + typeSize + " room types with " + roomSize + " rooms."); _log.info("DimensionalRiftManager: Loaded " + countGood + " dimensional rift spawns, " + countBad + " errors."); }