Example usage for java.beans Statement execute

List of usage examples for java.beans Statement execute

Introduction

In this page you can find the example usage for java.beans Statement execute.

Prototype

public void execute() throws Exception 

Source Link

Document

The execute method finds a method whose name is the same as the methodName property, and invokes the method on the target.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object o = new MyBean();
    // Get the value of prop1
    Expression expr = new Expression(o, "getProp1", new Object[0]);
    expr.execute();//from   ww  w .ja v a 2  s. com
    String s = (String) expr.getValue();

    // Set the value of prop1
    Statement stmt = new Statement(o, "setProp1", new Object[] { "new string" });
    stmt.execute();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object o = new MyBean();
    // Get the value of prop2
    Expression expr = new Expression(o, "getProp2", new Object[0]);
    expr.execute();/*from  w  ww.  jav a  2  s  .  c o m*/
    int i = ((Integer) expr.getValue()).intValue();

    // Set the value of prop2
    Statement stmt = new Statement(o, "setProp2", new Object[] { new Integer(123) });
    stmt.execute();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Object o = new MyBean();

    Expression expr = new Expression(o, "getProp3", new Object[0]);
    expr.execute();//w  ww . j  av a  2 s.c o  m
    byte[] bytes = (byte[]) expr.getValue();

    Statement stmt = new Statement(o, "setProp3", new Object[] { new byte[] { 0x12, 0x23 } });
    stmt.execute();
}

From source file:org.codehaus.sonarplugins.cxx.cxxlint.CxxLint.java

/**
 * @param args the command line arguments
 * @throws java.lang.InstantiationException
 * @throws java.lang.IllegalAccessException
 * @throws java.io.IOException//  ww  w . j a va2 s  . c  o  m
 */
