~ubuntu-branches/ubuntu/quantal/netbeans/quantal

« back to all changes in this revision

Viewing changes to scripting/php/srcmodel/src/org/netbeans/modules/php/model/impl/ClassMemberExpressionImpl.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 
3
 *
 
4
 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
 
5
 *
 
6
 * The contents of this file are subject to the terms of either the GNU
 
7
 * General Public License Version 2 only ("GPL") or the Common
 
8
 * Development and Distribution License("CDDL") (collectively, the
 
9
 * "License"). You may not use this file except in compliance with the
 
10
 * License. You can obtain a copy of the License at
 
11
 * http://www.netbeans.org/cddl-gplv2.html
 
12
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
 
13
 * specific language governing permissions and limitations under the
 
14
 * License.  When distributing the software, include this License Header
 
15
 * Notice in each file and include the License file at
 
16
 * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
 
17
 * particular file as subject to the "Classpath" exception as provided
 
18
 * by Sun in the GPL Version 2 section of the License file that
 
19
 * accompanied this code. If applicable, add the following below the
 
20
 * License Header, with the fields enclosed by brackets [] replaced by
 
21
 * your own identifying information:
 
22
 * "Portions Copyrighted [year] [name of copyright owner]"
 
23
 *
 
24
 * Contributor(s):
 
25
 *
 
26
 * The Original Software is NetBeans. The Initial Developer of the Original
 
27
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
28
 * Microsystems, Inc. All Rights Reserved.
 
29
 *
 
30
 * If you wish your version of this file to be governed by only the CDDL
 
31
 * or only the GPL Version 2, indicate your decision by adding
 
32
 * "[Contributor] elects to include this software in this distribution
 
33
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
 
34
 * single choice of license, a recipient has the option to distribute
 
35
 * your version of this file under either the CDDL, the GPL Version 2 or
 
36
 * to extend the choice of license to its licensees as provided above.
 
37
 * However, if you add GPL Version 2 code and therefore, elected the GPL
 
38
 * Version 2 license, then the option applies only if the new code is
 
39
 * made subject to such option by the copyright holder.
 
40
 */
 
41
package org.netbeans.modules.php.model.impl;
 
42
 
 
43
import java.util.LinkedList;
 
44
import java.util.List;
 
45
 
 
46
import org.netbeans.api.languages.ASTItem;
 
47
import org.netbeans.api.languages.ASTNode;
 
48
import org.netbeans.api.lexer.TokenSequence;
 
49
import org.netbeans.modules.php.model.CallExpression;
 
50
import org.netbeans.modules.php.model.ClassMemberExpression;
 
51
import org.netbeans.modules.php.model.Expression;
 
52
import org.netbeans.modules.php.model.IdentifierExpression;
 
53
import org.netbeans.modules.php.model.PhpModelVisitor;
 
54
import org.netbeans.modules.php.model.SourceElement;
 
55
import org.netbeans.modules.php.model.impl.factory.CallExpressionBuilder;
 
56
import org.netbeans.modules.php.model.impl.factory.CallExpressionFactory;
 
57
import org.netbeans.modules.php.model.impl.factory.ClassMemberBuilder;
 
58
import org.netbeans.modules.php.model.impl.factory.ExpressionFactory;
 
59
import org.netbeans.modules.php.model.impl.factory.IdentifierBuilder;
 
60
 
 
61
 
 
62
/**
 
63
 * @author ads
 
64
 *
 
65
 */
 
66
public class ClassMemberExpressionImpl extends SourceElementImpl implements
 
67
        ClassMemberExpression
 
