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

io.sarl.api.naming.namespace.FieldAccess.sarl Maven / Gradle / Ivy

The newest version!
/*
 * $Id$
 *
 * SARL is an general-purpose agent programming language.
 * More details on http://www.sarl.io
 *
 * Copyright (C) 2014-2024 SARL.io, the Original Authors and Main Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.sarl.api.naming.namespace

import io.sarl.api.naming.name.SarlName
import java.lang.reflect.Field
import java.lang.reflect.Modifier
import org.eclipse.xtend.lib.annotations.Accessors

/** 
 * Accessor to a field.
 * 
 * @author Stéphane Galland
 * @version api.naming 0.14.0 20241106-161408
 * @mavengroupid io.sarl.sdk
 * @mavenartifactid api.naming
 * @since 0.12
 */
class FieldAccess {

	@Accessors(PUBLIC_GETTER)
	val name : SarlName

	@Accessors(PUBLIC_GETTER)
	val field : Field

	@Accessors(PUBLIC_GETTER)
	val instance : Object

	@Accessors(PUBLIC_GETTER)
	val writable : boolean

	/** Constructor.
	 *
	 * @param name the name of the field.
	 * @param field the field declaration.
	 * @param instance the object to have access to.
	 * @param writable indicates if the field is writeable through this accessor.
	 */
	new (name : SarlName, field : Field, instance : Object, writable : boolean) {
		assert !Modifier::isStatic(field.modifiers)
		this.name = name
		this.field = field
		this.instance = instance
		this.writable = writable
		this.field.accessible = true
	}

	/** Replies the value of the field.
	 *
	 * @return the field's value.
	 */
	@Pure
	def get : Object {
		try {
			return this.field.get(this.instance)
		} catch (ex : Throwable) {
			//
		}
		return null
	}

	/** Change the value of the field.
	 * 
	 * @param value the field's value.
	 * @return the value before setting.
	 * @throws IllegalArgumentException if the given value cannot be assigned to the field.
	 * @throws IllegalAccessException if the field cannot be written.
	 */
	def set(value : Object) : Object throws IllegalArgumentException, IllegalAccessException {
		if (this.writable) {
			try {
				val oldValue = this.field.get(this.instance)
				this.field.set(this.instance, value)
				return oldValue
			} catch (ex : IllegalArgumentException) {
				throw ex
			} catch (ex : Throwable) {
				//
			}
			return null
		} else {
			throw new IllegalAccessException
		}
	}

	@Pure
	override toString : String {
		this.field.name
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy