001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.visualweb.insync.java;
043:
044: import com.sun.source.tree.ExpressionStatementTree;
045: import com.sun.source.tree.ExpressionTree;
046: import com.sun.source.tree.IdentifierTree;
047: import com.sun.source.tree.MemberSelectTree;
048: import com.sun.source.tree.MethodInvocationTree;
049: import com.sun.source.tree.NewClassTree;
050: import com.sun.source.tree.StatementTree;
051: import com.sun.source.tree.Tree;
052: import com.sun.source.util.SourcePositions;
053: import javax.lang.model.element.TypeElement;
054: import org.netbeans.api.java.source.CompilationInfo;
055: import org.netbeans.api.java.source.TreeMaker;
056: import org.netbeans.api.java.source.TreePathHandle;
057: import org.netbeans.api.java.source.WorkingCopy;
058:
059: /**
060: *
061: * @author jdeva
062: */
063: public class Statement {
064: private TreePathHandle stmtTreePathHandle;
065: private Method method; //Enclosing method
066: private String beanName;
067: private String setterName;
068:
069: /** Creates a new instance of StatementClass */
070: public Statement(TreePathHandle stmtTreePathHandle, Method method,
071: String beanName, String setterName) {
072: this .stmtTreePathHandle = stmtTreePathHandle;
073: this .method = method;
074: this .beanName = beanName;
075: this .setterName = setterName;
076: }
077:
078: /*
079: * Returns bean name for this property set statement
080: */
081: public String getBeanName() {
082: return beanName;
083: }
084:
085: /*
086: * Returns bean name for this property set statement
087: */
088: public String getPropertySetterName() {
089: return setterName;
090: }
091:
092: /*
093: * Replaces the argument for property setter method invocation. Parses the passed in string
094: * to obtain the expression which is then replaces the old one
095: */
096: public void replaceArgument(final String valueSource) {
097: WriteTaskWrapper.execute(new WriteTaskWrapper.Write() {
098: public Object run(WorkingCopy wc) {
099: TreeMaker make = wc.getTreeMaker();
100: ExpressionTree arg = getArgument(wc);
101: SourcePositions[] positions = new SourcePositions[1];
102: ExpressionTree newArg = wc.getTreeUtilities()
103: .parseExpression(valueSource, positions);
104: wc.rewrite(arg, newArg);
105: return null;
106: }
107: }, method.getJavaClass().getFileObject());
108: }
109:
110: /*
111: * Evaluate the argument of property setter method
112: */
113: public Object evaluateArgument() {
114: return ReadTaskWrapper.execute(new ReadTaskWrapper.Read() {
115: public Object run(CompilationInfo cinfo) {
116: return ExpressionUtils.getValue(cinfo,
117: getArgument(cinfo));
118: }
119: }, method.getJavaClass().getFileObject());
120: }
121:
122: /*
123: * Returns the argument for the property set statement
124: */
125: private ExpressionTree getArgument(CompilationInfo cinfo) {
126: StatementTree stmtTree = (StatementTree) stmtTreePathHandle
127: .resolve(cinfo).getLeaf();
128: //StatementTree stmtTree = method.findPropertyStatement(cinfo, beanName, setterName);
129: if (stmtTree != null) {
130: return getMethodInvocationTree(cinfo, stmtTree)
131: .getArguments().get(0);
132: }
133: return null;
134: }
135:
136: /*
137: * Returns the eventset adapter class
138: */
139: public JavaClass getAdapterClass() {
140: return (JavaClass) ReadTaskWrapper.execute(
141: new ReadTaskWrapper.Read() {
142: public Object run(CompilationInfo cinfo) {
143: ExpressionTree arg = getArgument(cinfo);
144: if (arg.getKind() == Tree.Kind.NEW_CLASS) {
145: NewClassTree newClassTree = (NewClassTree) arg;
146: TypeElement typeElem = (TypeElement) TreeUtils
147: .getElement(cinfo, newClassTree
148: .getClassBody());
149: return new JavaClass(typeElem,
150: Statement.this .method
151: .getJavaClass()
152: .getFileObject());
153: }
154: return null;
155: }
156: }, method.getJavaClass().getFileObject());
157: }
158:
159: /*
160: * Returns the textual string of the argument of property setter method
161: */
162: public String getArgumentSource() {
163: return (String) ReadTaskWrapper.execute(
164: new ReadTaskWrapper.Read() {
165: public Object run(CompilationInfo cinfo) {
166: return ExpressionUtils.getArgumentSource(cinfo,
167: getArgument(cinfo));
168: }
169: }, method.getJavaClass().getFileObject());
170: }
171:
172: /*
173: * Returns true if the statement represents a property set statement
174: * For example - a.foo(arg);
175: */
176: public static boolean IsPropertySetter(CompilationInfo cinfo,
177: StatementTree stmtTree) {
178: if (stmtTree.getKind() == Tree.Kind.EXPRESSION_STATEMENT) {
179: ExpressionStatementTree exprStmtTree = (ExpressionStatementTree) stmtTree;
180: if (exprStmtTree.getExpression().getKind() == Tree.Kind.METHOD_INVOCATION) {
181: MethodInvocationTree methInvkTree = (MethodInvocationTree) exprStmtTree
182: .getExpression();
183: if (methInvkTree.getMethodSelect().getKind() == Tree.Kind.MEMBER_SELECT) {
184: MemberSelectTree memSelTree = (MemberSelectTree) methInvkTree
185: .getMethodSelect();
186: if (methInvkTree.getArguments().size() == 1
187: && memSelTree.getExpression().getKind() == Tree.Kind.IDENTIFIER) {
188: return true;
189: }
190: }
191: }
192: }
193: return false;
194: }
195:
196: /*
197: * Creates a Statement class provided a statement tree reprsenting property set statement and its
198: * enclosing Method
199: */
200: public static Statement createStatementClass(CompilationInfo cinfo,
201: StatementTree stmtTree, Method method) {
202: MethodInvocationTree methInvkTree = getMethodInvocationTree(
203: cinfo, stmtTree);
204: MemberSelectTree memSelTree = (MemberSelectTree) methInvkTree
205: .getMethodSelect();
206: String beanName = ((IdentifierTree) memSelTree.getExpression())
207: .getName().toString();
208: String setterName = memSelTree.getIdentifier().toString();
209: return new Statement(TreePathHandle.create(TreeUtils
210: .getTreePath(cinfo, stmtTree), cinfo), method,
211: beanName, setterName);
212: }
213:
214: /*
215: * Returns a method invocation tree.
216: * It assumes the passed in argument represents a bean property set statement
217: *
218: */
219: public static MethodInvocationTree getMethodInvocationTree(
220: CompilationInfo cinfo, StatementTree stmtTree) {
221: return (MethodInvocationTree) ((ExpressionStatementTree) stmtTree)
222: .getExpression();
223: }
224:
225: public boolean remove() {
226: Boolean result = (Boolean) WriteTaskWrapper.execute(
227: new WriteTaskWrapper.Write() {
228: public Object run(WorkingCopy wc) {
229: TreeMaker make = wc.getTreeMaker();
230: StatementTree stmtTree = (StatementTree) stmtTreePathHandle
231: .resolve(wc).getLeaf();
232: //StatementTree stmtTree = method.findPropertyStatement(wc, beanName, setterName);
233: return method.removeStatement(wc, stmtTree);
234: }
235: }, method.getJavaClass().getFileObject());
236: return result.booleanValue();
237: }
238:
239: //This class holds on to bean name because of bug #96387
240: //Until that bug is fixed, this is a workaround to fix #103122
241: public void setBeanName(String name) {
242: this.beanName = name;
243: }
244: }
|