View | Details | Raw Unified | Return to bug 228894
Collapse All | Expand All

(-)a/api.debugger.jpda/apichanges.xml (+22 lines)
 Lines 835-840    Link Here 
835
        <issue number="79027"/>
835
        <issue number="79027"/>
836
    </change>
836
    </change>
837
837
838
    <change>
839
        <api name="JPDADebuggerAPI"/>
840
        <summary>A possibility to get/set mirror objects from debugger variables.</summary>
841
        <version major="2" minor="44"/>
842
        <date day="30" month="4" year="2013"/>
843
        <author login="mentlicher"/>
844
        <compatibility addition="yes" source="compatible" binary="compatible"/>
845
        <description>
846
            <code>MutableVariable</code> interface is introduced. It's to be implemented by
847
            variables that can change their values. <code>Field</code>,
848
            <code>LocalVariable</code> and <code>JPDAWatch</code> now extend
849
            <code>MutableVariable</code>.
850
            <p/>
851
            <code>Object createMirrorObject()</code> method is added to <code>Variable</code>
852
            class and <code>void setFromMirrorObject(Object obj)</code> method
853
            is declared by <code>MutableVariable</code>.
854
        </description>
855
        <class package="org.netbeans.api.debugger.jpda" name="Variable" />
856
        <class package="org.netbeans.api.debugger.jpda" name="MutableVariable" />
857
        <issue number="228894"/>
858
    </change>
859
838
860
839
</changes>
861
</changes>
840
862
(-)a/api.debugger.jpda/manifest.mf (-1 / +1 lines)
 Lines 1-6    Link Here 
1
Manifest-Version: 1.0
1
Manifest-Version: 1.0
2
OpenIDE-Module: org.netbeans.api.debugger.jpda/2
2
OpenIDE-Module: org.netbeans.api.debugger.jpda/2
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/debugger/jpda/Bundle.properties
3
OpenIDE-Module-Localizing-Bundle: org/netbeans/api/debugger/jpda/Bundle.properties
4
OpenIDE-Module-Specification-Version: 2.43
4
OpenIDE-Module-Specification-Version: 2.44
5
OpenIDE-Module-Package-Dependencies: com.sun.jdi[VirtualMachineManager]
5
OpenIDE-Module-Package-Dependencies: com.sun.jdi[VirtualMachineManager]
6
6
(-)a/api.debugger.jpda/src/org/netbeans/api/debugger/jpda/Field.java (-1 / +2 lines)
 Lines 58-64    Link Here 
58
 * @see ObjectVariable
58
 * @see ObjectVariable
59
 * @author   Jan Jancura
59
 * @author   Jan Jancura
60
 */
60
 */
61
public interface Field extends Variable {
61
public interface Field extends MutableVariable {
62
62
63
    /**
63
    /**
64
     * Declared name of field.
64
     * Declared name of field.
 Lines 100-105    Link Here 
100
     * @return sets value of this field represented as text
100
     * @return sets value of this field represented as text
101
     * @throws InvalidExpressionException if the expression is not correct
101
     * @throws InvalidExpressionException if the expression is not correct
102
     */
102
     */
103
    @Override
103
    public abstract void setValue (String value) 
104
    public abstract void setValue (String value) 
104
    throws InvalidExpressionException;
105
    throws InvalidExpressionException;
105
}
106
}
(-)a/api.debugger.jpda/src/org/netbeans/api/debugger/jpda/JPDAWatch.java (-1 / +2 lines)
 Lines 57-63    Link Here 
57
 * @author   Jan Jancura
57
 * @author   Jan Jancura
58
 */
58
 */