68
{
 
69
 
 
70
    public ClassMemberExpressionImpl( SourceElement parent, ASTNode node, 
 
71
            ASTNode realNode, ASTNode memberAccessNode ,TokenSequence sequence ) 
 
72
    {
 
73
        super(parent, node, realNode, sequence);
 
74
        myMemberNode = memberAccessNode;
 
75
    }
 
76
 
 
77
    /* (non-Javadoc)
 
78
     * @see org.netbeans.modules.php.model.ClassMemberExpression#getExpression()
 
79
     */
 
80
    public Expression getExpression() {
 
81
        List<Expression> children = getChildren( Expression.class );
 
82
        for (Expression expression : children) {
 
83
            /*
 
84
             * No need in this check.
 
85
             * If expression is complex ( owner identidier is itself 
 
86
             * member access ) then owner has the same node as this class
 
87
             * ( and this is MemberAccessExpression type ).
 
88
             * If expression is simple then owner has IdentifierExpression
 
89
             * type. In both cases check below failed. 
 
90
            if ( expression == getOwnerIdentifier() ){
 
91
                continue;
 
92
            }*/
 
93
            assert expression instanceof SourceElementImpl;
 
94
            SourceElementImpl impl = (SourceElementImpl) expression;
 
95
            String nt = impl.getNarrowNode().getNT();
 
96
            if ( nt.equals( CallExpressionBuilder.MEMBER_EXPRESSION )) {
 
97
                return expression;
 
98
            }
 
99
        }
 
100
        return null;
 
101
    }
 
102
 
 
103
    /* (non-Javadoc)
 
104
     * @see org.netbeans.modules.php.model.MemberExpression#getCallExpression()
 
105
     */
 
106
    public CallExpression getCallExpression() {
 
107
        List<CallExpression> children = getChildren( CallExpression.class );
 
108
        for ( CallExpression expression : children ){
 
109
            assert expression instanceof SourceElementImpl;
 
110
            SourceElementImpl impl = (SourceElementImpl) expression;
 
111
            if ( impl.getNode() == getNode() ) {
 
112
                return (CallExpression)impl;
 
113
            }
 
114
        }
 
115
        return null;
 
116
    }
 
117
 
 
118
    /* (non-Javadoc)
 
119
     * @see org.netbeans.modules.php.model.MemberExpression#getOwnerIdentifier()
 
120
     */
 
121
    public IdentifierExpression getOwnerIdentifier() {
 
122
        if ( getCallExpression() != null ) {
 
123
            return null;
 
124
        }
 
125
        return getChild( IdentifierExpression.class );
 
126
    }
 
127
 
 
128
    /* (non-Javadoc)
 
129
     * @see org.netbeans.modules.php.model.SourceElement#getElementType()
 
130
     */
 
131
    public Class<? extends SourceElement> getElementType() {
 
132
        return ClassMemberExpression.class;
 
133
    }
 
134
 
 
135
    /* (non-Javadoc)
 
136
     * @see org.netbeans.modules.php.model.Acceptor#accept(org.netbeans.modules.php.model.PhpModelVisitor)
 
137
     */
 
138
    public void accept( PhpModelVisitor visitor ) {
 
139
        visitor.visit( this );
 
140
    }
 
141
    
 
142
    @Override
 
143
    protected List<SourceElement> createChildrenList()
 
144
    {
 
145
        ASTNode node = getNarrowNode();
 
146
        List<ASTItem> nodes = node.getChildren();
 
147
        ASTNode before = null;
 
148
        ASTNode beforeBefore = null;
 
149
        for (ASTItem item : nodes) {
 
150
            if ( !(item instanceof ASTNode ) ){
 
151
                continue;
 
152
            }
 
153
            ASTNode child = (ASTNode) item;
 
154
            if ( child != getMemberNode() ) {
 
155
                beforeBefore = before;
 
156
                before = child;
 
157
            }
 
158
            else {
 
159
                break;
 
160
            }
 
161
        }
 
162
        
 
163
        SourceElement memberExpression =  
 
164
            ClassMemberBuilder.getInstance().build( this , getMemberNode() ,
 
165
                    getMemberNode(), getTokenSequence() );
 
166
                        
 
167
        List<SourceElement> children = new LinkedList<SourceElement>();
 
168
        assert before != null;
 
169
        if ( before.getNT().equals( CallExpressionFactory.IDENTIFIER )) {
 
170
            // this is the case : $a[ $i ];
 
171
            SourceElement sourceElement = IdentifierBuilder.getInstance().
 
172
                build( this, before , before , getTokenSequence() );
 
173
            children.add( sourceElement );
 
174
        }
 
175
        else {
 
176
            assert before.getNT().equals( CallExpressionBuilder.MEMBER) ;
 
177
            if ( before.getNode( CallExpressionBuilder.ARGS) != null ) {
 
178
                SourceElement callExpression = null;
 
179
                assert beforeBefore!= null;
 
180
                if ( beforeBefore.getNT().equals(CallExpressionBuilder.MEMBER ) ) {
 
181
                    callExpression = new CallMemberExpressionImpl(
 
182
                        this , getNode() , getNarrowNode() , beforeBefore,
 
183
                        before , getTokenSequence()); 
 
184
                }
 
185
                else {
 
186
                    assert beforeBefore.getNT().equals( 
 
187
                            CallExpressionFactory.IDENTIFIER );
 
188
                    callExpression = new CallExpressionImpl( this , getNode() ,
 
189
                            getNarrowNode() , before , getTokenSequence() );
 
190
                }
 
191
                children.add( callExpression );
 
192
            }
 
193
            else if ( before.getNode( ExpressionFactory.EXPRESSION) != null ) {
 
194
                SourceElement arrayMember = new ArrayMemberExpressionImpl( this ,
 
195
                        getNode() , getNarrowNode() , before , getTokenSequence() );
 
196
                children.add( arrayMember );
 
197
            }
 
198
            else if ( before.getNode( CallExpressionBuilder.MEMBER_EXPRESSION) 
 
199
                    != null) 
 
200
            {
 
201
                /*
 
202
                 * 'before' could be method name or attribute name 
 
203
                 */
 
204
                SourceElement classMember = new ClassMemberExpressionImpl( this ,
 
205
                        getNode() , getNarrowNode() , before , getTokenSequence() );
 
206
                children.add( classMember );
 
207
            }
 
208
            else {
 
209
                assert false;
 
210
            }
 
211
        }
 
212
 
 
213
        children.add( memberExpression );
 
214
        return children;
 
215
    }
 
216
    
 
217
    private ASTNode getMemberNode() {
 
218
        return myMemberNode;
 
219
    }
 
220
 
 
221
    private ASTNode myMemberNode;
 
222
}