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 |
* |