| 1 |
// Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima). |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package com.eprosima.idl.parser.tree; |
| 16 |
|
| 17 |
import java.util.ArrayList; |
| 18 |
import java.util.Map; |
| 19 |
import java.util.HashMap; |
| 20 |
|
| 21 |
import org.antlr.v4.runtime.Token; |
| 22 |
|
| 23 |
public class Interface extends ExportContainer implements Definition |
| 24 |
{ |
| 25 |
public Interface(String scopeFile, boolean isInScope, String scope, String name, Token tk) |
| 26 |
{ |
| 27 |
super(scopeFile, isInScope, scope, name, tk); |
| 28 |
|
| 29 |
m_bases = new HashMap<String, Interface>(); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
public void setParent(Object obj) |
| 34 |
{ |
| 35 |
m_parent = obj; |
| 36 |
} |
| 37 |
|
| 38 |
public Object getParent() |
| 39 |
{ |
| 40 |
return m_parent; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public boolean isIsModule() |
| 45 |
{ |
| 46 |
return false; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public boolean isIsInterface() |
| 51 |
{ |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
@Override |
| 56 |
public boolean isIsException() |
| 57 |
{ |
| 58 |
return false; |
| 59 |
} |
| 60 |
|
| 61 |
@Override |
| 62 |
public boolean isIsTypeDeclaration() |
| 63 |
{ |
| 64 |
return false; |
| 65 |
} |
| 66 |
|
| 67 |
@Override |
| 68 |
public boolean isIsConstDeclaration() |
| 69 |
{ |
| 70 |
return false; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public boolean isIsAnnotation() |
| 75 |
{ |
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
public boolean addBase(Interface interf) |
| 80 |
{ |
| 81 |
Interface prev = m_bases.put(interf.getName(), interf); |
| 82 |
|
| 83 |
if(prev != null) |
| 84 |
return false; |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
public ArrayList<Interface> getBases() |
| 90 |
{ |
| 91 |
return new ArrayList<Interface>(m_bases.values()); |
| 92 |
} |
| 93 |
|
| 94 |
/*! |
| 95 |
* @brief This function returns the exception defined inside the interface. |
| 96 |
*/ |
| 97 |
public Exception getException(String currentScope, String ename) |
| 98 |
{ |
| 99 |
com.eprosima.idl.parser.tree.Exception exception = null; |
| 100 |
|
| 101 |
for(int count = 0; exception == null && count < getExports().size(); ++count) |
| 102 |
{ |
| 103 |
int lastIndex = -1; |
| 104 |
|
| 105 |
if(getExports().get(count).isIsException()) |
| 106 |
{ |
| 107 |
String tmpname = ((com.eprosima.idl.parser.tree.Exception)getExports().get(count)).getScopedname(); |
| 108 |
|
| 109 |
if(tmpname.equals(ename)) |
| 110 |
{ |
| 111 |
exception = (com.eprosima.idl.parser.tree.Exception)getExports().get(count); |
| 112 |
} |
| 113 |
else |
| 114 |
{ |
| 115 |
// Probar si no tiene scope, con el scope actual. |
| 116 |
if(exception == null && ((lastIndex = ename.lastIndexOf("::")) == -1) && |
| 117 |
tmpname.equals(currentScope + ename)) |
| 118 |
{ |
| 119 |
exception = (com.eprosima.idl.parser.tree.Exception)getExports().get(count); |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
return exception; |
| 126 |
} |
| 127 |
|
| 128 |
/*! |
| 129 |
* @brief This function returns all operations of the interface. |
| 130 |
* This function is used in the string templates. |
| 131 |
*/ |
| 132 |
public ArrayList<Operation> getOperations() |
| 133 |
{ |
| 134 |
if(m_operations == null) |
| 135 |
{ |
| 136 |
m_operations = new ArrayList<Operation>(); |
| 137 |
|
| 138 |
// Get own operations. |
| 139 |
for(int count = 0; count < getExports().size(); ++count) |
| 140 |
{ |
| 141 |
if(getExports().get(count).isIsOperation()) |
| 142 |
{ |
| 143 |
m_operations.add((Operation)getExports().get(count)); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return m_operations; |
| 149 |
} |
| 150 |
|
| 151 |
public ArrayList<Operation> getAll_operations() |
| 152 |
{ |
| 153 |
if(m_all_operations == null) |
| 154 |
{ |
| 155 |
m_all_operations = new ArrayList<Operation>(); |
| 156 |
|
| 157 |
// Get parent operations. |
| 158 |
for(Interface iface : m_bases.values()) |
| 159 |
{ |
| 160 |
m_all_operations.addAll(iface.getAll_operations()); |
| 161 |
} |
| 162 |
|
| 163 |
// Get own operations. |
| 164 |
for(int count = 0; count < getExports().size(); ++count) |
| 165 |
{ |
| 166 |
if(getExports().get(count).isIsOperation()) |
| 167 |
{ |
| 168 |
m_all_operations.add((Operation)getExports().get(count)); |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
return m_all_operations; |
| 174 |
} |
| 175 |
|
| 176 |
/*! |
| 177 |
* @brief This function is used in stringtemplates to not generate module in some cases (Right now in generated (previous c) idl). |
| 178 |
*/ |
| 179 |
public boolean isThereAreDeclarations() |
| 180 |
{ |
| 181 |
boolean returnedValue = false; |
| 182 |
|
| 183 |
for(int count = 0; !returnedValue && count < getExports().size(); ++count) |
| 184 |
{ |
| 185 |
returnedValue = getExports().get(count).isIsTypeDeclaration() || |
| 186 |
getExports().get(count).isIsConstDeclaration() || getExports().get(count).isIsException(); |
| 187 |
} |
| 188 |
|
| 189 |
return returnedValue; |
| 190 |
} |
| 191 |
|
| 192 |
private Object m_parent = null; |
| 193 |
|
| 194 |
//! Contains all interfaces it inherits from. |
| 195 |
private Map<String, Interface> m_bases = null; |
| 196 |
//! Contains all operations. |
| 197 |
private ArrayList<Operation> m_operations = null; |
| 198 |
private ArrayList<Operation> m_all_operations = null; |
| 199 |
} |