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-2006 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: package org.netbeans.modules.php.model.impl.refs.resolvers;
042:
043: import java.util.HashSet;
044: import java.util.List;
045: import java.util.Set;
046:
047: import org.netbeans.modules.php.model.ClassDefinition;
048: import org.netbeans.modules.php.model.ClassFunctionDeclaration;
049: import org.netbeans.modules.php.model.FunctionDefinition;
050: import org.netbeans.modules.php.model.InterfaceDefinition;
051: import org.netbeans.modules.php.model.Reference;
052: import org.netbeans.modules.php.model.SourceElement;
053:
054: /**
055: * @author ads
056: *
057: */
058: public class ClassFunctionDeclarationResolver extends
059: FunctionDefinitionResolver {
060:
061: /* (non-Javadoc)
062: * @see org.netbeans.modules.php.model.refs.ReferenceResolver#isApplicable(java.lang.Class)
063: */
064: public <T extends SourceElement> boolean isApplicable(Class<T> clazz) {
065: return ClassFunctionDeclaration.class.isAssignableFrom(clazz);
066: }
067:
068: protected <T extends SourceElement> void resolve(
069: SourceElement sourceElement, List<T> functions,
070: String funcName, Class<T> clazz, boolean exactComparison) {
071: SourceElement scope = sourceElement.getParent();
072:
073: if (InterfaceDefinition.class.isAssignableFrom(clazz)
074: && scope instanceof InterfaceDefinition) {
075: // avoid cyclic references to super class and interface
076: Set<String> set = new HashSet<String>();
077: findInInterface((InterfaceDefinition) scope, functions,
078: funcName, clazz, exactComparison, set);
079: } else {
080: super .resolve(sourceElement, functions, funcName, clazz,
081: exactComparison);
082: }
083: }
084:
085: protected <T extends SourceElement> void findInScope(
086: SourceElement scope, List<T> functions, String funcName,
087: Class<T> clazz, boolean exactComparison) {
088: List<ClassFunctionDeclaration> list = scope
089: .getChildren(ClassFunctionDeclaration.class);
090: for (ClassFunctionDeclaration decl : list) {
091: if (!clazz.isAssignableFrom(decl.getElementType())) {
092: continue;
093: }
094: String name = decl.getName();
095: if (!exactComparison && name.startsWith(funcName)) {
096: functions.add(clazz.cast(decl));
097: } else if (exactComparison && name.equals(funcName)) {
098: functions.add(clazz.cast(decl));
099: }
100: }
101: }
102:
103: protected <T extends SourceElement> void findInClass(
104: ClassDefinition scope, List<T> functions, String funcName,
105: Class<T> clazz, boolean exactComparison,
106: Set<String> classNames) {
107: super .findInClass(scope, functions, funcName, clazz,
108: exactComparison, classNames);
109:
110: if (exactComparison && functions.size() > 0) {
111: return;
112: }
113:
114: List<Reference<InterfaceDefinition>> ifaces = scope
115: .getImplementedInterfaces();
116: for (Reference<InterfaceDefinition> reference : ifaces) {
117: InterfaceDefinition iface = reference.get();
118: if (iface == null) {
119: continue;
120: }
121: findInInterface(iface, functions, funcName, clazz,
122: exactComparison, classNames);
123: if (exactComparison && functions.size() > 0) {
124: return;
125: }
126: }
127: }
128:
129: protected <T extends SourceElement> void findInList(
130: List<T> functions, String funcName, Class<T> clazz,
131: boolean exactComparison, List<FunctionDefinition> list) {
132: }
133:
134: private <T extends SourceElement> void findInInterface(
135: InterfaceDefinition scope, List<T> functions,
136: String funcName, Class<T> clazz, boolean exactComparison,
137: Set<String> classNames) {
138: String name = scope.getName();
139: if (classNames.contains(name)) {
140: return;
141: } else {
142: classNames.add(name);
143: }
144:
145: findInScope(scope.getBody(), functions, funcName, clazz,
146: exactComparison);
147:
148: if (exactComparison && functions.size() > 0) {
149: return;
150: }
151:
152: List<Reference<InterfaceDefinition>> ifaces = scope
153: .getSuperInterfaces();
154: for (Reference<InterfaceDefinition> reference : ifaces) {
155: InterfaceDefinition iface = reference.get();
156: if (iface == null) {
157: continue;
158: }
159: findInInterface(iface, functions, funcName, clazz,
160: exactComparison, classNames);
161: if (exactComparison && functions.size() > 0) {
162: return;
163: }
164: }
165: }
166: }
|