59
59
60
public interface JPDAWatch extends Variable {
60
public interface JPDAWatch extends MutableVariable {
61
61
62
    /**
62
    /**
63
     * Watched expression.
63
     * Watched expression.
 Lines 105-110    Link Here 
105
     *
105
     *
106
     * @param value a new value of this variable represented as text
106
     * @param value a new value of this variable represented as text
107
     */
107
     */
108
    @Override
108
    public abstract void setValue (String value) throws InvalidExpressionException;
109
    public abstract void setValue (String value) throws InvalidExpressionException;
109
110
110
    /**
111
    /**
(-)a/api.debugger.jpda/src/org/netbeans/api/debugger/jpda/LocalVariable.java (-1 / +2 lines)
 Lines 58-64    Link Here 
58
 * @see ObjectVariable
58
 * @see ObjectVariable
59
 * @author   Jan Jancura
59
 * @author   Jan Jancura
60
 */
60
 */
61
public interface LocalVariable extends Variable {
61
public interface LocalVariable extends MutableVariable {
62
62
63
    /**
63
    /**
64
     * Declared name of local.
64
     * Declared name of local.
 Lines 87-92    Link Here 
87
     * @param value a new value of this local represented as text
87
     * @param value a new value of this local represented as text
88
     * @throws InvalidExpressionException if the expression is not correct
88
     * @throws InvalidExpressionException if the expression is not correct
89
     */
89
     */
90
    @Override
90
    public abstract void setValue (String value) 
91
    public abstract void setValue (String value) 
91
    throws InvalidExpressionException;
92
    throws InvalidExpressionException;
92
}
93
}
(-)a/api.debugger.jpda/src/org/netbeans/api/debugger/jpda/MutableVariable.java (+81 lines)
Line 0    Link Here 
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.api.debugger.jpda;
43
44
import java.io.InvalidObjectException;
45
46
/**
47
 * Represents a variable that can be modified.
48
 *
49
 * <pre style="background-color: rgb(255, 255, 102);">
50
 * Since JDI interfaces evolve from one version to another, it's strongly recommended
51
 * not to implement this interface in client code. New methods can be added to
52
 * this interface at any time to keep up with the JDI functionality.</pre>
53
 *
54
 * @see LocalVariable
55
 * @see Field
56
 * @see JPDAWatch
57
 *
58
 * @author Martin Entlicher
59
 * @since 2.44
60
 */
61
public interface MutableVariable extends Variable {
62
    
63
    /**
64
     * Sets a value represented as text, to this variable.
65
     *
66
     * @param value The string value to be set to this variable
67
     * @throws InvalidExpressionException if the expression is not correct
68
     */
69
    void setValue (String value) throws InvalidExpressionException;
70
    
71
    /**
72
     * Set the value of this variable to match the given mirror object.
73
     * 
74
     * @param obj The mirror object
75
     * @throws InvalidObjectException when it was not possible to set value of
76
     *                                this variable from the provided object.
77
     * @see Variable#createMirrorObject()
78
     */
79
    void setFromMirrorObject(Object obj) throws InvalidObjectException;
80
    
81
}
(-)a/api.debugger.jpda/src/org/netbeans/api/debugger/jpda/Variable.java (+10 lines)
 Lines 77-80    Link Here 
77
     * @return text representation of current value of "this" variable
77
     * @return text representation of current value of "this" variable
78
     */
78
     */
79
    public abstract String getValue ();
79
    public abstract String getValue ();
80
81
    /**
82
     * Create an object in this JVM, which mirrors the value of this variable in
83
     * the target JVM.
84
     * 
85
     * @return The mirror object, or <code>null</code> when a mirror object can not be created.
86
     * @since 2.44
87
     */
88
    Object createMirrorObject();
89
    
80
}
90
}
(-)a/debugger.jpda/test/unit/src/org/netbeans/api/debugger/jpda/MirrorValuesTest.java (+183 lines)
Line 0    Link Here 
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.api.debugger.jpda;
43
44
import java.awt.Color;
45
import java.awt.Point;
46
import java.io.File;
47
import java.util.Arrays;
48
import java.util.Date;
49
import java.util.HashMap;
50
import java.util.Map;
51
import junit.framework.Test;
52
import org.netbeans.api.debugger.DebuggerManager;
53
import org.netbeans.junit.NbTestCase;
54
55
/**
56
 * Tests {@link Variable#createMirrorObject()} and {@link MutableVariable#setFromMirrorObject(java.lang.Object)}.
57
 * 
58
 * @author Martin Entlicher
59
 */
