
io.sarl.api.naming.namespace.FieldAccess Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of api.naming Show documentation
Show all versions of api.naming Show documentation
Naming Library of the SARL SDK
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 io.sarl.lang.core.annotation.SarlElementType;
import io.sarl.lang.core.annotation.SarlSpecification;
import io.sarl.lang.core.annotation.SyntheticMember;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import org.eclipse.xtend.lib.annotations.AccessorType;
import org.eclipse.xtend.lib.annotations.Accessors;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.XbaseGenerated;
/**
* 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
*/
@SarlSpecification("0.14")
@SarlElementType(10)
@XbaseGenerated
@SuppressWarnings("all")
public class FieldAccess {
@Accessors(AccessorType.PUBLIC_GETTER)
private final SarlName name;
@Accessors(AccessorType.PUBLIC_GETTER)
private final Field field;
@Accessors(AccessorType.PUBLIC_GETTER)
private final Object instance;
@Accessors(AccessorType.PUBLIC_GETTER)
private final boolean writable;
/**
* 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.
*/
public FieldAccess(final SarlName name, final Field field, final Object instance, final boolean writable) {
class $AssertEvaluator$ {
final boolean $$result;
$AssertEvaluator$() {
boolean _isStatic = Modifier.isStatic(field.getModifiers());
this.$$result = (!_isStatic);
}
}
assert new $AssertEvaluator$().$$result;
this.name = name;
this.field = field;
this.instance = instance;
this.writable = writable;
this.field.setAccessible(true);
}
/**
* Replies the value of the field.
*
* @return the field's value.
*/
@Pure
public Object get() {
try {
return this.field.get(this.instance);
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
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.
*/
public Object set(final Object value) throws IllegalArgumentException, IllegalAccessException {
if (this.writable) {
try {
final Object oldValue = this.field.get(this.instance);
this.field.set(this.instance, value);
return oldValue;
} catch (final Throwable _t) {
if (_t instanceof IllegalArgumentException) {
final IllegalArgumentException ex = (IllegalArgumentException)_t;
throw ex;
} else if (_t instanceof Throwable) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
return null;
} else {
throw new IllegalAccessException();
}
}
@Pure
@Override
public String toString() {
return this.field.getName();
}
@Override
@Pure
@SyntheticMember
public boolean equals(final Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
FieldAccess other = (FieldAccess) obj;
if (other.writable != this.writable)
return false;
return super.equals(obj);
}
@Override
@Pure
@SyntheticMember
public int hashCode() {
int result = super.hashCode();
final int prime = 31;
result = prime * result + Boolean.hashCode(this.writable);
return result;
}
@Pure
public SarlName getName() {
return this.name;
}
@Pure
public Field getField() {
return this.field;
}
@Pure
public Object getInstance() {
return this.instance;
}
@Pure
public boolean isWritable() {
return this.writable;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy