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.MethodInvocationTree;
048: import com.sun.source.tree.MethodTree;
049: import com.sun.source.tree.ReturnTree;
050: import com.sun.source.tree.StatementTree;
051: import com.sun.source.tree.Tree;
052: import java.util.List;
053: import javax.lang.model.element.ExecutableElement;
054: import org.netbeans.api.java.source.CompilationInfo;
055: import org.netbeans.api.java.source.TreeMaker;
056: import org.netbeans.api.java.source.WorkingCopy;
057:
058: /**
059: *
060: * @author jdeva
061: */
062: public class DelegatorMethod extends Method {
063: private String delegateName;
064:
065: /** Creates a new instance of DelegatorMethod */
066: public DelegatorMethod(ExecutableElement element,
067: JavaClass javaClass) {
068: super (element, javaClass);
069: }
070:
071: /*
072: * Returns the name of the method to which this method delegates
073: * Ex: foo() { fooImpl();}, returns 'fooImpl'
074: */
075: public String getDelegateName() {
076: if (delegateName != null) {
077: return delegateName;
078: }
079: return (String) ReadTaskWrapper.execute(
080: new ReadTaskWrapper.Read() {
081: public Object run(CompilationInfo cinfo) {
082: MethodInvocationTree methInvk = getDelegate(cinfo);
083: if (methInvk != null) {
084: delegateName = ((IdentifierTree) methInvk
085: .getMethodSelect()).getName()
086: .toString();
087: }
088: return delegateName;
089: }
090: }, javaClass.getFileObject());
091: }
092:
093: /*
094: * Renames the name of the method to which this method delegates
095: * Ex: foo() { fooImpl();} fooImpl is renamed
096: */
097: public void setDelegateName(final String name) {
098: WriteTaskWrapper.execute(new WriteTaskWrapper.Write() {
099: public Object run(WorkingCopy wc) {
100: MethodInvocationTree methInvk = getDelegate(wc);
101: if (methInvk != null) {
102: IdentifierTree oldTree = (IdentifierTree) methInvk
103: .getMethodSelect();
104: IdentifierTree newTree = wc.getTreeMaker()
105: .Identifier(name);
106: wc.rewrite(oldTree, newTree);
107: }
108: return null;
109: }
110: }, javaClass.getFileObject());
111: delegateName = name;
112: }
113:
114: /*
115: * Adds a delegate statement to this method
116: *
117: */
118: public Statement addDelegateStatement(final String methodName,
119: final String[] pNames, final boolean noreturn) {
120: WriteTaskWrapper.execute(new WriteTaskWrapper.Write() {
121: public Object run(WorkingCopy wc) {
122: TreeMaker make = wc.getTreeMaker();
123: ExecutableElement elem = execElementHandle.resolve(wc);//javaClass.getMethod(wc, "_init", new Class[]{});
124: MethodInvocationTree methInvkTree = TreeMakerUtils
125: .createMethodInvocation(wc, methodName, pNames);
126: if (noreturn) {
127: addMethodInvocationStatement(wc, wc.getTrees()
128: .getTree(elem), methInvkTree);
129: } else {
130: addReturnStatement(wc, wc.getTrees().getTree(elem),
131: methInvkTree);
132: }
133: return null;
134: }
135: }, javaClass.getFileObject());
136: return null;
137: }
138:
139: private MethodInvocationTree getDelegate(CompilationInfo cinfo) {
140: ExecutableElement execElement = execElementHandle
141: .resolve(cinfo);
142: if (execElement != null) {
143: MethodTree mTree = cinfo.getTrees().getTree(execElement);
144: List<? extends StatementTree> stmts = mTree.getBody()
145: .getStatements();
146: if (stmts.size() == 1) {
147: StatementTree stmt = stmts.get(0);
148: ExpressionTree expr = null;
149: if (stmt.getKind() == Tree.Kind.RETURN) {
150: expr = ((ReturnTree) stmt).getExpression();
151: } else if (stmt.getKind() == Tree.Kind.EXPRESSION_STATEMENT) {
152: expr = ((ExpressionStatementTree) stmt)
153: .getExpression();
154: }
155: if (expr != null
156: && expr.getKind() == Tree.Kind.METHOD_INVOCATION) {
157: MethodInvocationTree methInvk = (MethodInvocationTree) expr;
158: if (methInvk.getArguments().size() == mTree
159: .getParameters().size()
160: && methInvk.getMethodSelect().getKind() == Tree.Kind.IDENTIFIER) {
161: return methInvk;
162: }
163: }
164: }
165: }
166: return null;
167: }
168: }
|