60
public class MirrorValuesTest extends NbTestCase {
61
    
62
    private static final String CLASS_NAME =
63
        "org.netbeans.api.debugger.jpda.testapps.MirrorValuesApp";
64
    
65
    private JPDASupport     support;
66
    private DebuggerManager dm = DebuggerManager.getDebuggerManager ();
67
68
    public MirrorValuesTest(String s) {
69
        super(s);
70
    }
71
    
72
    public static Test suite() {
73
        return JPDASupport.createTestSuite(MirrorValuesTest.class);
74
    }
75
    
76
    public void testMirrors() throws Exception {
77
        try {
78
            Utils.BreakPositions bp = Utils.getBreakPositions(System.getProperty ("test.dir.src") + 
79
                    "org/netbeans/api/debugger/jpda/testapps/MirrorValuesApp.java");
80
            LineBreakpoint lb = bp.getLineBreakpoints().get(0);
81
            dm.addBreakpoint (lb);
82
83
            support = JPDASupport.attach (CLASS_NAME);
84
85
            support.waitState (JPDADebugger.STATE_STOPPED);  // breakpoint hit
86
            
87
            CallStackFrame sf = support.getDebugger ().getCurrentCallStackFrame ();
88
            LocalVariable [] vars = sf.getLocalVariables ();
89
            Map<String, Variable> variablesByName = getVariablesByName(vars);
90
            Map<String, Object> mirrorsByName = getMirrorsByName();
91
            
92
            Variable v;
93
            Object m;
94
            
95
            // Test of createMirrorObject():
96
            for (String name : mirrorsByName.keySet()) {
97
                v = variablesByName.get(name);
98
                m = v.createMirrorObject();
99
                assertNotNull(name, m);
100
                Object mm = mirrorsByName.get(name);
101
                if (mm.getClass().isArray()) {
102
                    assertTrue(name+" is array", m.getClass().isArray());
103
                    assertTrue(name+" array "+arrayToString(mm)+
104
                               " does not equal to "+arrayToString(m),
105
                               compareArrays(mm, m));
106
                } else {
107
                    assertEquals(name, mm, m);
108
                }
109
            }
110
            
111
            // Test of setFromMirrorObject():
112
            v = variablesByName.get("boo");
113
            assertEquals("boo", "true", v.getValue());
114
            ((MutableVariable) v).setFromMirrorObject(Boolean.FALSE);
115
            assertEquals("boo", "false", v.getValue());
116
            
117
            v = variablesByName.get("i");
118
            assertEquals("i", "10000", v.getValue());
119
            ((MutableVariable) v).setFromMirrorObject(12345);
120
            assertEquals("i", "12345", v.getValue());
121
            
122
            v = variablesByName.get("color");
123
            assertEquals("color", "java.awt.Color[r=255,g=0,b=0]", ((ObjectVariable) v).getToStringValue());
124
            ((MutableVariable) v).setFromMirrorObject(Color.GREEN);
125
            assertEquals("color", "java.awt.Color[r=0,g=255,b=0]", ((ObjectVariable) v).getToStringValue());
126
127
        } finally {
128
            support.doFinish ();
129
        }
130
        
131
    }
132
    
133
    private static boolean compareArrays(Object arr1, Object arr2) {
134
        if (arr1 instanceof Object[]) {
135
            return Arrays.deepEquals((Object[]) arr1, (Object[]) arr2);
136
        } else if (arr1 instanceof int[]) {
137
            return Arrays.equals((int[]) arr1, (int[]) arr2);
138
        } else {
139
            throw new IllegalStateException(arr1+", "+arr2);
140
        }
141
    }
142
    
143
    private static String arrayToString(Object a) {
144
        if (a instanceof Object[]) {
145
            return Arrays.deepToString((Object[]) a);
146
        } else if (a instanceof int[]) {
147
            return Arrays.toString((int[]) a);
148
        } else {
149
            throw new IllegalStateException(a.toString());
150
        }
151
    }
152
    
153
    private static Map<String, Variable> getVariablesByName(LocalVariable[] vars) {
154
        Map<String, Variable> map = new HashMap<String, Variable>();
155
        for (LocalVariable lv : vars) {
156
            assertTrue("Not mutable", lv instanceof MutableVariable);
157
            map.put(lv.getName(), lv);
158
        }
159
        return map;
160
    }
161
    
162
    private static Map<String, Object> getMirrorsByName() {
163
        String[] names = { "boo", "b", "s", "i", "l",
164
                           "f", "d",
165
                           "iarr",
166
                           "darr",
167
                           "str", "date", "color",
168
                           "point", "file" };
169
        Object[] mirrors = { true, (byte) 5, (short) 512, 10000, Long.MAX_VALUE,
170
                             12.12f, 1e150,
171
                             new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
172
                             new double[][] { { 0.1, 0.2, 0.3 }, { 1.1, 1.2, 1.3 }, { 2.1, 2.2, 2.3 } },
173
                             "A String", new Date(1000000000l), Color.RED,
174
                             new Point(10, 10), new File("/tmp/Foo.txt") };
175
        
176
        Map<String, Object> map = new HashMap<String, Object>();
177
        assertEquals(names.length, mirrors.length);
178
        for (int i = 0; i < names.length; i++) {
179
            map.put(names[i], mirrors[i]);
180
        }
181
        return map;
182
    }
183
}
(-)a/debugger.jpda/test/unit/src/org/netbeans/api/debugger/jpda/testapps/MirrorValuesApp.java (+84 lines)
Line 0    Link Here 
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.api.debugger.jpda.testapps;
43
44
import java.awt.Color;
45
import java.awt.Point;
46
import java.io.File;
47
import java.util.Date;
48
49
/**
50
 * A test application for mirror values.
51
 * 
52
 * @author Martin Entlicher
53
 */
54
public class MirrorValuesApp {
55
    
56
    public static void main(String[] args) {
57
        MirrorValuesApp mva = new MirrorValuesApp();
58
        mva.mirrors();
59
    }
60
61
    private void mirrors() {
62
        boolean boo = true;
63
        byte b = 5;
64
        short s = 512;
65
        int i = 10000;
66
        long l = Long.MAX_VALUE;
67
        float f = 12.12f;
68
        double d = 1e150;
69
        
70
        int[] iarr = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
71
        double[][] darr = new double[][] { { 0.1, 0.2, 0.3 }, { 1.1, 1.2, 1.3 }, { 2.1, 2.2, 2.3 } };
72
        
73
        String str = "A String";
74
        
75
        Date date = new Date(1000000000l);
76
        Color color = Color.RED;
77
        Point point = new Point(10, 10);
78
        File file = new File("/tmp/Foo.txt");
79
        
80
        Color[][] colors = new Color[][] { { Color.WHITE, Color.BLACK }, { Color.YELLOW, Color.GRAY } };
81
        
82
        System.currentTimeMillis();             // LBREAKPOINT
83
    }
84
}

Return to bug 228894
By use of this website, you agree to the NetBeans Policies and Terms of Use. © 2014, Oracle Corporation and/or its affiliates. Sponsored by Oracle logo