Search in sources :

Example 1 with CREPluginInternalException

use of com.laytonsmith.core.exceptions.CRE.CREPluginInternalException in project CommandHelper by EngineHub.

the class Prefilters method ExpressionMatch.

private static void ExpressionMatch(Construct expression, String key, Construct dvalue) throws PrefilterNonMatchException {
    String exp = expression.val().substring(1, expression.val().length() - 1);
    boolean inequalityMode = false;
    if (exp.contains("<") || exp.contains(">") || exp.contains("==")) {
        inequalityMode = true;
    }
    String eClass = "com.sk89q.worldedit.internal.expression.Expression";
    String errClass = "com.sk89q.worldedit.internal.expression.ExpressionException";
    Class eClazz, errClazz;
    try {
        eClazz = Class.forName(eClass);
        errClazz = Class.forName(errClass);
    } catch (ClassNotFoundException cnf) {
        throw new CREPluginInternalException("You are missing a required dependency: " + eClass, expression.getTarget(), cnf);
    }
    try {
        Object e = ReflectionUtils.invokeMethod(eClazz, null, "compile", new Class[] { String.class, String[].class }, new Object[] { exp, new String[] { key } });
        double val = (double) ReflectionUtils.invokeMethod(eClazz, e, "evaluate", new Class[] { double[].class }, new Object[] { new double[] { Static.getDouble(dvalue, Target.UNKNOWN) } });
        if (inequalityMode) {
            if (val == 0) {
                throw new PrefilterNonMatchException();
            }
        } else {
            if (val != Static.getDouble(dvalue, Target.UNKNOWN)) {
                throw new PrefilterNonMatchException();
            }
        }
    } catch (ReflectionUtils.ReflectionException rex) {
        if (rex.getCause().getClass().isAssignableFrom(errClazz)) {
            throw new CREPluginInternalException("Your expression was invalidly formatted", expression.getTarget(), rex.getCause());
        } else {
            throw new CREPluginInternalException(rex.getMessage(), expression.getTarget(), rex.getCause());
        }
    }
}
Also used : PrefilterNonMatchException(com.laytonsmith.core.exceptions.PrefilterNonMatchException) CREPluginInternalException(com.laytonsmith.core.exceptions.CRE.CREPluginInternalException) CString(com.laytonsmith.core.constructs.CString) ReflectionUtils(com.laytonsmith.PureUtilities.Common.ReflectionUtils)

Example 2 with CREPluginInternalException

use of com.laytonsmith.core.exceptions.CRE.CREPluginInternalException in project CommandHelper by EngineHub.

the class Crypto method getHMAC.

private static CString getHMAC(String algorithm, Target t, Construct[] args) {
    try {
        SecretKeySpec signingKey = new SecretKeySpec(args[0].val().getBytes(), algorithm);
        Mac mac = Mac.getInstance(algorithm);
        mac.init(signingKey);
        byte[] hmac = mac.doFinal(args[1].val().getBytes());
        String hash = StringUtils.toHex(hmac).toLowerCase();
        return new CString(hash, t);
    } catch (NoSuchAlgorithmException | InvalidKeyException ex) {
        throw new CREPluginInternalException("An error occured while trying to hash your data", t, ex);
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) CREPluginInternalException(com.laytonsmith.core.exceptions.CRE.CREPluginInternalException) CString(com.laytonsmith.core.constructs.CString) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) Mac(javax.crypto.Mac) CString(com.laytonsmith.core.constructs.CString)

Example 3 with CREPluginInternalException

use of com.laytonsmith.core.exceptions.CRE.CREPluginInternalException in project CommandHelper by EngineHub.

the class RandomTests method expressionTester.

@Test
public void expressionTester() throws ConfigRuntimeException {
    // verify basic usage works
    String eClass = "com.sk89q.worldedit.internal.expression.Expression";
    try {
        Class clazz = Class.forName(eClass);
        Object e = ReflectionUtils.invokeMethod(clazz, null, "compile", new Class[] { String.class, String.class, String.class }, new Object[] { "(x + 2) * y", "x", "y" });
        double d = (double) ReflectionUtils.invokeMethod(clazz, e, "evaluate", new Class[] { double.class, double.class }, new Object[] { 2, 4 });
        assertEquals(16, d, 0.00001);
    } catch (ClassNotFoundException cnf) {
    /* Not much we can really do about this during testing.
			throw new CREPluginInternalException("You are missing a required dependency: " + eClass, Target.UNKNOWN);*/
    } catch (ReflectionUtils.ReflectionException rex) {
        throw new CREPluginInternalException("Your expression was invalidly formatted", Target.UNKNOWN, rex.getCause());
    }
}
Also used : AbstractionObject(com.laytonsmith.abstraction.AbstractionObject) CREPluginInternalException(com.laytonsmith.core.exceptions.CRE.CREPluginInternalException) CString(com.laytonsmith.core.constructs.CString) ReflectionUtils(com.laytonsmith.PureUtilities.Common.ReflectionUtils) Test(org.junit.Test)

Example 4 with CREPluginInternalException

use of com.laytonsmith.core.exceptions.CRE.CREPluginInternalException in project CommandHelper by EngineHub.

the class EventBuilder method instantiate.

public static <T extends BindableEvent> T instantiate(Class<? extends BindableEvent> clazz, Object... params) {
    try {
        if (!methods.containsKey((Class<BindableEvent>) clazz)) {
            warmup(clazz);
        }
        Object o = methods.get((Class<BindableEvent>) clazz).invoke(null, params);
        // of the event BindableEvent should know how to handle in a constructor.
        if (!constructors.containsKey((Class<BindableEvent>) clazz)) {
            Class bindableEvent = eventImplementations.get((Class<BindableEvent>) clazz);
            Constructor constructor = null;
            for (Constructor c : bindableEvent.getConstructors()) {
                if (c.getParameterTypes().length == 1) {
                    // looks promising
                    if (c.getParameterTypes()[0].equals(o.getClass())) {
                        // This is it
                        constructor = c;
                        break;
                    }
                }
            }
            if (constructor == null) {
                throw new CREPluginInternalException("Cannot find an acceptable constructor that follows the format:" + " public " + bindableEvent.getClass().getSimpleName() + "(" + o.getClass().getSimpleName() + " event)." + " Please notify the plugin author of this error.", Target.UNKNOWN);
            }
            constructors.put((Class<BindableEvent>) clazz, constructor);
        }
        // Construct a new instance, then return it.
        Constructor constructor = constructors.get((Class<BindableEvent>) clazz);
        BindableEvent be = (BindableEvent) constructor.newInstance(o);
        return (T) be;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}
Also used : Constructor(java.lang.reflect.Constructor) CREPluginInternalException(com.laytonsmith.core.exceptions.CRE.CREPluginInternalException) CREPluginInternalException(com.laytonsmith.core.exceptions.CRE.CREPluginInternalException)

Aggregations

CREPluginInternalException (com.laytonsmith.core.exceptions.CRE.CREPluginInternalException)4 CString (com.laytonsmith.core.constructs.CString)3 ReflectionUtils (com.laytonsmith.PureUtilities.Common.ReflectionUtils)2 AbstractionObject (com.laytonsmith.abstraction.AbstractionObject)1 PrefilterNonMatchException (com.laytonsmith.core.exceptions.PrefilterNonMatchException)1 Constructor (java.lang.reflect.Constructor)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Mac (javax.crypto.Mac)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 Test (org.junit.Test)1