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.BlockTree;
045: import com.sun.source.tree.ExpressionTree;
046: import com.sun.source.tree.LiteralTree;
047: import com.sun.source.tree.ReturnTree;
048: import com.sun.source.tree.StatementTree;
049: import java.util.List;
050: import javax.lang.model.element.ExecutableElement;
051: import org.netbeans.api.java.source.CompilationInfo;
052: import org.netbeans.api.java.source.WorkingCopy;
053: import com.sun.source.tree.Tree;
054: import com.sun.source.util.TreePath;
055:
056: /**
057: *
058: * @author jdeva
059: */
060: public class EventMethod extends Method {
061: private String delegateName;
062:
063: /** Creates a new instance of DelegatorMethod */
064: public EventMethod(ExecutableElement element, JavaClass javaClass) {
065: super (element, javaClass);
066: }
067:
068: /**
069: * Update the last null return statement with a string return statement
070: */
071: public void updateLastReturnStatement(final String newStr) {
072: WriteTaskWrapper.execute(new WriteTaskWrapper.Write() {
073: public Object run(WorkingCopy wc) {
074: ExecutableElement execElement = execElementHandle
075: .resolve(wc);
076: BlockTree block = wc.getTrees().getTree(execElement)
077: .getBody();
078: List<? extends StatementTree> stmts = block
079: .getStatements();
080: if (stmts.size() > 0) {
081: StatementTree stmt = stmts.get(stmts.size() - 1);
082: if (stmt.getKind() == Tree.Kind.RETURN) {
083: ReturnTree ret = (ReturnTree) stmt;
084: ExpressionTree expr = ret.getExpression();
085: if (expr.getKind() == Tree.Kind.NULL_LITERAL) {
086: Tree newExpr = wc.getTreeMaker().Literal(
087: newStr);
088: wc.rewrite(expr, newExpr);
089: }
090: }
091: }
092: return null;
093: }
094: }, javaClass.getFileObject());
095: }
096:
097: /**
098: * Update all return statements returning the old string literal with new
099: * string literal. Uses visitor class to achieve
100: */
101: public void updateReturnStrings(final String oldStr,
102: final String newStr) {
103: WriteTaskWrapper.execute(new WriteTaskWrapper.Write() {
104: public Object run(WorkingCopy wc) {
105: ExecutableElement execElement = execElementHandle
106: .resolve(wc);
107: BlockTree block = wc.getTrees().getTree(execElement)
108: .getBody();
109: TreePath treePath = TreeUtils.getTreePath(wc, block);
110: new Refactor.ReturnStatementLiteralRenamer(wc, oldStr,
111: newStr).scan(treePath, null);
112: return null;
113: }
114: }, javaClass.getFileObject());
115: }
116:
117: /**
118: * @return the string returned by the last return statement
119: */
120: public String getMethodReturn() {
121: return (String) ReadTaskWrapper.execute(
122: new ReadTaskWrapper.Read() {
123: public Object run(CompilationInfo cinfo) {
124: ExecutableElement execElement = execElementHandle
125: .resolve(cinfo);
126: BlockTree block = cinfo.getTrees().getTree(
127: execElement).getBody();
128: List<? extends StatementTree> stmts = block
129: .getStatements();
130: if (stmts.size() > 0) {
131: StatementTree stmt = stmts
132: .get(stmts.size() - 1);
133: if (stmt.getKind() == Tree.Kind.RETURN) {
134: ReturnTree ret = (ReturnTree) stmt;
135: ExpressionTree expr = ret
136: .getExpression();
137: if (expr.getKind() == Tree.Kind.STRING_LITERAL) {
138: return ((LiteralTree) expr)
139: .getValue();
140: }
141: }
142: }
143: return null;
144: }
145: }, javaClass.getFileObject());
146: }
147: }
|