Return-Path: X-Original-To: apmail-apex-dev-archive@minotaur.apache.org Delivered-To: apmail-apex-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BF67818240 for ; Mon, 21 Mar 2016 06:31:19 +0000 (UTC) Received: (qmail 95881 invoked by uid 500); 21 Mar 2016 06:31:19 -0000 Delivered-To: apmail-apex-dev-archive@apex.apache.org Received: (qmail 95812 invoked by uid 500); 21 Mar 2016 06:31:19 -0000 Mailing-List: contact dev-help@apex.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@apex.incubator.apache.org Delivered-To: mailing list dev@apex.incubator.apache.org Received: (qmail 95801 invoked by uid 99); 21 Mar 2016 06:31:19 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 Mar 2016 06:31:19 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 95283180495 for ; Mon, 21 Mar 2016 06:31:18 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -4.021 X-Spam-Level: X-Spam-Status: No, score=-4.021 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.001] autolearn=disabled Received: from mx2-lw-us.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id Bp-LwTV_S7bL for ; Mon, 21 Mar 2016 06:31:17 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx2-lw-us.apache.org (ASF Mail Server at mx2-lw-us.apache.org) with SMTP id 623B75F1D5 for ; Mon, 21 Mar 2016 06:31:17 +0000 (UTC) Received: (qmail 95797 invoked by uid 99); 21 Mar 2016 06:31:16 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 Mar 2016 06:31:16 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id AFA55DFB7D; Mon, 21 Mar 2016 06:31:16 +0000 (UTC) From: chinmaykolhatkar To: dev@apex.incubator.apache.org Reply-To: dev@apex.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-apex-malhar pull request: APEXMALHAR-2010 Add Tranform o... Content-Type: text/plain Message-Id: <20160321063116.AFA55DFB7D@git1-us-west.apache.org> Date: Mon, 21 Mar 2016 06:31:16 +0000 (UTC) Github user chinmaykolhatkar commented on a diff in the pull request: https://github.com/apache/incubator-apex-malhar/pull/209#discussion_r56785718 --- Diff: library/src/main/java/com/datatorrent/lib/util/PojoUtils.java --- @@ -696,11 +662,97 @@ private static Object createSetter(Class pojoClass, String setterExpr, String code = getSingleFieldSetterExpression(pojoClass, setterExpr, exprClass); } + return compileExpression(code, setterClass, new String[] {PojoUtils.OBJECT, PojoUtils.VAL}); + } + + /** + * This method takes in expression, compiles the expression to provide a executable form of expression. + * This method uses {@link com.datatorrent.lib.expression.JavaExpressionParser} as expression parser. + * + * @param inputType Type of input object + * @param expr expression to be compiled. + * @param returnType Return type of the expression. + * @return Object of type {@link Expression} which can be directly executed. + */ + public static Expression createExpression(Class inputType, String expr, Class returnType) + { + return createExpression(inputType, expr, returnType, null); + } + + /** + * This method takes in expression, compiles the expression to provide a executable form of expression. + * This methods also takes in list of classes and method which can be imported statically in expression. + *

+ * This method uses {@link JavaExpressionParser} as expression parser. + * + * @param inputType Type of input object + * @param expr expression to be compiled. + * @param returnType Return type of the expression. + * @param defaultImports List of classes/method which will be statically imported to expression compilation. + * @return Object of type {@link Expression} which can be directly executed. + */ + public static Expression createExpression(Class inputType, String expr, Class returnType, + String[] defaultImports) + { + JavaExpressionParser javaExpressionParser = new JavaExpressionParser(); + javaExpressionParser.setInputObjectPlaceholder("$", PojoUtils.OBJECT); + + return createExpression(inputType, expr, returnType, defaultImports, javaExpressionParser); + } + + /** + * This method takes in expression, compiles the expression to provide a executable form of expression. + * This methods also takes in list of classes and method which can be imported statically in expression. + *

+ * Using this method one can override expression parser implementation. + * + * @param inputType Type of input object + * @param expr expression to be compiled. + * @param returnType Return type of the expression. + * @param defaultImports List of classes/method which will be statically imported to expression compilation. + * @param parser Expression parser that should be used to parse expression. + * @return Object of type {@link Expression} which can be directly executed. + * @see {@link JavaExpressionParser} as a example. + */ + public static Expression createExpression(Class inputType, String expr, Class returnType, + String[] defaultImports, Expression.ExpressionParser parser) + { + String code = parser.convertToCompilableExpression(expr, inputType, returnType); + + return (Expression)compileExpression(code, Expression.class, new String[] {PojoUtils.OBJECT}, defaultImports); + } + + private static Object compileExpression(String code, Class implClass, String[] params) + { + return compileExpression(code, implClass, params, null); + } + + private static Object compileExpression(String code, Class implClass, String[] params, String[] defaultImports) + { + List imports = new LinkedList<>(); + if (defaultImports != null && defaultImports.length != 0) { + for (String defaultImport : defaultImports) { + if (!defaultImport.startsWith("static")) { --- End diff -- When adding a given import as static, that means the method will be directly available in expression to be used. This way, the expression writer can use similar syntax like excel functions. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---