View Javadoc

1   /*
2    * ASTASInterfaceType.java
3    * 
4    * Copyright (c) 2006-2007 David Holroyd
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package uk.co.badgersinfoil.metaas.impl;
20  
21  import java.util.Collections;
22  import java.util.LinkedList;
23  import java.util.List;
24  import org.asdt.core.internal.antlr.AS3Parser;
25  import uk.co.badgersinfoil.metaas.dom.ASInterfaceType;
26  import uk.co.badgersinfoil.metaas.dom.ASMethod;
27  import uk.co.badgersinfoil.metaas.dom.Visibility;
28  import uk.co.badgersinfoil.metaas.impl.antlr.LinkedListToken;
29  import uk.co.badgersinfoil.metaas.impl.antlr.LinkedListTree;
30  
31  
32  public class ASTASInterfaceType extends ASTASType implements ASInterfaceType {
33  
34  	private static final int EXTENDS_INDEX = 3;
35  
36  	public ASTASInterfaceType(LinkedListTree ast) {
37  		super(ast);
38  	}
39  
40  	public List getSuperInterfaces() {
41  		List results = new LinkedList();
42  		LinkedListTree impls = ASTUtils.findChildByType(ast, AS3Parser.EXTENDS);
43  		if (impls != null) {
44  			for (ASTIterator i=new ASTIterator(impls); i.hasNext(); ) {
45  				results.add(ASTUtils.identText(i.next()));
46  			}
47  		}
48  		return Collections.unmodifiableList(results);
49  	}
50  
51  	public void addSuperInterface(String interfaceName) {
52  		LinkedListTree iface = AS3FragmentParser.parseIdent(interfaceName);
53  		LinkedListTree ext = ASTUtils.findChildByType(ast, AS3Parser.EXTENDS);
54  		if (ext == null) {
55  			ext = ASTUtils.newAST(AS3Parser.EXTENDS, "extends");
56  			ext.appendToken(TokenBuilder.newSpace());
57  			// hack a space in at the right point,
58  			LinkedListToken sp = TokenBuilder.newSpace();
59  			ext.getStartToken().beforeInsert(sp);
60  			ext.setStartToken(sp);
61  			ast.addChildWithTokens(EXTENDS_INDEX, ext);
62  		} else {
63  			ext.appendToken(TokenBuilder.newComma());
64  		}
65  		ext.appendToken(TokenBuilder.newSpace());
66  		ext.addChildWithTokens(iface);
67  	}
68  
69  	public void removeSuperInterface(String interfaceName) {
70  		LinkedListTree impls = ASTUtils.findChildByType(ast, AS3Parser.EXTENDS);
71  		int count = 0;
72  		for (ASTIterator i=new ASTIterator(impls); i.hasNext(); ) {
73  			LinkedListTree iface = i.next();
74  			String name = ASTUtils.identText(iface);
75  			if (name.equals(interfaceName)) {
76  				if (i.hasNext()) {
77  					ASTUtils.removeTrailingWhitespaceAndComma(iface.getStopToken());
78  				} else if (count == 0) {
79  					// no interfaces left, so remove the
80  					// 'implements' clause completely,
81  					ast.deleteChild(ast.getIndexOfChild(impls));
82  					break;
83  				}
84  				i.remove();
85  				if (i.hasNext()) {
86  					count++;
87  				}
88  				break;
89  			}
90  			count++;
91  		}
92  	}
93  
94  	public ASMethod newMethod(String name, Visibility visibility, String returnType) {
95  		ASTASMethod meth = ASTBuilder.newInterfaceMethod(name, visibility, returnType);
96  		addMethod(meth);
97  		return meth;
98  	}
99  
100 	public void addMethod(ASTASMethod meth) {
101 		ASTUtils.addChildWithIndentation(findTypeBlock(), meth.getAST());
102 	}
103 }