001package eu.cqse.check.framework.util.clike;
002
003import java.util.List;
004
005import eu.cqse.check.framework.scanner.ETokenType;
006import eu.cqse.check.framework.scanner.IToken;
007import eu.cqse.check.framework.shallowparser.framework.ShallowEntity;
008
009/** Utility methods for analyzing C-like code */
010public class CLikeCheckUtils {
011        /**
012         * Returns the modifiers and the return type of a method.
013         */
014        public static List<IToken> getMethodModifiersAndReturnType(ShallowEntity method) {
015                List<IToken> methodDefinition = method.includedTokens();
016
017                IToken lParen = methodDefinition.stream().filter(token -> ETokenType.LPAREN == token.getType()).findFirst()
018                                .orElse(null);
019                int parenthesisIndex = methodDefinition.indexOf(lParen);
020                int methodNameIndex = parenthesisIndex - 1;
021
022                return methodDefinition.subList(0, methodNameIndex);
023        }
024
025        /**
026         * Returns the arguments of a method
027         */
028        public static List<IToken> getMethodArguments(ShallowEntity method) {
029                List<IToken> methodDefinition = method.includedTokens();
030
031                IToken lParen = methodDefinition.stream().filter(token -> ETokenType.LPAREN == token.getType()).findFirst()
032                                .orElse(null);
033                IToken rParen = methodDefinition.stream().filter(token -> ETokenType.RPAREN == token.getType()).findFirst()
034                                .orElse(null);
035
036                return methodDefinition.subList(methodDefinition.indexOf(lParen) + 1, methodDefinition.indexOf(rParen));
037        }
038}