public static void main(String[] args)
        throws InstantiationException, IllegalAccessException, IOException, Exception {

    CommandLineParser commandlineParser = new DefaultParser();
    Options options = CreateCommandLineOptions();
    CommandLine parsedArgs;
    String settingsFile = "";
    String fileToAnalyse = "";
    CxxConfiguration configuration = new CxxConfiguration();

    try {
        parsedArgs = commandlineParser.parse(CreateCommandLineOptions(), args);
        if (!parsedArgs.hasOption("f")) {
            throw new ParseException("f option mandatory");
        } else {
            fileToAnalyse = parsedArgs.getOptionValue("f");
            File f = new File(fileToAnalyse);
            if (!f.exists()) {
                throw new ParseException("file to analysis not found");
            }
        }

        if (parsedArgs.hasOption("s")) {
            settingsFile = parsedArgs.getOptionValue("s");
            File f = new File(settingsFile);
            if (!f.exists()) {
                throw new ParseException("optional settings file given with -s, however file was not found");
            }
        }
    } catch (ParseException exp) {
        System.err.println("Parsing Command line Failed.  Reason: " + exp.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar CxxLint-<sersion>.jar -f filetoanalyse", options);
        throw exp;
    }

    HashMap<String, CheckerData> rulesData = new HashMap<>();
    if (!"".equals(settingsFile)) {
        JsonParser parser = new JsonParser();
        String fileContent = readFile(settingsFile);

        // get basic information
        String platformToolset = GetJsonStringValue(parser, fileContent, "platformToolset");
        String platform = GetJsonStringValue(parser, fileContent, "platform");
        String projectFile = GetJsonStringValue(parser, fileContent, "projectFile");

        JsonElement rules = parser.parse(fileContent).getAsJsonObject().get("rules");
        if (rules != null) {
            for (JsonElement rule : rules.getAsJsonArray()) {
                JsonObject data = rule.getAsJsonObject();
                String ruleId = data.get("ruleId").getAsString();
                String enabled = data.get("status").getAsString();
                if (rulesData.containsKey(ruleId)) {
                    continue;
                }

                CheckerData check = new CheckerData();
                check.id = ruleId;
                check.enabled = enabled.equals("Enabled");
                JsonElement region = data.get("properties");
                if (region != null) {
                    for (Entry parameter : region.getAsJsonObject().entrySet()) {
                        JsonElement elem = (JsonElement) parameter.getValue();
                        check.parameterData.put(parameter.getKey().toString(), elem.getAsString());
                    }
                }

                rulesData.put(ruleId, check);
            }
        }

        JsonElement includes = parser.parse(fileContent).getAsJsonObject().get("includes");
        if (includes != null) {
            for (JsonElement include : includes.getAsJsonArray()) {
                configuration.addOverallIncludeDirectory(include.getAsString());
            }
        }

        JsonElement defines = parser.parse(fileContent).getAsJsonObject().get("defines");
        if (defines != null) {
            for (JsonElement define : defines.getAsJsonArray()) {
                configuration.addOverallDefine(define.getAsString());
            }
        }

        JsonElement additionalOptions = parser.parse(fileContent).getAsJsonObject().get("additionalOptions");
        String elementsOfAdditionalOptions = "";
        if (additionalOptions != null) {
            for (JsonElement option : additionalOptions.getAsJsonArray()) {
                elementsOfAdditionalOptions = elementsOfAdditionalOptions + " " + option.getAsString();
            }
        }

        HandleVCppAdditionalOptions(platformToolset, platform, elementsOfAdditionalOptions + " ", projectFile,
                fileToAnalyse, configuration);
    }

    List<Class> checks = CheckList.getChecks();
    List<SquidAstVisitor<Grammar>> visitors = new ArrayList<>();
    HashMap<String, String> KeyData = new HashMap<String, String>();

    for (Class check : checks) {
        Rule rule = (Rule) check.getAnnotation(Rule.class);
        if (rule == null) {
            continue;
        }

        SquidAstVisitor<Grammar> element = (SquidAstVisitor<Grammar>) check.newInstance();

        KeyData.put(check.getCanonicalName(), rule.key());

        if (!parsedArgs.hasOption("s")) {
            visitors.add(element);
            continue;
        }

        if (!rulesData.containsKey(rule.key())) {
            continue;
        }

        CheckerData data = rulesData.get(rule.key());

        if (!data.enabled) {
            continue;
        }

        for (Field f : check.getDeclaredFields()) {
            for (Annotation a : f.getAnnotations()) {
                RuleProperty ruleProp = (RuleProperty) a;
                if (ruleProp != null) {
                    if (data.parameterData.containsKey(ruleProp.key())) {
                        if (f.getType().equals(int.class)) {
                            String cleanData = data.parameterData.get(ruleProp.key());
                            int value = Integer.parseInt(cleanData);
                            if (f.toString().startsWith("public ")) {
                                f.set(element, value);
                            } else {
                                char first = Character.toUpperCase(ruleProp.key().charAt(0));
                                Statement stmt = new Statement(element,
                                        "set" + first + ruleProp.key().substring(1), new Object[] { value });
                                stmt.execute();
                            }
                        }

                        if (f.getType().equals(String.class)) {
                            String cleanData = data.parameterData.get(ruleProp.key());

                            if (f.toString().startsWith("public ")) {
                                f.set(element, cleanData);
                            } else {
                                char first = Character.toUpperCase(ruleProp.key().charAt(0));
                                Statement stmt = new Statement(element,
                                        "set" + first + ruleProp.key().substring(1),
                                        new Object[] { cleanData });
                                stmt.execute();
                            }
                        }
                    }
                }
            }
        }
        visitors.add(element);
    }

    System.out.println("Analyse with : " + visitors.size() + " checks");
    SourceFile file = CxxAstScanner.scanSingleFileConfig(new File(fileToAnalyse), configuration,
            visitors.toArray(new SquidAstVisitor[visitors.size()]));
    for (CheckMessage message : file.getCheckMessages()) {
        String key = KeyData.get(message.getCheck().getClass().getCanonicalName());
        // E:\TSSRC\Core\Common\libtools\tool_archive.cpp(390): Warning : sscanf can be ok, but is slow and can overflow buffers.  [runtime/printf-5] [1]
        System.out.println(message.getSourceCode() + "(" + message.getLine() + "): Warning : "
                + message.formatDefaultMessage() + " [" + key + "]");
    }
}

From source file:org.sonar.cxx.cxxlint.CxxLint.java

/**
 * @param args the command line arguments
 * @throws java.lang.InstantiationException
 * @throws java.lang.IllegalAccessException
 * @throws java.io.IOException/* w w  w  .  j  a  v a  2s  . co m*/
 */
public static void main(String[] args)
        throws InstantiationException, IllegalAccessException, IOException, Exception {

    CommandLineParser commandlineParser = new DefaultParser();
    Options options = CreateCommandLineOptions();
    CommandLine parsedArgs;
    String settingsFile = "";
    String fileToAnalyse = "";
    String encodingOfFile = "UTF-8";

    try {
        parsedArgs = commandlineParser.parse(CreateCommandLineOptions(), args);
        if (!parsedArgs.hasOption("f")) {
            throw new ParseException("f option mandatory");
        } else {
            fileToAnalyse = parsedArgs.getOptionValue("f");
            File f = new File(fileToAnalyse);
            if (!f.exists()) {
                throw new ParseException("file to analysis not found");
            }
        }

        if (parsedArgs.hasOption("s")) {
            settingsFile = parsedArgs.getOptionValue("s");
            File f = new File(settingsFile);
            if (!f.exists()) {
                throw new ParseException("optional settings file given with -s, however file was not found");
            }
        }

        if (parsedArgs.hasOption("e")) {
            encodingOfFile = parsedArgs.getOptionValue("e");
        }

    } catch (ParseException exp) {
        System.err.println("Parsing Command line Failed.  Reason: " + exp.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("java -jar CxxLint-<sersion>.jar -f filetoanalyse", options);
        throw exp;
    }

    CxxConfiguration configuration = new CxxConfiguration(Charset.forName(encodingOfFile));

    String fileName = new File(fileToAnalyse).getName();
    SensorContextTester sensorContext = SensorContextTester
            .create(new File(fileToAnalyse).getParentFile().toPath());
    String content = new String(Files.readAllBytes(new File(fileToAnalyse).toPath()), encodingOfFile);
    sensorContext.fileSystem().add(new DefaultInputFile("myProjectKey", fileName).initMetadata(content));
    InputFile cxxFile = sensorContext.fileSystem()
            .inputFile(sensorContext.fileSystem().predicates().hasPath(fileName));

    HashMap<String, CheckerData> rulesData = new HashMap<>();
    if (!"".equals(settingsFile)) {
        JsonParser parser = new JsonParser();
        String fileContent = readFile(settingsFile);

        // get basic information
        String platformToolset = GetJsonStringValue(parser, fileContent, "platformToolset");
        String platform = GetJsonStringValue(parser, fileContent, "platform");
        String projectFile = GetJsonStringValue(parser, fileContent, "projectFile");

        JsonElement rules = parser.parse(fileContent).getAsJsonObject().get("rules");
        if (rules != null) {
            for (JsonElement rule : rules.getAsJsonArray()) {
                JsonObject data = rule.getAsJsonObject();
                String ruleId = data.get("ruleId").getAsString();
                String enabled = data.get("status").getAsString();
                if (rulesData.containsKey(ruleId)) {
                    continue;
                }

                CheckerData check = new CheckerData();
                check.id = ruleId;
                check.enabled = enabled.equals("Enabled");
                JsonElement region = data.get("properties");
                if (region != null) {
                    for (Entry parameter : region.getAsJsonObject().entrySet()) {
                        JsonElement elem = (JsonElement) parameter.getValue();
                        check.parameterData.put(parameter.getKey().toString(), elem.getAsString());
                    }
                }

                rulesData.put(ruleId, check);
            }
        }

        JsonElement includes = parser.parse(fileContent).getAsJsonObject().get("includes");
        if (includes != null) {
            for (JsonElement include : includes.getAsJsonArray()) {
                configuration.addOverallIncludeDirectory(include.getAsString());
            }
        }

        JsonElement defines = parser.parse(fileContent).getAsJsonObject().get("defines");
        if (defines != null) {
            for (JsonElement define : defines.getAsJsonArray()) {
                configuration.addOverallDefine(define.getAsString());
            }
        }

        JsonElement additionalOptions = parser.parse(fileContent).getAsJsonObject().get("additionalOptions");
        String elementsOfAdditionalOptions = "";
        if (additionalOptions != null) {
            for (JsonElement option : additionalOptions.getAsJsonArray()) {
                elementsOfAdditionalOptions = elementsOfAdditionalOptions + " " + option.getAsString();
            }
        }

        HandleVCppAdditionalOptions(platformToolset, platform, elementsOfAdditionalOptions + " ", projectFile,
                fileToAnalyse, configuration);
    }

    List<Class> checks = CheckList.getChecks();
    List<SquidAstVisitor<Grammar>> visitors = new ArrayList<>();
    HashMap<String, String> KeyData = new HashMap<String, String>();

    for (Class check : checks) {
        Rule rule = (Rule) check.getAnnotation(Rule.class);
        if (rule == null) {
            continue;
        }

        SquidAstVisitor<Grammar> element = (SquidAstVisitor<Grammar>) check.newInstance();

        KeyData.put(check.getCanonicalName(), rule.key());

        if (!parsedArgs.hasOption("s")) {
            visitors.add(element);
            continue;
        }

        if (!rulesData.containsKey(rule.key())) {
            continue;
        }

        CheckerData data = rulesData.get(rule.key());

        if (!data.enabled) {
            continue;
        }

        for (Field f : check.getDeclaredFields()) {
            for (Annotation a : f.getAnnotations()) {
                RuleProperty ruleProp = (RuleProperty) a;
                if (ruleProp != null) {
                    if (data.parameterData.containsKey(ruleProp.key())) {
                        if (f.getType().equals(int.class)) {
                            String cleanData = data.parameterData.get(ruleProp.key());
                            int value = Integer.parseInt(cleanData);
                            if (f.toString().startsWith("public ")) {
                                f.set(element, value);
                            } else {
                                char first = Character.toUpperCase(ruleProp.key().charAt(0));
                                Statement stmt = new Statement(element,
                                        "set" + first + ruleProp.key().substring(1), new Object[] { value });
                                stmt.execute();
                            }
                        }

                        if (f.getType().equals(String.class)) {
                            String cleanData = data.parameterData.get(ruleProp.key());

                            if (f.toString().startsWith("public ")) {
                                f.set(element, cleanData);
                            } else {
                                char first = Character.toUpperCase(ruleProp.key().charAt(0));
                                Statement stmt = new Statement(element,
                                        "set" + first + ruleProp.key().substring(1),
                                        new Object[] { cleanData });
                                stmt.execute();
                            }
                        }
                    }
                }
            }
        }
        visitors.add(element);
    }

    System.out.println("Analyse with : " + visitors.size() + " checks");

    SourceFile file = CxxAstScanner.scanSingleFileConfig(cxxFile, configuration, sensorContext,
            visitors.toArray(new SquidAstVisitor[visitors.size()]));

    for (CheckMessage message : file.getCheckMessages()) {
        String key = KeyData.get(message.getCheck().getClass().getCanonicalName());
        // E:\TSSRC\Core\Common\libtools\tool_archive.cpp(390): Warning : sscanf can be ok, but is slow and can overflow buffers.  [runtime/printf-5] [1]
        System.out.println(message.getSourceCode() + "(" + message.getLine() + "): Warning : "
                + message.formatDefaultMessage() + " [" + key + "]");
    }

    System.out.println("LOC: " + file.getInt(CxxMetric.LINES_OF_CODE));
    System.out.println("COMPLEXITY: " + file.getInt(CxxMetric.COMPLEXITY));
}

From source file:ca.oson.json.util.ObjectUtil.java

@SuppressWarnings("unchecked")
public static <E> void setMethodValue(E obj, Method method, Object... args) {
    try {/*w ww. j a v  a  2s. c  o m*/
        method.setAccessible(true);

        method.invoke(obj, args);

    } catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
        // e.printStackTrace();
        try {
            if (obj != null) {
                Statement stmt = new Statement(obj, method.getName(), args);
                stmt.execute();
            }

        } catch (Exception e1) {
            // e1.printStackTrace();
        }
    }
}

From source file:org.jasypt.spring31.xml.encryption.EncryptorFactoryBean.java

private Object computeObject() throws Exception {

    Object encryptor = null;//w  w w.  j  a v a2s .  co m

    if (isPooled()) {

        if (this.encryptorType == ENCRYPTOR_TYPE_BYTE) {
            encryptor = new PooledPBEByteEncryptor();
        } else if (this.encryptorType == ENCRYPTOR_TYPE_STRING) {
            encryptor = new PooledPBEStringEncryptor();
        } else if (this.encryptorType == ENCRYPTOR_TYPE_BIG_DECIMAL) {
            encryptor = new PooledPBEBigDecimalEncryptor();
        } else if (this.encryptorType == ENCRYPTOR_TYPE_BIG_INTEGER) {
            encryptor = new PooledPBEBigIntegerEncryptor();
        } else {
            throw new IllegalArgumentException("Unknown encryptor type: " + this.encryptorType);
        }

        if (this.poolSizeSet && this.poolSize != null) {
            final Statement st = new Statement(encryptor, "setPoolSize", new Object[] { this.poolSize });
            st.execute();
        }

    } else {

        if (this.encryptorType == ENCRYPTOR_TYPE_BYTE) {
            encryptor = new StandardPBEByteEncryptor();
        } else if (this.encryptorType == ENCRYPTOR_TYPE_STRING) {
            encryptor = new StandardPBEStringEncryptor();
        } else if (this.encryptorType == ENCRYPTOR_TYPE_BIG_DECIMAL) {
            encryptor = new StandardPBEBigDecimalEncryptor();
        } else if (this.encryptorType == ENCRYPTOR_TYPE_BIG_INTEGER) {
            encryptor = new StandardPBEBigIntegerEncryptor();
        } else {
            throw new IllegalArgumentException("Unknown encryptor type: " + this.encryptorType);
        }

    }

    if (this.algorithmSet) {
        final Statement st = new Statement(encryptor, "setAlgorithm", new Object[] { this.algorithm });
        st.execute();
    }
    if (this.configSet) {
        final Statement st = new Statement(encryptor, "setConfig", new Object[] { this.config });
        st.execute();
    }
    if (this.keyObtentionIterationsSet && this.keyObtentionIterations != null) {
        final Statement st = new Statement(encryptor, "setKeyObtentionIterations",
                new Object[] { this.keyObtentionIterations });
        st.execute();
    }
    if (this.passwordSet) {
        final Statement st = new Statement(encryptor, "setPassword", new Object[] { this.password });
        st.execute();
    }
    if (this.providerSet) {
        final Statement st = new Statement(encryptor, "setProvider", new Object[] { this.provider });
        st.execute();
    }
    if (this.providerNameSet) {
        final Statement st = new Statement(encryptor, "setProviderName", new Object[] { this.providerName });
        st.execute();
    }
    if (this.saltGeneratorSet) {
        final Statement st = new Statement(encryptor, "setSaltGenerator", new Object[] { this.saltGenerator });
        st.execute();
    }
    if (this.stringOutputTypeSet && encryptor instanceof PBEStringEncryptor) {
        final Statement st = new Statement(encryptor, "setStringOutputType",
                new Object[] { this.stringOutputType });
        st.execute();
    }

    return encryptor;

}

From source file:org.jasypt.spring31.xml.encryption.DigesterFactoryBean.java

private synchronized Object computeObject() throws Exception {

    Object digester = null;/*from   ww  w  .jav a  2 s  .co m*/

    if (isPooled()) {

        if (this.digesterType == DIGESTER_TYPE_BYTE) {
            digester = new PooledByteDigester();
        } else if (this.digesterType == DIGESTER_TYPE_STRING) {
            digester = new PooledStringDigester();
        } else {
            throw new IllegalArgumentException("Unknown digester type: " + this.digesterType);
        }

        if (this.poolSizeSet && this.poolSize != null) {
            final Statement st = new Statement(digester, "setPoolSize", new Object[] { this.poolSize });
            st.execute();
        }

    } else {

        if (this.digesterType == DIGESTER_TYPE_BYTE) {
            digester = new StandardByteDigester();
        } else if (this.digesterType == DIGESTER_TYPE_STRING) {
            digester = new StandardStringDigester();
        } else {
            throw new IllegalArgumentException("Unknown digester type: " + this.digesterType);
        }

    }

    if (this.algorithmSet) {
        final Statement st = new Statement(digester, "setAlgorithm", new Object[] { this.algorithm });
        st.execute();
    }
    if (this.configSet) {
        final Statement st = new Statement(digester, "setConfig", new Object[] { this.config });
        st.execute();
    }
    if (this.iterationsSet && this.iterations != null) {
        final Statement st = new Statement(digester, "setIterations", new Object[] { this.iterations });
        st.execute();
    }
    if (this.invertPositionOfSaltInMessageBeforeDigestingSet
            && this.invertPositionOfSaltInMessageBeforeDigesting != null) {
        final Statement st = new Statement(digester, "setInvertPositionOfSaltInMessageBeforeDigesting",
                new Object[] { this.invertPositionOfSaltInMessageBeforeDigesting });
        st.execute();
    }
    if (this.invertPositionOfPlainSaltInEncryptionResultsSet
            && this.invertPositionOfPlainSaltInEncryptionResults != null) {
        final Statement st = new Statement(digester, "setInvertPositionOfPlainSaltInEncryptionResults",
                new Object[] { this.invertPositionOfPlainSaltInEncryptionResults });
        st.execute();
    }
    if (this.providerSet) {
        final Statement st = new Statement(digester, "setProvider", new Object[] { this.provider });
        st.execute();
    }
    if (this.providerNameSet) {
        final Statement st = new Statement(digester, "setProviderName", new Object[] { this.providerName });
        st.execute();
    }
    if (this.saltGeneratorSet) {
        final Statement st = new Statement(digester, "setSaltGenerator", new Object[] { this.saltGenerator });
        st.execute();
    }
    if (this.saltSizeBytesSet && this.saltSizeBytes != null) {
        final Statement st = new Statement(digester, "setSaltSizeBytes", new Object[] { this.saltSizeBytes });
        st.execute();
    }
    if (this.useLenientSaltSizeCheckSet && this.useLenientSaltSizeCheck != null) {
        final Statement st = new Statement(digester, "setUseLenientSaltSizeCheck",
                new Object[] { this.useLenientSaltSizeCheck });
        st.execute();
    }

    if (digester instanceof StringDigester) {

        if (this.stringOutputTypeSet) {
            final Statement st = new Statement(digester, "setStringOutputType",
                    new Object[] { this.stringOutputType });
            st.execute();
        }
        if (this.unicodeNormalizationIgnoredSet) {
            final Statement st = new Statement(digester, "setUnicodeNormalizationIgnored",
                    new Object[] { this.unicodeNormalizationIgnored });
            st.execute();
        }
        if (this.prefixSet) {
            final Statement st = new Statement(digester, "setPrefix", new Object[] { this.prefix });
            st.execute();
        }
        if (this.suffixSet) {
            final Statement st = new Statement(digester, "setSuffix", new Object[] { this.suffix });
            st.execute();
        }

    }

    return digester;

}