Example #1
0
        /// <summary>
        /// Set Value to Variable
        /// </summary>
        public static MethodeCall CallMethode(this CodeBlock inParentBlock, string inName, params CodeBlock[] inParamEntries)
        {
            var tmpData = new MethodeCall()
            {
                Name      = inName,
                Parameter = inParamEntries.ToList()
            };

            inParentBlock.CodeEntries.Add(tmpData);
            return(tmpData);
        }
Example #2
0
        private MethodeCall HandleMethodeCall(MethodCallContext inMethodeCallContext)
        {
            var tmpMethodeCall = new MethodeCall()
            {
                Name = (inMethodeCallContext.SUPER() ?? inMethodeCallContext.THIS() ?? inMethodeCallContext.IDENTIFIER()).GetText()
            };

            if (inMethodeCallContext.expressionList() != null)
            {
                foreach (ExpressionContext tmpExpression in inMethodeCallContext.expressionList().expression())
                {
                    //Handle expression Context for Methode Call Parameter
                    var tmpCodeParam = new CodeBlock();
                    HandleExpressionContext(tmpCodeParam, tmpExpression, null);
                    tmpMethodeCall.Parameter.Add(tmpCodeParam);
                }
            }

            return(tmpMethodeCall);
        }
Example #3
0
        public T CallMethode <T>(String methode, object[] parameters)
        {
            var call = new MethodeCall(methode, parameters, typeof(T).AssemblyQualifiedName);


            _outStream.WriteLine(SerializeToString(call));
            _outStream.WriteLine("SYNC");
            Console.WriteLine("[C#-Security] MethodeCall sent");

            //outStream.WriteLine("READY");

            string temp;
            string command = "";

            while ((temp = _inStream.ReadLine()) != null)
            {
                if (temp == "SYNC")
                {
                    break;
                }
                command = command + temp + "\n";
            }

            T r;

            if (command.Equals("NULL\n"))
            {
                r = default(T);
            }
            else if (command.Equals("FAIL\n") || !_pipeStream.IsConnected)
            {
                throw new SecurityException();
            }
            else
            {
                Console.WriteLine(command);
                r = SerializeFromString <T>(command);
            }

            return(r);
        }
