Package: MethodSearchEngine

MethodSearchEngine

nameinstructionbranchcomplexitylinemethod
MethodSearchEngine()
M: 8 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
isConstructor(AExplicitOperationDefinition)
M: 20 C: 0
0%
M: 6 C: 0
0%
M: 4 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%
searchMainMethods(IRunnableContext, Object[], int)
M: 358 C: 0
0%
M: 112 C: 0
0%
M: 57 C: 0
0%
M: 88 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*
2: * #%~
3: * org.overture.ide.debug
4: * %%
5: * Copyright (C) 2008 - 2014 Overture
6: * %%
7: * This program is free software: you can redistribute it and/or modify
8: * it under the terms of the GNU General Public License as
9: * published by the Free Software Foundation, either version 3 of the
10: * License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public
18: * License along with this program. If not, see
19: * <http://www.gnu.org/licenses/gpl-3.0.html>.
20: * #~%
21: */
22: package org.overture.ide.debug.ui.launchconfigurations;
23:
24: import java.util.Arrays;
25: import java.util.List;
26: import java.util.Vector;
27:
28: import org.eclipse.jface.operation.IRunnableContext;
29: import org.overture.ast.definitions.AClassClassDefinition;
30: import org.overture.ast.definitions.AExplicitFunctionDefinition;
31: import org.overture.ast.definitions.AExplicitOperationDefinition;
32: import org.overture.ast.modules.AModuleModules;
33: import org.overture.ast.node.INode;
34: import org.overture.ast.statements.ASubclassResponsibilityStm;
35: import org.overture.typechecker.assistant.ITypeCheckerAssistantFactory;
36: import org.overture.typechecker.assistant.TypeCheckerAssistantFactory;
37: import org.overture.typechecker.assistant.pattern.PatternListTC;
38:
39: public class MethodSearchEngine
40: {
41:         public final ITypeCheckerAssistantFactory assistantFactory = new TypeCheckerAssistantFactory();
42:         public final static int MAIN_ONLY = 1;
43:         public final static int EXPLICIT_FUNCTION = 2;
44:         public final static int EXPLICIT_OPERATION = 4;
45:         public final static int STATIC = 8;
46:         public final static int PUBLIC = 16;
47:         public static final int WORLD_CLASS = 32;
48:         public static final int RUN = 64;
49:
50:         public INode[] searchMainMethods(IRunnableContext context, Object[] nodes,
51:                         int constraints)
52:         {
53:•                boolean onlyMain = (constraints & MAIN_ONLY) == MAIN_ONLY;
54:•                boolean onlyExplicitFunction = (constraints & EXPLICIT_FUNCTION) == EXPLICIT_FUNCTION;
55:•                boolean onlyExplicitOperation = (constraints & EXPLICIT_OPERATION) == EXPLICIT_OPERATION;
56:•                boolean onlyStatic = (constraints & STATIC) == STATIC;
57:•                boolean onlyPublicAccess = (constraints & PUBLIC) == PUBLIC;
58:•                boolean onlyRun = (constraints & RUN) == RUN;
59:•                boolean onlyWorldClass = (constraints & WORLD_CLASS) == WORLD_CLASS;
60:
61:                 final String MAIN_NAME = "main";
62:                 final String RUN_NAME = "run";
63:                 final String WORLD_NAME = "world";
64:
65:                 List<INode> matched = new Vector<INode>();
66:
67:•                for (int i = 0; i < nodes.length; i++)
68:                 {
69:                         Object iAstNode = nodes[i];
70:                         boolean accept = false;
71:
72:•                        if (onlyExplicitOperation
73:•                                        && iAstNode instanceof AExplicitOperationDefinition)
74:                         {
75:                                 AExplicitOperationDefinition exop = (AExplicitOperationDefinition) iAstNode;
76:•                                if (isConstructor(exop))
77:                                 {
78:                                         continue;// is constructor
79:                                 }
80:
81:•                                if (onlyRun
82:•                                                && !exop.getName().getSimpleName().equalsIgnoreCase(RUN_NAME))
83:                                 {
84:                                         continue;
85:                                 }
86:
87:•                                if (onlyWorldClass
88:•                                                && (exop.getClassDefinition() == null || !exop.getClassDefinition().getName().getSimpleName().equalsIgnoreCase(WORLD_NAME)))
89:                                 {
90:                                         continue;
91:                                 }
92:
93:•                                if (onlyStatic
94:•                                                && !assistantFactory.createPAccessSpecifierAssistant().isStatic(exop.getAccess()))
95:                                 {
96:                                         continue;
97:                                 }
98:
99:•                                if (!assistantFactory.createPAccessSpecifierAssistant().isStatic(exop.getAccess())
100:•                                                && exop.getClassDefinition() != null)
101:                                 {
102:                                         // check for empty constructor
103:                                         boolean ok = false;
104:                                         boolean constructorFound = false;
105:•                                        for (Object def : exop.getClassDefinition().getDefinitions())
106:                                         {
107:•                                                if (def instanceof AExplicitOperationDefinition)
108:                                                 {
109:                                                         AExplicitOperationDefinition ctor = (AExplicitOperationDefinition) def;
110:•                                                        if (isConstructor(ctor))
111:                                                         {
112:                                                                 continue;
113:                                                         }
114:•                                                        if (ctor.getName().getSimpleName().equalsIgnoreCase(exop.getClassDefinition().getName().getSimpleName()))
115:                                                         {
116:                                                                 constructorFound = true;
117:                                                         }
118:•                                                        if (ctor.getBody() instanceof ASubclassResponsibilityStm)
119:                                                         {
120:                                                                 // abstract class instantiation impossible
121:                                                                 ok = false;
122:                                                                 break;
123:                                                         }
124:•                                                        if (ctor.getParameterPatterns() != null
125:•                                                                        && ctor.getParameterPatterns().size() > 0)
126:                                                         {
127:                                                                 ok = true;
128:                                                                 break;
129:                                                         }
130:                                                 }
131:
132:                                         }
133:•                                        if (!ok && constructorFound)
134:                                         {
135:                                                 continue;
136:                                         }
137:                                 }
138:•                                if (onlyMain
139:•                                                && !exop.getName().getSimpleName().toLowerCase().equals(MAIN_NAME))
140:                                 {
141:                                         continue;
142:                                 }
143:•                                if (onlyPublicAccess
144:•                                                && !assistantFactory.createPAccessSpecifierAssistant().isPublic(exop.getAccess()))
145:                                 {
146:                                         continue;
147:                                 }
148:
149:•                                if (exop.getParameterPatterns() != null
150:•                                                && exop.getParameterPatterns().size() > 0)
151:                                 {
152:                                         continue;
153:                                 }
154:                                 accept = true;
155:
156:                         }
157:•                        if (onlyExplicitFunction
158:•                                        && iAstNode instanceof AExplicitFunctionDefinition)
159:                         {
160:                                 AExplicitFunctionDefinition exfu = (AExplicitFunctionDefinition) iAstNode;
161:•                                if (onlyStatic
162:•                                                && !assistantFactory.createPAccessSpecifierAssistant().isStatic(exfu.getAccess()))
163:                                 {
164:                                         continue;
165:                                 }
166:
167:•                                if (onlyRun
168:•                                                && !exfu.getName().getSimpleName().equalsIgnoreCase(RUN_NAME))
169:                                 {
170:                                         continue;
171:                                 }
172:
173:•                                if (onlyWorldClass
174:•                                                && (exfu.getClassDefinition() == null || !exfu.getClassDefinition().getName().getSimpleName().equalsIgnoreCase(WORLD_NAME)))
175:                                 {
176:                                         continue;
177:                                 }
178:
179:•                                if (onlyMain
180:•                                                && !exfu.getName().getSimpleName().toLowerCase().equals(MAIN_NAME))
181:                                 {
182:                                         continue;
183:                                 }
184:•                                if (onlyPublicAccess
185:•                                                && !assistantFactory.createPAccessSpecifierAssistant().isPublic(exfu.getAccess()))
186:                                 {
187:                                         continue;
188:                                 }
189:
190:•                                if (exfu.getParamPatternList() != null
191:•                                                && exfu.getParamPatternList().size() > 0
192:•                                                && exfu.getParamPatternList().get(0) instanceof PatternListTC
193:•                                                && ((PatternListTC) exfu.getParamPatternList().get(0)).size() > 0)
194:                                 {
195:                                         continue;
196:                                 }
197:                                 accept = true;
198:
199:                         }
200:
201:•                        if (accept && iAstNode instanceof INode)
202:                         {
203:                                 matched.add((INode) iAstNode);
204:                         }
205:
206:•                        if (iAstNode instanceof AClassClassDefinition)
207:                         {
208:                                 Object[] elements = ((AClassClassDefinition) iAstNode).getDefinitions().toArray();
209:                                 matched.addAll(Arrays.asList(searchMainMethods(context, elements, constraints)));
210:                         }
211:•                        if (iAstNode instanceof AModuleModules)
212:                         {
213:                                 Object[] elements = ((AModuleModules) iAstNode).getDefs().toArray();
214:                                 matched.addAll(Arrays.asList(searchMainMethods(context, elements, constraints)));
215:                         }
216:                 }
217:                 return matched.toArray(new INode[matched.size()]);
218:         }
219:
220:         public static boolean isConstructor(AExplicitOperationDefinition op)
221:         {
222:•                if (op.getIsConstructor()
223:•                                || op.getClassDefinition() != null
224:•                                && op.getName().getSimpleName().equalsIgnoreCase(op.getClassDefinition().getLocation().getModule()))
225:                 {
226:                         return true;
227:                 }
228:                 return false;
229:         }
230: }