Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[api][naming] Add right management for access to named fields.
The access right could be specified with annotations or by implementing a dedicated agent skill. see #972 Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
bfc9c5b
commit 3f302c0
Showing
61 changed files
with
3,564 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...api.naming/src/main/sarl/io/sarl/api/naming/namespace/AnnotationFieldAccessValidator.sarl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2020 the original authors or 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 java.lang.reflect.Field | ||
|
||
/** | ||
* A tool that is able to determine if a field is observable or not based | ||
* on the annotation {@link Observable}. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 0.12 | ||
*/ | ||
class AnnotationFieldAccessValidator implements IFieldAccessValidator { | ||
|
||
override getFieldAccessRight(fieldOwner : Object, field : Field) : FieldAccessRight { | ||
val observable = field.observableAnnotation | ||
if (observable !== null) { | ||
if (observable.writable) { | ||
return FieldAccessRight::WRITE | ||
} | ||
return FieldAccessRight::READ | ||
} | ||
return FieldAccessRight::NONE | ||
} | ||
|
||
private static def getObservableAnnotation(field : Field) : Observable { | ||
var annon : Observable = field.getAnnotation(typeof(Observable)) | ||
if (annon !== null) { | ||
return annon | ||
} | ||
var container = field.declaringClass | ||
while (container !== null) { | ||
annon = container.getAnnotation(typeof(Observable)) | ||
if (annon !== null) { | ||
return annon | ||
} | ||
container = container.enclosingClass | ||
} | ||
return null | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
...l.api.naming/src/main/sarl/io/sarl/api/naming/namespace/CapacityFieldAccessValidator.sarl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2020 the original authors or 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.lang.annotation.PrivateAPI | ||
import io.sarl.lang.core.Agent | ||
import io.sarl.lang.core.AgentTrait | ||
import io.sarl.lang.core.SREutils | ||
import java.lang.reflect.Field | ||
|
||
/** | ||
* A tool that is able to determine if a field is observable or not based on the owned | ||
* capacity {@link FieldAccessValidationCapacity} of an agent. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 0.12 | ||
*/ | ||
class CapacityFieldAccessValidator implements IFieldAccessValidator { | ||
|
||
@SuppressWarnings("use_reserved_sarl_annotation") | ||
@PrivateAPI(isCallerOnly = true) | ||
override getFieldAccessRight(fieldOwner : Object, field : Field) : FieldAccessRight { | ||
var ag : Agent = null | ||
if (fieldOwner instanceof AgentTrait) { | ||
ag = fieldOwner.owner | ||
} else if (fieldOwner instanceof Agent) { | ||
ag = fieldOwner | ||
} | ||
if (ag !== null) { | ||
try { | ||
var sk = SREutils::getInternalSkill(ag, typeof(FieldAccessValidationCapacity)) | ||
var right = sk.getFieldAccessRight(field) | ||
if (right !== null) { | ||
return right | ||
} | ||
} catch (exception : Throwable) { | ||
// Ignore this exception | ||
} | ||
} | ||
return FieldAccessRight::NONE | ||
} | ||
|
||
} |
Oops, something went wrong.