Example #4
0
        /// <summary>
        /// Handling of an Expression Block
        /// </summary>
        /// <param name="inCodeBlock"></param>
        /// <param name="inBlockStatement"></param>
        public void HandleExpressionContext(CodeBlock inCodeBlock, ExpressionContext inBlockStatement, VariableDeclaration inVariable = null)
        {
            if (inBlockStatement == null)
            {
                return;
            }

            //if (inBlockStatement.IDENTIFIER() != null)
            //{
            //    inCodeBlock.CodeEntries.Add(new ConstantValue { Value = inBlockStatement.IDENTIFIER().GetText() });
            //}
            //else if (inBlockStatement.THIS() != null)
            //{
            //    throw new NotImplementedException("Not done yet");
            //}
            //else if (inBlockStatement.NEW() != null)
            //{
            //    throw new NotImplementedException("Not done yet");
            //}
            //else if (inBlockStatement.SUPER() != null)
            //{
            //    throw new NotImplementedException("Not done yet");
            //}
            //else if (inBlockStatement.INSTANCEOF() != null)
            //{
            //    throw new NotImplementedException("Not done yet");
            //}
            //else
            {
                if (inBlockStatement.primary() != null)
                {
                    //Primary Value analyse type
                    var tmpPrimary       = inBlockStatement.primary();
                    var tmpPrimaryAsText = tmpPrimary.GetText();

                    if (tmpPrimary.expression() != null)
                    {
                        var tmpCodeBlock = new CodeBlockContainer();
                        HandleExpressionContext(tmpCodeBlock.InnerBlock, tmpPrimary.expression(), inVariable);
                        inCodeBlock.CodeEntries.Add(tmpCodeBlock);
                    }
                    else if (tmpPrimary.literal() != null)
                    {
                        inCodeBlock.CodeEntries.Add(new ConstantValue {
                            Value = tmpPrimaryAsText
                        });
                    }
                    else if (tmpPrimary.typeTypeOrVoid() != null)
                    {
                        var tmpType  = JavaAntlrClassLoader.CreateTypeContainerFromType(tmpPrimary.typeTypeOrVoid());
                        var tmpConst = new ConstantValue {
                            Type = tmpType, Value = tmpPrimary.typeTypeOrVoid().typeType().GetText()
                        };
                        if (tmpPrimary.CLASS() != null)
                        {
                            var tmpVariableAccess = new VariableAccess();
                            tmpVariableAccess.Access = tmpConst;
                            tmpVariableAccess.Child  = new ConstantValue {
                                Value = "class"
                            };
                            inCodeBlock.CodeEntries.Add(tmpConst);
                        }
                        else
                        {
                            inCodeBlock.CodeEntries.Add(tmpConst);
                        }
                    }
                    else if (tmpPrimary.nonWildcardTypeArguments() != null)
                    {
                        throw new NotImplementedException("Not done yet");
                    }
                    else if (tmpPrimary.explicitGenericInvocationSuffix() != null)
                    {
                        throw new NotImplementedException("Not done yet");
                    }
                    else if (tmpPrimary.arguments() != null)
                    {
                        throw new NotImplementedException("Not done yet");
                    }
                    else
                    {
                        inCodeBlock.CodeEntries.Add(new ConstantValue {
                            Value = tmpPrimaryAsText
                        });
                    }
                }
                else if (inBlockStatement.expression().Length == 1 &&
                         inBlockStatement.typeType() != null)
                {
                    //Type Conversion
                    var tmpInfo      = inBlockStatement.expression();
                    var tmpConverter = new TypeConversion();
                    tmpConverter.PreconversionValue = new CodeBlock();
                    HandleExpressionContext(tmpConverter.PreconversionValue, tmpInfo[0]);

                    var tmpType = inBlockStatement.typeType();
                    tmpConverter.Type = JavaAntlrClassLoader.GetTypeContainer(tmpType);
                    inCodeBlock.CodeEntries.Add(tmpConverter);
                }
                else if (inBlockStatement.expression().Length == 2 &&
                         inBlockStatement.children[1].GetText() == "[")
                {
                    var tmpAccess    = new VariableAccess {
                    };
                    var tmpCodeBlock = new CodeBlock();
                    HandleExpressionContext(tmpCodeBlock, inBlockStatement.expression()[0], null);
                    tmpAccess.Access = tmpCodeBlock.CodeEntries[0];

                    tmpCodeBlock = new CodeBlock();
                    HandleExpressionContext(tmpCodeBlock, inBlockStatement.expression()[1], null);
                    tmpAccess.Child = new CodeBlockContainer {
                        InnerBlock = tmpCodeBlock
                    };
                    tmpAccess.IsArrayAccess = true;

                    inCodeBlock.CodeEntries.Add(tmpAccess);
                }
                else if (inBlockStatement.expression().Length == 2 &&
                         inBlockStatement.children[1].GetText() != "=")
                {
                    var tmpCodeExpression = new CodeExpression
                    {
                        Manipulator = JavaStaticInfo.GetManipulator(string.Join("", inBlockStatement.children
                                                                                .Where(inItem => inItem is ITerminalNode)
                                                                                .Select(inItem => inItem.GetText())))
                    };
                    var tmpCodeBlock = new CodeBlock();
                    HandleExpressionContext(tmpCodeBlock, inBlockStatement.expression()[0], inVariable);
                    tmpCodeExpression.SubClauseEntries.Add(tmpCodeBlock);
                    tmpCodeBlock = new CodeBlock();//Second Code Block
                    HandleExpressionContext(tmpCodeBlock, inBlockStatement.expression()[1], inVariable);
                    tmpCodeExpression.SubClauseEntries.Add(tmpCodeBlock);

                    inCodeBlock.CodeEntries.Add(tmpCodeExpression);
                }
                else
                {
                    var tmpChildList = inBlockStatement.children;
                    if (tmpChildList.Count > 2)
                    {
                        var tmpSecondChildText = tmpChildList[1].GetText();
                        if (tmpSecondChildText == "=")
                        {
                            //SetVariable with Value
                            var tmpVarSetter = new SetFieldWithValue();
                            HandleExpressionContext(tmpVarSetter.VariableToAccess, tmpChildList[0] as ExpressionContext, inVariable);
                            HandleExpressionContext(tmpVarSetter.ValueToSet, tmpChildList[2] as ExpressionContext, inVariable);
                            inCodeBlock.CodeEntries.Add(tmpVarSetter);
                        }
                        else if (JavaStaticInfo.VariableOperators.ContainsKey(tmpSecondChildText))
                        {
                            var tmpCodeExpression = new CodeExpression
                            {
                                Manipulator = JavaStaticInfo.GetManipulator(tmpSecondChildText)
                            };
                            var tmpCodeBlock = new CodeBlock();
                            HandleExpressionContext(tmpCodeBlock, tmpChildList[0] as ExpressionContext, inVariable);
                            tmpCodeExpression.SubClauseEntries.Add(tmpCodeBlock);
                            tmpCodeBlock = new CodeBlock();//Second Code Block
                            HandleExpressionContext(tmpCodeBlock, tmpChildList[2] as ExpressionContext, inVariable);
                            tmpCodeExpression.SubClauseEntries.Add(tmpCodeBlock);

                            inCodeBlock.CodeEntries.Add(tmpCodeExpression);
                        }
                        //Multi Part Property Access
                        else if (tmpSecondChildText == ".")
                        {
                            VariableAccess tmpParent = null;
                            for (var tmpI = 0; tmpI < tmpChildList.Count; tmpI += 2)
                            {
                                var tmpChild  = tmpChildList[tmpI];
                                var tmpAccess = new VariableAccess();
                                if (tmpChild is ExpressionContext)
                                {
                                    var tmpCodeBlock = new CodeBlock();
                                    HandleExpressionContext(tmpCodeBlock, tmpChild as ExpressionContext, null);
                                    tmpAccess.Access = tmpCodeBlock.CodeEntries[0];
                                }
                                else if (tmpChild is MethodCallContext)
                                {
                                    var tmpResult = HandleMethodeCall(tmpChild as MethodCallContext);
                                    tmpAccess.Access = tmpResult;
                                }
                                else if (tmpChild is TerminalNodeImpl)
                                {
                                    var tmpChildText = tmpChild.GetText();
                                    if (tmpChildText == ".")
                                    {
                                    }
                                    else if (RegexHelper.WordCheck.IsMatch(tmpChildText))
                                    {
                                        tmpAccess.Access = new ConstantValue(tmpChildText);
                                    }
                                    else
                                    {
                                        throw new NotImplementedException("Not done yet");
                                    }
                                }
                                else
                                {
                                    throw new NotImplementedException("Not done yet");
                                }
                                if (tmpParent != null)
                                {
                                    tmpParent.Child = tmpAccess;
                                }
                                else
                                {
                                    inCodeBlock.CodeEntries.Add(tmpAccess);
                                }
                                tmpParent = tmpAccess;
                            }
                        }
                        else if (tmpSecondChildText == "?")
                        {
                            //Implement Elvis
                            var tmpStatement = new StatementCode
                            {
                                StatementType       = StatementTypeEnum.Elvis,
                                StatementCodeBlocks = new List <CodeBlock>()
                            };
                            //Boolean query
                            var tmpCodeBlock = new CodeBlock();
                            HandleExpressionContext(tmpCodeBlock, tmpChildList[0] as ExpressionContext, inVariable);
                            tmpStatement.StatementCodeBlocks.Add(tmpCodeBlock);
                            //First Result
                            tmpCodeBlock = new CodeBlock();
                            HandleExpressionContext(tmpCodeBlock, tmpChildList[2] as ExpressionContext, inVariable);
                            tmpStatement.StatementCodeBlocks.Add(tmpCodeBlock);
                            //Second Result
                            tmpCodeBlock = new CodeBlock();
                            HandleExpressionContext(tmpCodeBlock, tmpChildList[4] as ExpressionContext, inVariable);
                            tmpStatement.StatementCodeBlocks.Add(tmpCodeBlock);

                            inCodeBlock.CodeEntries.Add(tmpStatement);
                        }
                        else
                        {
                            throw new NotImplementedException("Not done yet");
                        }
                    }
                    else if (tmpChildList.Count == 1)
                    {
                        var         tmpValue       = tmpChildList[0] as MethodCallContext;
                        MethodeCall tmpMethodeCall = HandleMethodeCall(tmpValue);
                        inCodeBlock.CodeEntries.Add(tmpMethodeCall);
                    }
                    else if (tmpChildList.Count == 2 &&
                             tmpChildList[1] is ExpressionContext)
                    {
                        if (tmpChildList[0].GetText() == "!")
                        {
                            var tmpCodeExpression = new CodeExpression
                            {
                                Manipulator = JavaStaticInfo.GetManipulator(tmpChildList[0].GetText())
                            };
                            tmpCodeExpression.SubClauseEntries = new List <CodeBlock> {
                                new CodeBlock()
                            };
                            HandleExpressionContext(tmpCodeExpression.SubClauseEntries[0], tmpChildList[1] as ExpressionContext, inVariable);
                            inCodeBlock.CodeEntries.Add(tmpCodeExpression);
                        }
                        else if (tmpChildList[0].GetText() != "-")
                        {
                            throw new NotImplementedException("Not done yet");
                        }
                        else
                        {
                            HandleExpressionContext(inCodeBlock, tmpChildList[1] as ExpressionContext, inVariable);
                            (inCodeBlock.CodeEntries.Last() as ConstantValue).Value = "-" + (inCodeBlock.CodeEntries.Last() as ConstantValue).Value;
                        }
                    }
                    else if (tmpChildList.Count == 2 &&
                             tmpChildList[0] is ExpressionContext)
                    {
                        if (tmpChildList[1].GetText() != "--" && tmpChildList[1].GetText() != "++")
                        {
                            throw new NotImplementedException("Not done yet");
                        }
                        var tmpCodeExpression = new CodeExpression
                        {
                            Manipulator = JavaStaticInfo.GetManipulator(tmpChildList[1].GetText())
                        };
                        tmpCodeExpression.SubClauseEntries = new List <CodeBlock> {
                            new CodeBlock()
                        };
                        HandleExpressionContext(tmpCodeExpression.SubClauseEntries[0], tmpChildList[0] as ExpressionContext, inVariable);
                        inCodeBlock.CodeEntries.Add(tmpCodeExpression);
                    }
                    else if (tmpChildList.Count == 2)
                    {
                        if (tmpChildList[0].GetText() != "new")
                        {
                            throw new NotImplementedException("Not done yet");
                        }
                        inCodeBlock.CodeEntries.Add(HandleCreatorContext(tmpChildList[1] as CreatorContext, inVariable));
                    }
                    else
                    {
                        throw new NotImplementedException("Not done yet");
                    }
                }
            }
        }