📄 javautils.java
字号:
if (value == null) ; // Don't need to set anything else valueField.set(holder, value); // Automatically unwraps value to primitive } else { valueField.set(holder, value); } } catch (Exception e) { throw new HolderException(Messages.getMessage("exception01", e.getMessage())); } } public static class HolderException extends Exception { public HolderException(String msg) { super(msg); } } /** * Used to cache a result from IsEnumClassSub(). * Class->Boolean mapping. */ private static WeakHashMap enumMap = new WeakHashMap(); /** * Determine if the class is a JAX-RPC enum class. * An enumeration class is recognized by * a getValue() method, a toString() method, a fromString(String) method * a fromValue(type) method and the lack * of a setValue(type) method */ public static boolean isEnumClass(Class cls) { Boolean b = (Boolean)enumMap.get(cls); if (b == null) { b = (isEnumClassSub(cls)) ? Boolean.TRUE : Boolean.FALSE; synchronized (enumMap) { enumMap.put(cls, b); } } return b.booleanValue(); } private static boolean isEnumClassSub(Class cls) { try { java.lang.reflect.Method[] methods = cls.getMethods(); java.lang.reflect.Method getValueMethod = null, fromValueMethod = null, setValueMethod = null, fromStringMethod = null; // linear search: in practice, this is faster than // sorting/searching a short array of methods. for (int i = 0; i < methods.length; i++) { String name = methods[i].getName(); if (name.equals("getValue") && methods[i].getParameterTypes().length == 0) { // getValue() getValueMethod = methods[i]; } else if (name.equals("fromString")) { // fromString(String s) Object[] params = methods[i].getParameterTypes(); if (params.length == 1 && params[0] == String.class) { fromStringMethod = methods[i]; } } else if (name.equals("fromValue") && methods[i].getParameterTypes().length == 1) { // fromValue(Something s) fromValueMethod = methods[i]; } else if (name.equals("setValue") && methods[i].getParameterTypes().length == 1) { // setValue(Something s) setValueMethod = methods[i]; } } // must have getValue and fromString, but not setValue // must also have toString(), but every Object subclass has that, so // no need to check for it. if (null != getValueMethod && null != fromStringMethod) { if (null != setValueMethod && setValueMethod.getParameterTypes().length == 1 && getValueMethod.getReturnType() == setValueMethod.getParameterTypes()[0]) { // setValue exists: return false return false; } else { return true; } } else { return false; } } catch (java.lang.SecurityException e) { return false; } // end of catch } public static String stackToString(Throwable e){ java.io.StringWriter sw= new java.io.StringWriter(1024); java.io.PrintWriter pw= new java.io.PrintWriter(sw); e.printStackTrace(pw); pw.close(); return sw.toString(); } /** * Tests the String 'value': * return 'false' if its 'false', '0', or 'no' - else 'true' * * Follow in 'C' tradition of boolean values: * false is specific (0), everything else is true; */ public static final boolean isTrue(String value) { return !isFalseExplicitly(value); } /** * Tests the String 'value': * return 'true' if its 'true', '1', or 'yes' - else 'false' */ public static final boolean isTrueExplicitly(String value) { return value != null && (value.equalsIgnoreCase("true") || value.equals("1") || value.equalsIgnoreCase("yes")); } /** * Tests the Object 'value': * if its null, return default. * if its a Boolean, return booleanValue() * if its an Integer, return 'false' if its '0' else 'true' * if its a String, return isTrueExplicitly((String)value). * All other types return 'true' */ public static final boolean isTrueExplicitly(Object value, boolean defaultVal) { if ( value == null ) return defaultVal; if ( value instanceof Boolean ) { return ((Boolean)value).booleanValue(); } if ( value instanceof Integer ) { return ((Integer)value).intValue() != 0; } if ( value instanceof String ) { return isTrueExplicitly( (String)value ); } return true; } public static final boolean isTrueExplicitly(Object value) { return isTrueExplicitly(value, false); } /** * Tests the Object 'value': * if its null, return default. * if its a Boolean, return booleanValue() * if its an Integer, return 'false' if its '0' else 'true' * if its a String, return 'false' if its 'false', 'no', or '0' - else 'true' * All other types return 'true' */ public static final boolean isTrue(Object value, boolean defaultVal) { return !isFalseExplicitly(value, !defaultVal); } public static final boolean isTrue(Object value) { return isTrue(value, false); } /** * Tests the String 'value': * return 'true' if its 'false', '0', or 'no' - else 'false' * * Follow in 'C' tradition of boolean values: * false is specific (0), everything else is true; */ public static final boolean isFalse(String value) { return isFalseExplicitly(value); } /** * Tests the String 'value': * return 'true' if its null, 'false', '0', or 'no' - else 'false' */ public static final boolean isFalseExplicitly(String value) { return value == null || value.equalsIgnoreCase("false") || value.equals("0") || value.equalsIgnoreCase("no"); } /** * Tests the Object 'value': * if its null, return default. * if its a Boolean, return !booleanValue() * if its an Integer, return 'true' if its '0' else 'false' * if its a String, return isFalseExplicitly((String)value). * All other types return 'false' */ public static final boolean isFalseExplicitly(Object value, boolean defaultVal) { if ( value == null ) return defaultVal; if ( value instanceof Boolean ) { return !((Boolean)value).booleanValue(); } if ( value instanceof Integer ) { return ((Integer)value).intValue() == 0; } if ( value instanceof String ) { return isFalseExplicitly( (String)value ); } return false; } public static final boolean isFalseExplicitly(Object value) { return isFalseExplicitly(value, true); } /** * Tests the Object 'value': * if its null, return default. * if its a Boolean, return booleanValue() * if its an Integer, return 'false' if its '0' else 'true' * if its a String, return 'false' if its 'false', 'no', or '0' - else 'true' * All other types return 'true' */ public static final boolean isFalse(Object value, boolean defaultVal) { return isFalseExplicitly(value, defaultVal); } public static final boolean isFalse(Object value) { return isFalse(value, true); } /** * Given the MIME type string, return the Java mapping. */ public static String mimeToJava(String mime) { if ("image/gif".equals(mime) || "image/jpeg".equals(mime)) { return "java.awt.Image"; } else if ("text/plain".equals(mime)) { return "java.lang.String"; } else if ("text/xml".equals(mime) || "application/xml".equals(mime)) { return "javax.xml.transform.Source"; } else if ("application/octet-stream".equals(mime)|| "application/octetstream".equals(mime)) { return "org.apache.axis.attachments.OctetStream"; } else if (mime != null && mime.startsWith("multipart/")) { return "javax.mail.internet.MimeMultipart"; } else { return "javax.activation.DataHandler"; } } // mimeToJava //avoid testing and possibly failing everytime. private static boolean checkForAttachmentSupport = true; private static boolean attachmentSupportEnabled = false; /** * Determine whether attachments are supported by checking if the following * classes are available: javax.activation.DataHandler, * javax.mail.internet.MimeMultipart. */ public static synchronized boolean isAttachmentSupported() { if (checkForAttachmentSupport) { //aviod testing and possibly failing everytime. checkForAttachmentSupport = false; try { // Attempt to resolve DataHandler and MimeMultipart and // javax.xml.transform.Source, all necessary for full // attachment support ClassUtils.forName("javax.activation.DataHandler"); ClassUtils.forName("javax.mail.internet.MimeMultipart"); attachmentSupportEnabled = true; } catch (Throwable t) { } log.debug(Messages.getMessage("attachEnabled") + " " + attachmentSupportEnabled); if(!attachmentSupportEnabled) { log.warn(Messages.getMessage("attachDisabled")); } } return attachmentSupportEnabled; } // isAttachmentSupported /** * Makes the value passed in <code>initValue</code> unique among the * {@link String} values contained in <code>values</code> by suffixing * it with a decimal digit suffix. */ public static String getUniqueValue(Collection values, String initValue) { if (!values.contains(initValue)) { return initValue; } else { StringBuffer unqVal = new StringBuffer(initValue); int beg = unqVal.length(), cur, end; while (Character.isDigit(unqVal.charAt(beg - 1))) { beg--; } if (beg == unqVal.length()) { unqVal.append('1'); } cur = end = unqVal.length() - 1; while (values.contains(unqVal.toString())) { if (unqVal.charAt(cur) < '9') { unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1)); } else { while (cur-- > beg) { if (unqVal.charAt(cur) < '9') { unqVal.setCharAt(cur, (char) (unqVal.charAt(cur) + 1)); break; } } // See if there's a need to insert a new digit. if (cur < beg) { unqVal.insert(++cur, '1'); end++; } while (cur < end) { unqVal.setCharAt(++cur, '0'); } } } return unqVal.toString(); } /* For else clause of selection-statement If(!values ... */ } /* For class method JavaUtils.getUniqueValue */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -