View | Details | Raw Unified | Return to bug 325131 | Differences between
and this patch

Collapse All | Expand All

(-)dom/org/eclipse/jdt/core/dom/ASTNode.java (-6 / +8 lines)
Lines 966-974 Link Here
966
	/**
966
	/**
967
	 * Primary field used in representing node properties efficiently.
967
	 * Primary field used in representing node properties efficiently.
968
	 * If <code>null</code>, this node has no properties.
968
	 * If <code>null</code>, this node has no properties.
969
	 * If a <code>String</code>, this is the name of this node's sole property,
969
	 * If a {@link String}, this is the name of this node's sole property,
970
	 * and <code>property2</code> contains its value.
970
	 * and <code>property2</code> contains its value.
971
	 * If a <code>HashMap</code>, this is the table of property name-value
971
	 * If a {@link Map}, this is the table of property name-value
972
	 * mappings; <code>property2</code>, if non-null is its unmodifiable
972
	 * mappings; <code>property2</code>, if non-null is its unmodifiable
973
	 * equivalent.
973
	 * equivalent.
974
	 * Initially <code>null</code>.
974
	 * Initially <code>null</code>.
Lines 2079-2085 Link Here
2079
	}
2079
	}
2080
2080
2081
	/**
2081
	/**
2082
	 * Returns the named property of this node, or <code>null</code> if none.
2082
	 * Returns the value of the named property of this node, or <code>null</code> if none.
2083
	 *
2083
	 *
2084
	 * @param propertyName the property name
2084
	 * @param propertyName the property name
2085
	 * @return the property value, or <code>null</code> if none
2085
	 * @return the property value, or <code>null</code> if none
Lines 2124-2129 Link Here
2124
	 * @param propertyName the property name
2124
	 * @param propertyName the property name
2125
	 * @param data the new property value, or <code>null</code> if none
2125
	 * @param data the new property value, or <code>null</code> if none
2126
	 * @see #getProperty(String)
2126
	 * @see #getProperty(String)
2127
	 * @throws IllegalArgumentException if the given property name is <code>null</code>
2127
	 */
2128
	 */
2128
	public final void setProperty(String propertyName, Object data) {
2129
	public final void setProperty(String propertyName, Object data) {
2129
		if (propertyName == null) {
2130
		if (propertyName == null) {
Lines 2147-2157 Link Here
2147
			// node has only a single property
2148
			// node has only a single property
2148
			if (propertyName.equals(this.property1)) {
2149
			if (propertyName.equals(this.property1)) {
2149
				// we're in luck
2150
				// we're in luck
2150
				this.property2 = data;
2151
				if (data == null) {
2151
				if (data == null) {
2152
					// just deleted last property
2152
					// just deleted last property
2153
					this.property1 = null;
2153
					this.property1 = null;
2154
					this.property2 = null;
2154
					this.property2 = null;
2155
				} else {
2156
					this.property2 = data;
2155
				}
2157
				}
2156
				return;
2158
				return;
2157
			}
2159
			}
Lines 2161-2167 Link Here
2161
			}
2163
			}
2162
			// node already has one property - getting its second
2164
			// node already has one property - getting its second
2163
			// convert to more flexible representation
2165
			// convert to more flexible representation
2164
			HashMap m = new HashMap(2);
2166
			Map m = new HashMap(3);
2165
			m.put(this.property1, this.property2);
2167
			m.put(this.property1, this.property2);
2166
			m.put(propertyName, data);
2168
			m.put(propertyName, data);
2167
			this.property1 = m;
2169
			this.property1 = m;
Lines 2170-2176 Link Here
2170
		}
2172
		}
2171
2173
2172
		// node has two or more properties
2174
		// node has two or more properties
2173
		HashMap m = (HashMap) this.property1;
2175
		Map m = (Map) this.property1;
