Method: ExecutableAnalysis(int, String)

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.utils;
23:
24: import org.overture.ast.analysis.AnalysisException;
25: import org.overture.ast.analysis.DepthFirstAnalysisAdaptor;
26: import org.overture.ast.definitions.SClassDefinition;
27: import org.overture.ast.expressions.PExp;
28: import org.overture.ast.modules.AModuleModules;
29: import org.overture.ast.node.INode;
30: import org.overture.ast.statements.PStm;
31:
32: public class ExecutableAnalysis extends DepthFirstAnalysisAdaptor
33: {
34:         private final int searchLine;
35:
36:         private String module = null;
37:
38:         private ExecutableAnalysis(int searchLine, String module)
39:         {
40:                 this.searchLine = searchLine;
41:                 this.module = module;
42:         }
43:
44:         public static boolean isExecutable(INode node, int line, boolean findModule)
45:         {
46:
47:                 String nodeModule = findModule ? searchForModule(node) : null;
48:
49:                 ExecutableAnalysis analysis = new ExecutableAnalysis(line, nodeModule);
50:
51:                 try
52:                 {
53:                         node.apply(analysis);
54:                 } catch (ExecutableAnalysisException e)
55:                 {
56:                         return e.isExecutable();
57:
58:                 } catch (AnalysisException e)
59:                 {
60:                         e.printStackTrace();
61:                         return false;
62:                 }
63:
64:                 return false;
65:         }
66:
67:         private static String searchForModule(INode node)
68:         {
69:
70:                 String nodeModule = null;
71:
72:                 SClassDefinition classDef = node.getAncestor(SClassDefinition.class);
73:
74:                 if (classDef != null)
75:                 {
76:                         nodeModule = classDef.getLocation().getModule();
77:                 } else
78:                 {
79:                         AModuleModules slModule = node.getAncestor(AModuleModules.class);
80:
81:                         if (slModule != null)
82:                         {
83:                                 nodeModule = slModule.getName().getName();
84:                         }
85:                 }
86:
87:                 return nodeModule;
88:         }
89:
90:         private boolean isValidModule(PStm node)
91:         {
92:
93:                 return module == null
94:                                 || node.getLocation().getStartLine() == searchLine
95:                                 && module.equals(node.getLocation().getModule());
96:         }
97:
98:         private boolean isValidModule(PExp node)
99:         {
100:
101:                 return module == null
102:                                 || node.getLocation().getStartLine() == searchLine
103:                                 && module.equals(node.getLocation().getModule());
104:         }
105:
106:         public void defaultInPStm(PStm node) throws ExecutableAnalysisException
107:         {
108:                 if (isValidModule(node))
109:                 {
110:                         throw new ExecutableAnalysisException(true);
111:                 }
112:         }
113:
114:         public void defaultInPExp(PExp node) throws ExecutableAnalysisException
115:         {
116:                 if (isValidModule(node))
117:                 {
118:                         throw new ExecutableAnalysisException(true);
119:                 }
120:         }
121: }