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

Collapse All | Expand All

(-)dom/org/eclipse/jdt/core/dom/rewrite/ASTRewrite.java (+136 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 103-108 Link Here
103
	 * @since 3.1
104
	 * @since 3.1
104
	 */
105
	 */
105
	private TargetSourceRangeComputer targetSourceRangeComputer = null;
106
	private TargetSourceRangeComputer targetSourceRangeComputer = null;
107
	
108
	/**
109
	 * Primary field used in representing rewrite properties efficiently.
110
	 * If <code>null</code>, this rewrite has no properties.
111
	 * If a <code>String</code>, this is the name of this rewrite's sole property,
112
	 * and <code>property2</code> contains its value.
113
	 * If a <code>Map</code>, this is the table of property name-value
114
	 * mappings; <code>property2</code>, if non-null is its unmodifiable
115
	 * equivalent.
116
	 * Initially <code>null</code>.
117
	 * 
118
	 * @see #property2
119
	 */
120
	private Object property1 = null;
121
122
	/**
123
	 * Auxiliary field used in representing rewrite properties efficiently.
124
	 *
125
	 * @see #property1
126
	 */
127
	private Object property2 = null;
106
128
107
	/**
129
	/**
108
	 * Creates a new instance for describing manipulations of
130
	 * Creates a new instance for describing manipulations of
Lines 702-705 Link Here
702
		}
724
		}
703
		return buf.toString();
725
		return buf.toString();
704
	}
726
	}
727
	
728
	/**
729
	 * Returns the value of the named property of this rewrite, or <code>null</code> if none.
730
	 *
731
	 * @param propertyName the property name
732
	 * @return the property value, or <code>null</code> if none
733
	 * @see #setProperty(String,Object)
734
	 * @throws IllegalArgumentException if the given property name is <code>null</code>
735
	 * @since 3.7
736
	 */
737
	public final Object getProperty(String propertyName) {
738
		if (propertyName == null) {
739
			throw new IllegalArgumentException();
740
		}
741
		if (this.property1 == null) {
742
			// rewrite has no properties at all
743
			return null;
744
		}
745
		if (this.property1 instanceof String) {
746
			// rewrite has only a single property
747
			if (propertyName.equals(this.property1)) {
748
				return this.property2;
749
			} else {
750
				return null;
751
			}
752
		}
753
		// otherwise rewrite has table of properties
754
		Map m = (Map) this.property1;
755
		return m.get(propertyName);
756
	}
757
	
758
	/**
759
	 * Sets the named property of this rewrite to the given value,
760
	 * or to <code>null</code> to clear it.
761
	 * <p>
762
	 * Clients should employ property names that are sufficiently unique
763
	 * to avoid inadvertent conflicts with other clients that might also be
764
	 * setting properties on the same rewrite.
765
	 * </p>
766
	 * <p>
767
	 * Note that modifying a property is not considered a modification to the
768
	 * AST itself. This is to allow clients to decorate existing rewrites with
769
	 * their own properties without jeopardizing certain things (like the
770
	 * validity of bindings), which rely on the underlying tree remaining static.
771
	 * </p>
772
	 *
773
	 * @param propertyName the property name
774
	 * @param data the new property value, or <code>null</code> if none
775
	 * @see #getProperty(String)
776
	 * @throws IllegalArgumentException if the given property name is <code>null</code>
777
	 * @since 3.7
778
	 */
779
	public final void setProperty(String propertyName, Object data) {
780
		if (propertyName == null) {
781
			throw new IllegalArgumentException();
782
		}
783
784
		if (this.property1 == null) {
785
			// rewrite has no properties at all
786
			if (data == null) {
787
				// rewrite already knows this
788
				return;
789
			}
790
			// rewrite gets its fist property
791
			this.property1 = propertyName;
792
			this.property2 = data;
793
			return;
794
		}
795
796
		if (this.property1 instanceof String) {
797
			// rewrite has only a single property
798
			if (propertyName.equals(this.property1)) {
799
				// we're in luck
800
				if (data == null) {
801
					// just delete last property
802
					this.property1 = null;
803
					this.property2 = null;
804
				} else {
805
					this.property2 = data;
806
				}
807
				return;
808
			}
809
			if (data == null) {
810
				// we already know this
811
				return;
812
			}
813
			// rewrite already has one property - getting its second
814
			// convert to more flexible representation
815
			Map m = new HashMap(3);
816
			m.put(this.property1, this.property2);
817
			m.put(propertyName, data);
818
			this.property1 = m;
819
			this.property2 = null;
820
			return;
821
		}
822
823
		// rewrite has two or more properties
824
		Map m = (Map) this.property1;
825
		if (data == null) {
826
			m.remove(propertyName);
827
			// check for just one property left
828
			if (m.size() == 1) {
829
				// convert to more efficient representation
830
				Map.Entry[] entries = (Map.Entry[]) m.entrySet().toArray(new Map.Entry[1]);
831
				this.property1 = entries[0].getKey();
832
				this.property2 = entries[0].getValue();
833
			}
834
			return;
835
		} else {
836
			m.put(propertyName, data);
837
			// still has two or more properties
838
			return;
839
		}
840
	}
705
}
841
}
(-)src/org/eclipse/jdt/core/tests/rewrite/describing/ASTRewritePropertyTest.java (+90 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
import junit.framework.Test;
13
import junit.framework.TestSuite;
14
15
import org.eclipse.jdt.core.ICompilationUnit;
16
import org.eclipse.jdt.core.IPackageFragment;
17
import org.eclipse.jdt.core.dom.CompilationUnit;
18
import org.eclipse.jdt.core.dom.rewrite.ASTRewrite;
19
20
public class ASTRewritePropertyTest extends ASTRewritingTest {
21
22
	private static final Class THIS= ASTRewritePropertyTest.class;
23
24
	public ASTRewritePropertyTest(String name) {
25
		super(name);
26
	}
27
	public static Test allTests() {
28
		return new Suite(THIS);
29
	}
30
31
	public static Test setUpTest(Test someTest) {
32
		TestSuite suite= new Suite("one test");
33
		suite.addTest(someTest);
34
		return suite;
35
	}
36
37
	public static Test suite() {
38
		return buildModelTestSuite(THIS);
39
	}
40
41
	public void testInsert1() throws Exception {
42
		IPackageFragment pack1= this.sourceFolder.createPackageFragment("test1", false, null);
43
		/* foo(): append a return statement */
44
		StringBuffer buf= new StringBuffer();
45
		buf.append("package test1;\n");
46
		buf.append("public class C {\n");
47
		buf.append("    public Object foo() {\n");
48
		buf.append("        if (this.equals(new Object())) {\n");
49
		buf.append("            toString();\n");
50
		buf.append("        }\n");
51
		buf.append("    }\n");
52
		buf.append("}\n");
53
		ICompilationUnit cu= pack1.createCompilationUnit("C.java", buf.toString(), false, null);
54
55
		CompilationUnit astRoot= createAST(cu);
56
		ASTRewrite rewrite= ASTRewrite.create(astRoot.getAST());
57
		final String propertyName1 = "test.propertyName1";
58
		final String propertyName2 = "test.propertyName2";
59
		assertNull(rewrite.getProperty(propertyName1));
60
		try {
61
			rewrite.getProperty(null);
62
			assertTrue("Should not be reached", false);
63
		} catch(IllegalArgumentException e) {
64
			// ignore
65
		}
66
		rewrite.setProperty(propertyName1, "value");
67
		rewrite.setProperty(propertyName2, new Integer(1));
68
		try {
69
			rewrite.setProperty(null, "");
70
			assertTrue("Should not be reached", false);
71
		} catch(IllegalArgumentException e) {
72
			// ignore
73
		}
74
		Object value1 = rewrite.getProperty(propertyName1);
75
		assertTrue("Not a String", value1 instanceof String);
76
		assertTrue("Wrong value", "value".equals(value1));
77
		
78
		Object value2 = rewrite.getProperty(propertyName2);
79
		assertTrue("Not an Integer", value2 instanceof Integer);
80
		assertTrue("Wrong value", new Integer(1).equals(value2));
81
		
82
		rewrite.setProperty(propertyName1, null);
83
		value1 = rewrite.getProperty(propertyName1);
84
		assertNull("Not null", value1);
85
86
		rewrite.setProperty(propertyName2, null);
87
		value2 = rewrite.getProperty(propertyName2);
88
		assertNull("Not null", value2);
89
	}
90
}
(-)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