2174
		if (data == null) {
2176
		if (data == null) {
2175
			m.remove(propertyName);
2177
			m.remove(propertyName);
2176
			// check for just one property left
2178
			// check for just one property left
(-)dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java (-1 / +132 lines)
Lines 10-15 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.core.dom.rewrite;
11
package org.eclipse.jdt.core.dom.rewrite;
12
12
13
import java.util.HashMap;
13
import java.util.Iterator;
14
import java.util.Iterator;
14
import java.util.List;
15
import java.util.List;
15
import java.util.Map;
16
import java.util.Map;
Lines 90-96 Link Here
90
 * @noextend This class is not intended to be subclassed by clients.
91
 * @noextend This class is not intended to be subclassed by clients.
91
 */
92
 */
92
public class ASTRewrite {
93
public class ASTRewrite {
93
94
	/** root node for the rewrite: Only nodes under this root are accepted */
94
	/** root node for the rewrite: Only nodes under this root are accepted */
95
	private final AST ast;
95
	private final AST ast;
96
96
Lines 103-108 Link Here
103
	 * @since 3.1
103
	 * @since 3.1
104
	 */
104
	 */
105
	private TargetSourceRangeComputer targetSourceRangeComputer = null;
105
	private TargetSourceRangeComputer targetSourceRangeComputer = null;
106
	
107
	/**
108
	 * Primary field used in representing rewrite properties efficiently.
109
	 * If <code>null</code>, this rewrite has no properties.
110
	 * If a {@link String}, this is the name of this rewrite's sole property,
111
	 * and <code>property2</code> contains its value.
112
	 * If a {@link Map}, this is the table of property name-value
113
	 * mappings.
114
	 * Initially <code>null</code>.
115
	 * 
116
	 * @see #property2
117
	 */
118
	private Object property1 = null;
119
120
	/**
121
	 * Auxiliary field used in representing rewrite properties efficiently.
122
	 *
123
	 * @see #property1
124
	 */
125
	private Object property2 = null;
106
126
107
	/**
127
	/**
108
	 * Creates a new instance for describing manipulations of
128
	 * Creates a new instance for describing manipulations of
Lines 495-500 Link Here
495
	}
515
	}
496
516
497
	/**
517
	/**
518
	 * Returns the value of the named property of this rewrite, or <code>null</code> if none.
519
	 *
520
	 * @param propertyName the property name
521
	 * @return the property value, or <code>null</code> if none
522
	 * @see #setProperty(String,Object)
523
	 * @throws IllegalArgumentException if the given property name is <code>null</code>
524
	 * @since 3.7
525
	 */
526
	public final Object getProperty(String propertyName) {
527
		if (propertyName == null) {
528
			throw new IllegalArgumentException();
529
		}
530
		if (this.property1 == null) {
531
			// rewrite has no properties at all
532
			return null;
533
		}
534
		if (this.property1 instanceof String) {
535
			// rewrite has only a single property
536
			if (propertyName.equals(this.property1)) {
537
				return this.property2;
538
			} else {
539
				return null;
540
			}
541
		}
542
		// otherwise rewrite has table of properties
543
		Map m = (Map) this.property1;
544
		return m.get(propertyName);
545
	}
546
	
547
	/**
498
	 * Returns an object that tracks the source range of the given node
548
	 * Returns an object that tracks the source range of the given node
499
	 * across the rewrite to its AST. Upon return, the result object reflects
549
	 * across the rewrite to its AST. Upon return, the result object reflects
500
	 * the given node's current source range in the AST. After
550
	 * the given node's current source range in the AST. After
Lines 674-679 Link Here
674
	}
724
	}
675
725
676
	/**
726
	/**
727
	 * Sets the named property of this rewrite to the given value,
728
	 * or to <code>null</code> to clear it.
729
	 * <p>
730
	 * Clients should employ property names that are sufficiently unique
731
	 * to avoid inadvertent conflicts with other clients that might also be
732
	 * setting properties on the same rewrite.
733
	 * </p>
734
	 * <p>
735
	 * Note that modifying a property is not considered a modification to the
736
	 * AST itself. This is to allow clients to decorate existing rewrites with
737
	 * their own properties without jeopardizing certain things (like the
738
	 * validity of bindings), which rely on the underlying tree remaining static.
739
	 * </p>
740
	 *
741
	 * @param propertyName the property name
742
	 * @param data the new property value, or <code>null</code> if none
743
	 * @see #getProperty(String)
744
	 * @throws IllegalArgumentException if the given property name is <code>null</code>
745
	 * @since 3.7
746
	 */
747
	public final void setProperty(String propertyName, Object data) {
748
		if (propertyName == null) {
749
			throw new IllegalArgumentException();
750
		}
751
		if (this.property1 == null) {
752
			// rewrite has no properties at all
753
			if (data == null) {
754
				// rewrite already knows this
755
				return;
756
			}
757
			// rewrite gets its fist property
758
			this.property1 = propertyName;
759
			this.property2 = data;
760
			return;
761
		}
762
		if (this.property1 instanceof String) {
763
			// rewrite has only a single property
764
			if (propertyName.equals(this.property1)) {
765
				// we're in luck
766
				if (data == null) {
767
					// just delete last property
768
					this.property1 = null;
769
					this.property2 = null;
770
				} else {
771
					this.property2 = data;
772
				}
773
				return;
774
			}
775
			if (data == null) {
776
				// we already know this
777
				return;
778
			}
779
			// rewrite already has one property - getting its second
780
			// convert to more flexible representation
781
			Map m = new HashMap(3);
782
			m.put(this.property1, this.property2);
783
			m.put(propertyName, data);
784
			this.property1 = m;
785
			this.property2 = null;
786
			return;
787
		}
788
		// rewrite has two or more properties
789
		Map m = (Map) this.property1;
790
		if (data == null) {
791
			m.remove(propertyName);
792
			// check for just one property left
793
			if (m.size() == 1) {
794
				// convert to more efficient representation
795
				Map.Entry[] entries = (Map.Entry[]) m.entrySet().toArray(new Map.Entry[1]);
796
				this.property1 = entries[0].getKey();
797
				this.property2 = entries[0].getValue();
798
			}
799
			return;
800
		} else {
801
			m.put(propertyName, data);
802
			// still has two or more properties
803
			return;
804
		}
805
	}
806
807
	/**
677
	 * Sets a custom target source range computer for this AST rewriter. This is advanced feature to modify how
808
	 * Sets a custom target source range computer for this AST rewriter. This is advanced feature to modify how
678
	 * comments are associated with nodes, which should be done only in special cases.
809
	 * comments are associated with nodes, which should be done only in special cases.
679
	 *
810
	 *
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritePropertyTest.java (+84 lines)
Added Link Here
1
/*******************************************************************************
2
 * Copyright (c) 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11
package org.eclipse.jdt.core.tests.rewrite.describing;
12
13
import junit.framework.Test;
14
import junit.framework.TestSuite;
15
16
import org.eclipse.jdt.core.ICompilationUnit;
17
import org.eclipse.jdt.core.IPackageFragment;
18
import org.eclipse.jdt.core.dom.CompilationUnit;
19
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
20
21
public class ASTRewritePropertyTest extends ASTRewritingTest {
22
23
	private static final Class THIS= ASTRewritePropertyTest.class;
24
25
	public ASTRewritePropertyTest(String name) {
26
		super(name);
27
	}
28
	public static Test allTests() {
29
		return new Suite(THIS);
30
	}
31
32
	public static Test setUpTest(Test someTest) {
33
		TestSuite suite= new Suite("one test");
34
		suite.addTest(someTest);
35
		return suite;
36
	}
37
38
	public static Test suite() {
39
		return buildModelTestSuite(THIS);
40
	}
41
42
	public void testProperties() throws Exception {
43
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
44
		StringBuffer buf= new StringBuffer();
45
		buf.append("package test1;\n");
46
		buf.append("public class C {}");
47
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
48
49
		CompilationUnit astRoot= createAST(cu);
50
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
51
		final String propertyName1 = "test.propertyName1";
52
		final String propertyName2 = "test.propertyName2";
53
		assertNull(rewrite.getProperty(propertyName1));
54
		try {
55
			rewrite.getProperty(null);
56
			assertTrue("Should not be reached", false);
57
		} catch(IllegalArgumentException e) {
58
			// ignore
59
		}
60
		rewrite.setProperty(propertyName1, "value");
61
		rewrite.setProperty(propertyName2, new Integer(1));
62
		try {
63
			rewrite.setProperty(null, "");
64
			assertTrue("Should not be reached", false);
65
		} catch(IllegalArgumentException e) {
66
			// ignore
67
		}
68
		Object value1 = rewrite.getProperty(propertyName1);
69
		assertTrue("Not a String", value1 instanceof String);
70
		assertTrue("Wrong value", "value".equals(value1));
71
		
72
		Object value2 = rewrite.getProperty(propertyName2);
73
		assertTrue("Not an Integer", value2 instanceof Integer);
74
		assertTrue("Wrong value", new Integer(1).equals(value2));
75
76
		rewrite.setProperty(propertyName1, null);
77
		value1 = rewrite.getProperty(propertyName1);
78
		assertNull("Not null", value1);
79
80
		rewrite.setProperty(propertyName2, null);
81
		value2 = rewrite.getProperty(propertyName2);
82
		assertNull("Not null", value2);
83
	}
84
}
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritingTest.java (-1 / +2 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2000, 2009 IBM Corporation and others.
2
 * Copyright (c) 2000, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 50-55 Link Here
50
		suite.addTest(ImportRewriteTest.allTests());
50
		suite.addTest(ImportRewriteTest.allTests());
51
		suite.addTest(LineCommentOffsetsTest.allTests());
51
		suite.addTest(LineCommentOffsetsTest.allTests());
52
		suite.addTest(ASTRewritingWithStatementsRecoveryTest.allTests());
52
		suite.addTest(ASTRewritingWithStatementsRecoveryTest.allTests());
53
		suite.addTest(ASTRewritePropertyTest.allTests());
53
		return suite;
54
		return suite;
54
	}
55
	}
55
56

Return to bug 325131