All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jsimpledb.parse.expr.StaticFieldValue Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb.parse.expr;

import com.google.common.base.Preconditions;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import org.jsimpledb.parse.ParseSession;

/**
 * {@link Value} that reflects a static Java field in some {@link Class}.
 */
public class StaticFieldValue extends AbstractFieldValue {

    /**
     * Constructor.
     *
     * @param field field
     * @throws IllegalArgumentException if {@code field} is null
     * @throws IllegalArgumentException if {@code field} is not static
     */
    public StaticFieldValue(Field field) {
        super(field);
        Preconditions.checkArgument((field.getModifiers() & Modifier.STATIC) != 0, "field is not static");
    }

    @Override
    public Object get(ParseSession session) {
        try {
            return this.field.get(null);
        } catch (Exception e) {
            throw new EvalException("error reading static field `" + this.field.getName()
              + "' in class `" + this.field.getDeclaringClass().getName() + "': " + e, e);
        }
    }

    @Override
    public void set(ParseSession session, Value value) {
        final Object obj = value.get(session);
        try {
            this.field.set(null, obj);
        } catch (IllegalArgumentException e) {
            throw new EvalException("invalid " + AbstractValue.describeType(obj) + " for static field `"
              + this.field.getName() + "' in class `" + this.field.getDeclaringClass().getName() + "'", e);
        } catch (Exception e) {
            throw new EvalException("error writing static field `" + this.field.getName()
              + "' in class `" + this.field.getDeclaringClass().getName() + "': " + e, e);
        }
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy