
io.sarl.api.naming.namespace.AbstractNamespaceService Maven / Gradle / Ivy
Show all versions of api.naming Show documentation
/**
* $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.api.naming.parser.INameParser;
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.util.Objects;
import org.arakhne.afc.services.AbstractService;
import org.arakhne.afc.services.IService;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Pure;
import org.eclipse.xtext.xbase.lib.XbaseGenerated;
/**
* Abstract implementation of a service that manages name spaces into the SRE.
*
* @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 abstract class AbstractNamespaceService extends AbstractService implements NamespaceService {
private INameParser nameParser;
private IFieldAccessValidator fieldAccessValidator;
@Override
public Class extends IService> getReferenceType() {
return NamespaceService.class;
}
@Pure
@Override
public INameParser getNameParser() {
return this.nameParser;
}
/**
* Change the name parser used by this service.
*
* This function could be overloaded by sub-types in order to inject the name parser.
*
* @param parser the name parser, never {@code null}
* @since 0.11
*/
public void setNameParser(final INameParser parser) {
this.nameParser = parser;
}
@Pure
@Override
public IFieldAccessValidator getFieldAccessValidator() {
return this.fieldAccessValidator;
}
/**
* Change the field access validator used by this service.
*
*
This function could be overloaded by sub-types in order to inject the field access validator.
*
* @param validator the field access validator, never {@code null}
* @since 0.12
*/
public void setFieldAccessValidator(final IFieldAccessValidator validator) {
this.fieldAccessValidator = validator;
}
@Override
@Pure
public final T findObject(final SarlName name, final Class type) {
class $AssertEvaluator$ {
final boolean $$result;
$AssertEvaluator$() {
this.$$result = (type != null);
}
}
assert new $AssertEvaluator$().$$result;
Object obj = this.findObject(name);
if (((obj != null) && type.isInstance(obj))) {
return type.cast(obj);
}
return null;
}
@Override
@Pure
public final Object findObject(final SarlName name) {
if ((name == null)) {
return null;
}
Object associatedObject = name.getAssociatedObject();
if ((associatedObject == null)) {
Object obj = this.findObjectWithoutFragment(name);
if (((obj != null) && name.hasFragment())) {
FieldAccess field = this.getDeclaredField(obj, name);
if ((field != null)) {
associatedObject = field;
}
return field;
} else {
try {
associatedObject = obj;
} catch (final Throwable _t) {
if (_t instanceof ClassCastException) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
}
}
return associatedObject;
}
@Pure
private FieldAccess getDeclaredField(final Object obj, final SarlName name) {
Class> type = obj.getClass();
final IFieldAccessValidator fav = this.getFieldAccessValidator();
if ((fav != null)) {
while (((type != null) && (!Objects.equals(Object.class, type)))) {
{
try {
Field field = type.getDeclaredField(name.getFragment());
if ((field != null)) {
final FieldAccessRight right = fav.getFieldAccessRight(obj, field);
if (right != null) {
switch (right) {
case WRITE:
return new FieldAccess(name, field, obj, true);
case READ:
return new FieldAccess(name, field, obj, false);
default:
break;
}
} else {
}
}
} catch (final Throwable _t) {
if (_t instanceof Throwable) {
} else {
throw Exceptions.sneakyThrow(_t);
}
}
type = type.getSuperclass();
}
}
}
return null;
}
/**
* Find an object with the given name, but ignoring the fragment.
*
* @param name the name of the object, never {@code null}.
* @return the object, or {@code null} if the object was not found.
*/
protected abstract Object findObjectWithoutFragment(final SarlName name);
@Override
@Pure
@SyntheticMember
public boolean equals(final Object obj) {
return super.equals(obj);
}
@Override
@Pure
@SyntheticMember
public int hashCode() {
int result = super.hashCode();
return result;
}
@SyntheticMember
public AbstractNamespaceService() {
super();
}
}