diff --git a/src/java.management/share/classes/javax/management/modelmbean/DescriptorSupport.java b/src/java.management/share/classes/javax/management/modelmbean/DescriptorSupport.java index d003cd9d9d671..5015567adc920 100644 --- a/src/java.management/share/classes/javax/management/modelmbean/DescriptorSupport.java +++ b/src/java.management/share/classes/javax/management/modelmbean/DescriptorSupport.java @@ -174,137 +174,6 @@ public DescriptorSupport(DescriptorSupport inDescr) { init(inDescr.descriptorMap); } - - /** - *

Descriptor constructor taking an XML String.

- * - *

The format of the XML string is not defined, but an - * implementation must ensure that the string returned by - * {@link #toXMLString() toXMLString()} on an existing - * descriptor can be used to instantiate an equivalent - * descriptor using this constructor.

- * - *

In this implementation, all field values will be created - * as Strings. If the field values are not Strings, the - * programmer will have to reset or convert these fields - * correctly.

- * - * @param inStr An XML-formatted string used to populate this - * Descriptor. The format is not defined, but any - * implementation must ensure that the string returned by - * method {@link #toXMLString toXMLString} on an existing - * descriptor can be used to instantiate an equivalent - * descriptor when instantiated using this constructor. - * - * @exception RuntimeOperationsException If the String inStr - * passed in parameter is null - * @exception XMLParseException XML parsing problem while parsing - * the input String - * @exception MBeanException Wraps a distributed communication Exception. - * @deprecated This constructor exists for historical reasons. If - * reading from XML is required, it should be implemented externally. - */ - /* At some stage we should rewrite this code to be cleverer. Using - a StringTokenizer as we do means, first, that we accept a lot of - bogus strings without noticing they are bogus, and second, that we - split the string being parsed at characters like > even if they - occur in the middle of a field value. */ - @Deprecated(since="25", forRemoval=true) - @SuppressWarnings("removal") - public DescriptorSupport(String inStr) - throws MBeanException, RuntimeOperationsException, - XMLParseException { - /* parse an XML-formatted string and populate internal - * structure with it */ - if (MODELMBEAN_LOGGER.isLoggable(Level.TRACE)) { - MODELMBEAN_LOGGER.log(Level.TRACE, - "Descriptor(String = '" + inStr + "') Constructor"); - } - if (inStr == null) { - if (MODELMBEAN_LOGGER.isLoggable(Level.TRACE)) { - MODELMBEAN_LOGGER.log(Level.TRACE, - "Descriptor(String = null) Illegal arguments"); - } - final String msg = "String in parameter is null"; - final RuntimeException iae = new IllegalArgumentException(msg); - throw new RuntimeOperationsException(iae, msg); - } - - final String lowerInStr = inStr.toLowerCase(Locale.ENGLISH); - if (!lowerInStr.startsWith("") - || !lowerInStr.endsWith("")) { - throw new XMLParseException("No , pair"); - // XMLParseException is deprecated for removal. - } - - // parse xmlstring into structures - init(null); - // create dummy descriptor: should have same size - // as number of fields in xmlstring - // loop through structures and put them in descriptor - - StringTokenizer st = new StringTokenizer(inStr, "<> \t\n\r\f"); - - boolean inFld = false; - boolean inDesc = false; - String fieldName = null; - String fieldValue = null; - - - while (st.hasMoreTokens()) { // loop through tokens - String tok = st.nextToken(); - - if (tok.equalsIgnoreCase("FIELD")) { - inFld = true; - } else if (tok.equalsIgnoreCase("/FIELD")) { - if ((fieldName != null) && (fieldValue != null)) { - fieldName = - fieldName.substring(fieldName.indexOf('"') + 1, - fieldName.lastIndexOf('"')); - final Object fieldValueObject = - parseQuotedFieldValue(fieldValue); - setField(fieldName, fieldValueObject); - } - fieldName = null; - fieldValue = null; - inFld = false; - } else if (tok.equalsIgnoreCase("DESCRIPTOR")) { - inDesc = true; - } else if (tok.equalsIgnoreCase("/DESCRIPTOR")) { - inDesc = false; - fieldName = null; - fieldValue = null; - inFld = false; - } else if (inFld && inDesc) { - // want kw=value, eg, name="myname" value="myvalue" - int eq_separator = tok.indexOf('='); - if (eq_separator > 0) { - String kwPart = tok.substring(0,eq_separator); - String valPart = tok.substring(eq_separator+1); - if (kwPart.equalsIgnoreCase("NAME")) - fieldName = valPart; - else if (kwPart.equalsIgnoreCase("VALUE")) - fieldValue = valPart; - else { // xml parse exception - final String msg = - "Expected `name' or `value', got `" + tok + "'"; - throw new XMLParseException(msg); - // XMLParseException is deprecated for removal. - } - } else { // xml parse exception - final String msg = - "Expected `keyword=value', got `" + tok + "'"; - throw new XMLParseException(msg); - // XMLParseException is deprecated for removal. - } - } - } // while tokens - if (MODELMBEAN_LOGGER.isLoggable(Level.TRACE)) { - MODELMBEAN_LOGGER.log(Level.TRACE, - "Descriptor(XMLString) Exit"); - } - } - /** * Constructor taking field names and field values. Neither array * can be null. @@ -946,231 +815,6 @@ private boolean validateField(String fldName, Object fldValue) { return true; } - - - /** - *

Returns an XML String representing the descriptor.

- * - *

The format is not defined, but an implementation must - * ensure that the string returned by this method can be - * used to build an equivalent descriptor when instantiated - * using the constructor {@link #DescriptorSupport(String) - * DescriptorSupport(String inStr)}.

- * - *

Fields which are not String objects will have toString() - * called on them to create the value. The value will be - * enclosed in parentheses. It is not guaranteed that you can - * reconstruct these objects unless they have been - * specifically set up to support toString() in a meaningful - * format and have a matching constructor that accepts a - * String in the same format.

- * - *

If the descriptor is empty the following String is - * returned: <Descriptor></Descriptor>

- * - * @return the XML string. - * - * @exception RuntimeOperationsException for illegal value for - * field Names or field Values. If the XML formatted string - * construction fails for any reason, this exception will be - * thrown. - * @deprecated This method exists for historical reasons. If - * writing to XML is required, it should be implemented externally. - */ - @Deprecated(since="25", forRemoval=true) - public synchronized String toXMLString() { - final StringBuilder buf = new StringBuilder(""); - Set> returnedSet = descriptorMap.entrySet(); - for (Map.Entry currElement : returnedSet) { - final String name = currElement.getKey(); - Object value = currElement.getValue(); - String valueString = null; - /* Set valueString to non-null if and only if this is a string that - cannot be confused with the encoding of an object. If it - could be so confused (surrounded by parentheses) then we - call makeFieldValue as for any non-String object and end - up with an encoding like "(java.lang.String/(thing))". */ - if (value instanceof String) { - final String svalue = (String) value; - if (!svalue.startsWith("(") || !svalue.endsWith(")")) - valueString = quote(svalue); - } - if (valueString == null) - valueString = makeFieldValue(value); - buf.append(""); - } - buf.append(""); - return buf.toString(); - } - - private static final String[] entities = { - " ", - "\""", - "<<", - ">>", - "&&", - "\r ", - "\t ", - "\n ", - "\f ", - }; - private static final Map entityToCharMap = - new HashMap<>(); - private static final String[] charToEntityMap; - - static { - char maxChar = 0; - for (int i = 0; i < entities.length; i++) { - final char c = entities[i].charAt(0); - if (c > maxChar) - maxChar = c; - } - charToEntityMap = new String[maxChar + 1]; - for (int i = 0; i < entities.length; i++) { - final char c = entities[i].charAt(0); - final String entity = entities[i].substring(1); - charToEntityMap[c] = entity; - entityToCharMap.put(entity, c); - } - } - - private static boolean isMagic(char c) { - return (c < charToEntityMap.length && charToEntityMap[c] != null); - } - - /* - * Quote the string so that it will be acceptable to the (String) - * constructor. Since the parsing code in that constructor is fairly - * stupid, we're obliged to quote apparently innocuous characters like - * space, <, and >. In a future version, we should rewrite the parser - * and only quote " plus either \ or & (depending on the quote syntax). - */ - private static String quote(String s) { - boolean found = false; - for (int i = 0; i < s.length(); i++) { - if (isMagic(s.charAt(i))) { - found = true; - break; - } - } - if (!found) - return s; - final StringBuilder buf = new StringBuilder(); - for (int i = 0; i < s.length(); i++) { - char c = s.charAt(i); - if (isMagic(c)) - buf.append(charToEntityMap[c]); - else - buf.append(c); - } - return buf.toString(); - } - - @SuppressWarnings("removal") - private static String unquote(String s) throws XMLParseException { - if (!s.startsWith("\"") || !s.endsWith("\"")) { - throw new XMLParseException("Value must be quoted: <" + s + ">"); - // XMLParseException is deprecated for removal. - } - final StringBuilder buf = new StringBuilder(); - final int len = s.length() - 1; - for (int i = 1; i < len; i++) { - final char c = s.charAt(i); - final int semi; - final Character quoted; - if (c == '&' - && (semi = s.indexOf(';', i + 1)) >= 0 - && ((quoted = entityToCharMap.get(s.substring(i, semi+1))) - != null)) { - buf.append(quoted); - i = semi; - } else - buf.append(c); - } - return buf.toString(); - } - - /** - * Make the string that will go inside "..." for a value that is not - * a plain String. - * @throws RuntimeOperationsException if the value cannot be encoded. - */ - private static String makeFieldValue(Object value) { - if (value == null) - return "(null)"; - - Class valueClass = value.getClass(); - try { - valueClass.getConstructor(String.class); - } catch (NoSuchMethodException e) { - final String msg = - "Class " + valueClass + " does not have a public " + - "constructor with a single string arg"; - final RuntimeException iae = new IllegalArgumentException(msg); - throw new RuntimeOperationsException(iae, - "Cannot make XML descriptor"); - } catch (SecurityException e) { - // OK: we'll pretend the constructor is there - // too bad if it's not: we'll find out when we try to - // reconstruct the DescriptorSupport - } - - final String quotedValueString = quote(value.toString()); - - return "(" + valueClass.getName() + "/" + quotedValueString + ")"; - } - - /* - * Parse a field value from the XML produced by toXMLString(). - * Given a descriptor XML containing , - * the argument to this method will be "vvv" (a string including the - * containing quote characters). If vvv begins and ends with parentheses, - * then it may contain: - * - the characters "null", in which case the result is null; - * - a value of the form "some.class.name/xxx", in which case the - * result is equivalent to `new some.class.name("xxx")'; - * - some other string, in which case the result is that string, - * without the parentheses. - */ - @SuppressWarnings("removal") - private static Object parseQuotedFieldValue(String s) - throws XMLParseException { - s = unquote(s); - if (s.equalsIgnoreCase("(null)")) - return null; - if (!s.startsWith("(") || !s.endsWith(")")) - return s; - final int slash = s.indexOf('/'); - if (slash < 0) { - // compatibility: old code didn't include class name - return s.substring(1, s.length() - 1); - } - final String className = s.substring(1, slash); - - final Constructor constr; - try { - final ClassLoader contextClassLoader = - Thread.currentThread().getContextClassLoader(); - final Class c = - Class.forName(className, false, contextClassLoader); - constr = c.getConstructor(new Class[] {String.class}); - } catch (Exception e) { - throw new XMLParseException(e, "Cannot parse value: <" + s + ">"); - // XMLParseException is deprecated for removal. - } - final String arg = s.substring(slash + 1, s.length() - 1); - try { - return constr.newInstance(new Object[] {arg}); - } catch (Exception e) { - final String msg = - "Cannot construct instance of " + className + - " with arg: <" + s + ">"; - throw new XMLParseException(e, msg); - // XMLParseException is deprecated for removal. - } - } - /** * Returns a human readable string representing the * descriptor. The string will be in the format of diff --git a/src/java.management/share/classes/javax/management/modelmbean/XMLParseException.java b/src/java.management/share/classes/javax/management/modelmbean/XMLParseException.java deleted file mode 100644 index efcfdf54d67de..0000000000000 --- a/src/java.management/share/classes/javax/management/modelmbean/XMLParseException.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (c) 2000, 2025, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ -/* - * @author IBM Corp. - * - * Copyright IBM Corp. 1999-2000. All rights reserved. - */ - - -package javax.management.modelmbean; - -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.io.ObjectStreamField; - -/** -* This exception is thrown when an XML formatted string is being parsed into ModelMBean objects -* or when XML formatted strings are being created from ModelMBean objects. -* -* It is also used to wrapper exceptions from XML parsers that may be used. -* -*

The serialVersionUID of this class is 3176664577895105181L. -* -* @deprecated This class exists only to support XML parsing implemented privately in this module, -* in DescriptorSupport. That feature is deprecated for removal. -* -* @since 1.5 -*/ -@Deprecated(since="25", forRemoval=true) -public class XMLParseException -extends Exception -{ - private static final long serialVersionUID = 3176664577895105181L; - - /** - * Default constructor . - */ - public XMLParseException () - { - super("XML Parse Exception."); - } - - /** - * Constructor taking a string. - * - * @param s the detail message. - */ - public XMLParseException (String s) - { - super("XML Parse Exception: " + s); - } - /** - * Constructor taking a string and an exception. - * - * @param e the nested exception. - * @param s the detail message. - */ - public XMLParseException (Exception e, String s) - { - super("XML Parse Exception: " + s + ":" + e.toString()); - } - - /** - * Deserializes an {@link XMLParseException} from an {@link ObjectInputStream}. - */ - private void readObject(ObjectInputStream in) - throws IOException, ClassNotFoundException { - // New serial form ignores extra field "msgStr" - in.defaultReadObject(); - } - - - /** - * Serializes an {@link XMLParseException} to an {@link ObjectOutputStream}. - */ - private void writeObject(ObjectOutputStream out) - throws IOException { - out.defaultWriteObject(); - } -} diff --git a/test/jdk/javax/management/MBeanServer/ExceptionFactory.java b/test/jdk/javax/management/MBeanServer/ExceptionFactory.java index a615c5252da7a..dd82cf288f46c 100644 --- a/test/jdk/javax/management/MBeanServer/ExceptionFactory.java +++ b/test/jdk/javax/management/MBeanServer/ExceptionFactory.java @@ -46,7 +46,6 @@ import javax.management.ServiceNotFoundException; import javax.management.StringValueExp; import javax.management.modelmbean.InvalidTargetObjectTypeException; -import javax.management.modelmbean.XMLParseException; import javax.management.monitor.MonitorSettingException; import javax.management.openmbean.InvalidKeyException; import javax.management.openmbean.InvalidOpenTypeException; @@ -100,7 +99,6 @@ public class ExceptionFactory { exceptions.add(new RuntimeOperationsException(new RuntimeException(mes), mes)); exceptions.add(new ServiceNotFoundException()); exceptions.add(new InvalidTargetObjectTypeException()); - exceptions.add(new XMLParseException()); exceptions.add(new MonitorSettingException()); exceptions.add(new InvalidKeyException()); exceptions.add(new InvalidOpenTypeException()); diff --git a/test/jdk/javax/management/modelmbean/DescriptorSupportTest.java b/test/jdk/javax/management/modelmbean/DescriptorSupportTest.java index ed33c737175ac..45c397f987e4d 100644 --- a/test/jdk/javax/management/modelmbean/DescriptorSupportTest.java +++ b/test/jdk/javax/management/modelmbean/DescriptorSupportTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -183,9 +183,6 @@ public static void main(String[] args) throws Exception { ok &= caseTest(new DescriptorSupport(d1), "DescriptorSupport(Descriptor)"); d1 = new DescriptorSupport(new String[] {"NAME=blah"}); - ok &= caseTest(new DescriptorSupport(d1.toXMLString()), - "DescriptorSupport(String)"); - d1 = new DescriptorSupport(new String[] {"NAME=blah"}); ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(d1); diff --git a/test/jdk/javax/management/modelmbean/DescriptorSupportXMLTest.java b/test/jdk/javax/management/modelmbean/DescriptorSupportXMLTest.java deleted file mode 100644 index dc66b2ad79456..0000000000000 --- a/test/jdk/javax/management/modelmbean/DescriptorSupportXMLTest.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. - * - * This code is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 2 only, as - * published by the Free Software Foundation. - * - * This code is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * version 2 for more details (a copy is included in the LICENSE file that - * accompanied this code). - * - * You should have received a copy of the GNU General Public License version - * 2 along with this work; if not, write to the Free Software Foundation, - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA - * or visit www.oracle.com if you need additional information or have any - * questions. - */ - -/* - * @test - * @bug 4957393 - * @summary Test that DescriptorSupport.toXMLString() can be used to - * reconstruct an equivalent DescriptorSupport - * @author Eamonn McManus - * - * @run clean DescriptorSupportXMLTest - * @run build DescriptorSupportXMLTest - * @run main DescriptorSupportXMLTest - */ - -import java.util.Arrays; - -import javax.management.RuntimeOperationsException; -import javax.management.modelmbean.DescriptorSupport; - -public class DescriptorSupportXMLTest { - public static void main(String[] args) throws Exception { - System.out.println("Testing that DescriptorSupport.toXMLString() " + - "can be used to reconstruct an equivalent " + - "DescriptorSupport"); - int failed = 0; - - final Object[] testValues = { - // Values that should be encodable. - "", - "ok", - "null", - "(open", - "close)", - "(parens)", - "quote\"quote", - "a description with several words", - "magic&\"\\<> \r\t\n\f;&;magic", - "<descriptor>&&&</descriptor>", - "<descriptor>&&&</blahblahblah>", - null, - new Integer(10), - Boolean.TRUE, - new Float(1.0f), - - // Values that are not encodable: it is important that we throw - // an exception during encoding rather than waiting until decode - // time to discover the problem. These classes are not encodable - // because they don't have a (String) constructor. - new Character('!'), - new java.util.HashMap(), - }; - - for (int i = 0; i < testValues.length; i++) { - final Object v = testValues[i]; - final String what = - (v == null) ? "null" : - (v.getClass().getName() + "{" + v + "}"); - - final DescriptorSupport in = - new DescriptorSupport(new String[] {"bloo"}, new Object[] {v}); - - final String xml; - try { - xml = in.toXMLString(); - } catch (RuntimeOperationsException e) { - final Throwable cause = e.getCause(); - if (cause instanceof IllegalArgumentException) { - System.out.println("OK: " + what + ": got a " + - "RuntimeOperationsException wrapping " + - "an IllegalArgumentException: " + - cause.getMessage()); - } else { - final String causeString = - (cause == null) ? "null" : cause.getClass().getName(); - System.out.println("FAILED: " + what + ": got a " + - "RuntimeOperationException wrapping " + - causeString); - failed++; - } - continue; - } - - System.out.println("Encoded " + what + " as " + xml); - - final DescriptorSupport out; - try { - out = new DescriptorSupport(xml); - } catch (Exception e) { - System.out.println("FAILED: " + what + ": got an exception:"); - e.printStackTrace(System.out); - failed++; - continue; - } - - final String[] names = out.getFieldNames(); - if (names.length != 1 || !"bloo".equals(names[0])) { - System.out.println("FAILED: decoded names wrong: " + - Arrays.asList(names)); - failed++; - continue; - } - - final Object[] values = out.getFieldValues(names); - if (values.length != 1) { - System.out.println("FAILED: wrong number of values: " + - Arrays.asList(values)); - failed++; - continue; - } - - final Object outValue = values[0]; - - if (v == null) { - if (outValue == null) - System.out.println("OK: decoded null value"); - else { - System.out.println("FAILED: decoded null value as " + - outValue.getClass().getName() + "{" + - outValue + "}"); - failed++; - } - continue; - } - - if (outValue == null) { - System.out.println("FAILED: decoded non-null value as null"); - failed++; - continue; - } - - if (v.getClass() != outValue.getClass()) { - System.out.println("FAILED: decoded value has class " + - outValue.getClass().getName() + "{" + - outValue + "}"); - failed++; - continue; - } - - if (v.equals(outValue)) - System.out.println("OK: decoded value is equal to original"); - else { - System.out.println("FAILED: decoded value is different: {" + - outValue + "}"); - failed++; - } - } - - if (failed == 0) - System.out.println("OK: all tests passed"); - else { - System.out.println("TEST FAILED: fail count: " + failed); - System.exit(1); - } - } -} diff --git a/test/jdk/javax/management/modelmbean/LoggingExceptionTest.java b/test/jdk/javax/management/modelmbean/LoggingExceptionTest.java index 94dfee092eafd..c47e67b6afb3f 100644 --- a/test/jdk/javax/management/modelmbean/LoggingExceptionTest.java +++ b/test/jdk/javax/management/modelmbean/LoggingExceptionTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2015, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2008, 2025, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -47,7 +47,6 @@ public class LoggingExceptionTest { private static final String tests[] = new String[] { "DescriptorSupport()", "DescriptorSupport(int)", - "DescriptorSupport(String)", "DescriptorSupport(String...)", "DescriptorSupport(String[], Object[])", "DescriptorSupport(DescriptorSupport)", @@ -72,18 +71,15 @@ public static void main(String[] args) { ds = new DescriptorSupport(10); break; case 2: - ds = new DescriptorSupport(new DescriptorSupport().toXMLString()); - break; - case 3: ds = new DescriptorSupport("name1=value1", "name2=value2"); break; - case 4: + case 3: ds = new DescriptorSupport(new String[] {"name"}, new Object[] {"value"}); break; - case 5: + case 4: ds = new DescriptorSupport(new DescriptorSupport()); break; - case 6: + case 5: RequiredModelMBean mbean = new RequiredModelMBean(); NotificationListener nl = new NotificationListener() { public void handleNotification(Notification notification,