Skip to content

Commit

Permalink
[lang] Enable active annotations on SARL type declarations.
Browse files Browse the repository at this point in the history
close #868

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Dec 1, 2018
1 parent 8e7448a commit efc3585
Show file tree
Hide file tree
Showing 21 changed files with 1,906 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,48 @@ The following sections describe there annotations.
## @Accessors

If you want to add getter and or setter methods for your fields, [:accessorsannon:] is your friend.
This annotation can be applied to object-oriented types. The agent-oriented types cannot be annotated.
Let's a basic example.
This annotation can be applied to either object-oriented types and several agent-oriented types.
The agent-oriented types in which you could uses the [:accessorsannon:] annotation are the agents,
the behaviors and the skills.

[:Success:]
import org.eclipse.xtend.lib.annotations.Accessors
class MyClass {
[:On]
[:accessorsannon](@Accessors) var name : String
[:Off]
agent MyAgent {
[:accessorsannon!] var name : String
}
[:End:]
[:Success:]
import org.eclipse.xtend.lib.annotations.Accessors
behavior MyBehavior {
[:accessorsannon!] var name : String
}
[:End:]
[:Success:]
import org.eclipse.xtend.lib.annotations.Accessors
capacity MyCap { }
skill MySkill implements MyCap {
[:accessorsannon!] var name : String
}
[:End:]
[:Failure:]
import org.eclipse.xtend.lib.annotations.Accessors
agent MyAgent {
event MyEvent {
[:accessorsannon!] var name : String
}
[:End:]

will compile to the code:
Let's a basic example.

[:Success:]
import org.eclipse.xtend.lib.annotations.Accessors
class MyClass {
[:On]
[:accessorsannon](@Accessors) var name : String
[:Off]
}
[:End:]

It is compiled to the code:

[:Success:]
import org.eclipse.xtend.lib.annotations.Accessors
Expand All @@ -51,9 +74,31 @@ will compile to the code:
[:End:]


By default a public getter and a public setter method is created. The [:accessorsannon:] can be configured to tell
that you only want one or the other and to change the visibility. This is done by passing the visibility types
as parameters to the annotation.
By default, a public getter and a public setter method is created. The [:accessorsannon:] can be configured to tell
that you only want one or the other, and to change the visibility. This is done by passing one or more values of
type [:accessorparamtype:] for representing the visibility categories
as parameters to the annotation: [:accessorpublicgetterparam:], [:accessorprotectedgetterparam:], [:accessorpackagegetterparam:],
[:accessorprivategetterparam:], [:accessorpublicsetterparam:], [:accessorprotectedsetterparam:], [:accessorpackagesetterparam:],
[:accessorprivatesetterparam:], [:accessornoneparam:].

[:Success:]
import org.eclipse.xtend.lib.annotations.AccessorType
class MyClass {
private var t1 = [:accessorparamtype](AccessorType)::[:accessorpublicgetterparam](PUBLIC_GETTER)
private var t2 = AccessorType::[:accessorprotectedgetterparam](PROTECTED_GETTER)
private var t3 = AccessorType::[:accessorpackagegetterparam](PACKAGE_GETTER)
private var t4 = AccessorType::[:accessorprivategetterparam](PRIVATE_GETTER)

private var t5 = AccessorType::[:accessorpublicsetterparam](PUBLIC_SETTER)
private var t6 = AccessorType::[:accessorprotectedsetterparam](PROTECTED_SETTER)
private var t7 = AccessorType::[:accessorpackagesetterparam](PACKAGE_SETTER)
private var t8 = AccessorType::[:accessorprivatesetterparam](PRIVATE_SETTER)

private var t9 = AccessorType::[:accessornoneparam](NONE)
}
[:End:]


You can also use the annotation on class level to do the same for all fields.

Here is a more complex example, that shows how it works:
Expand All @@ -70,7 +115,7 @@ Here is a more complex example, that shows how it works:
[:Off]
[:End:]

will compile to the code:
It is compiled to the code:

[:Success:]
import org.eclipse.xtend.lib.annotations.Accessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,20 @@ Workflow {
bind = "io.sarl.lang.extralanguage.validator.IExtraLanguageValidatorProvider"
to = "io.sarl.lang.extralanguage.ContributionBasedExtraLanguageValidatorProvider"
}
runtime = {
bind = "org.eclipse.xtend.core.macro.ActiveAnnotationContextProvider"
to = "io.sarl.lang.macro.SarlActiveAnnotationContextProvider"
}
runtime = {
bind = "org.eclipse.xtend.core.macro.ProcessorInstanceForJvmTypeProvider"
to = "io.sarl.lang.macro.SarlProcessorInstanceForJvmTypeProvider"
}
runtime = {
bind = "org.eclipse.xtend.core.macro.declaration.CompilationUnitImpl"
to = "io.sarl.lang.macro.SarlCompilationUnitProvider"
provider = true
functionName = "configureCompilationUnitImpl"
}

ui = {
bind = "io.sarl.lang.extralanguage.IExtraLanguageContributions"
Expand Down Expand Up @@ -785,6 +799,10 @@ Workflow {
to = "io.sarl.lang.ui.editor.SARLEditorErrorTickUpdater"
functionName = "configureXtextEditorErrorTickUpdater"
}
ui = {
bind = "org.eclipse.xtend.core.macro.ProcessorInstanceForJvmTypeProvider"
to = "io.sarl.lang.ui.macro.SarlJdtProcessorInstanceForJvmTypeProvider"
}
}

// Xtend-extension bindings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ public class BindingElement {

private boolean overridePreviousDefinition;

private boolean provider;

@Override
public boolean equals(Object obj) {
if (obj instanceof BindingElement) {
Expand Down Expand Up @@ -119,6 +121,23 @@ public boolean isOverride() {
return this.overridePreviousDefinition;
}

/** Set the element is a provider.
*
* @param provider <code>true</code> if a provider.
*/
public void setProvider(boolean provider) {
this.provider = provider;
}

/** Replies if the element is a provider.
*
* @return <code>true</code> if it is a provider.
*/
@Pure
public boolean isProvider() {
return this.provider;
}

/** Set the function name.
*
* @param name the name of the binding function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,27 +268,42 @@ protected void appendTo(TargetStringConcatenation builder) {
* @param functionName the name of the binding function. It may be <code>null</code> for the default name.
* @param to the concrete type.
* @param isSingleton indicates if the instance is a singleton.
* @param isEager indicates if the instance is an eager singleton
* @param isEager indicates if the instance is an eager singleton.
* @param isProvider indicates if the binding is for a provider.
* @return the binding element.
*/
protected Binding bindToType(
TypeReference bind, String functionName, TypeReference to,
boolean isSingleton, boolean isEager) {
boolean isSingleton, boolean isEager, boolean isProvider) {
final BindKey type;
final BindValue value;
if (!Strings.isEmpty(functionName) && functionName.startsWith(CONFIGURE_PREFIX)) {
final String fname = functionName.substring(CONFIGURE_PREFIX.length());
type = new GuiceModuleAccess.BindKey(Strings.toFirstUpper(fname), null, false, false);
final StringConcatenationClient client = new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation builder) {
builder.append("binder.bind("); //$NON-NLS-1$
builder.append(bind);
builder.append(".class).to("); //$NON-NLS-1$
builder.append(to);
builder.append(".class);"); //$NON-NLS-1$
}
};
final StringConcatenationClient client;
if (isProvider) {
client = new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation builder) {
builder.append("binder.bind("); //$NON-NLS-1$
builder.append(bind);
builder.append(".class).toProvider("); //$NON-NLS-1$
builder.append(to);
builder.append(".class);"); //$NON-NLS-1$
}
};
} else {
client = new StringConcatenationClient() {
@Override
protected void appendTo(TargetStringConcatenation builder) {
builder.append("binder.bind("); //$NON-NLS-1$
builder.append(bind);
builder.append(".class).to("); //$NON-NLS-1$
builder.append(to);
builder.append(".class);"); //$NON-NLS-1$
}
};
}
value = new BindValue(null, null, false, Collections.singletonList(client));
} else {
String fname = functionName;
Expand Down Expand Up @@ -424,7 +439,8 @@ public Binding toBinding(BindingElement element) {
element.getFunctionName(),
typeReference2,
element.isSingleton(),
element.isEager());
element.isEager(),
element.isProvider());
}

}
2 changes: 2 additions & 0 deletions main/coreplugins/io.sarl.lang.ui/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Export-Package: io.sarl.lang.ide,
io.sarl.lang.ui.contentassist.javadoc,
io.sarl.lang.ui.contentassist.templates,
io.sarl.lang.ui.editor,
io.sarl.lang.ui.extralanguage,
io.sarl.lang.ui.extralanguage.compiler,
io.sarl.lang.ui.extralanguage.preferences,
io.sarl.lang.ui.extralanguage.properties,
Expand All @@ -47,6 +48,7 @@ Export-Package: io.sarl.lang.ide,
io.sarl.lang.ui.hover,
io.sarl.lang.ui.internal,
io.sarl.lang.ui.labeling,
io.sarl.lang.ui.macro,
io.sarl.lang.ui.outline,
io.sarl.lang.ui.preferences,
io.sarl.lang.ui.quickfix,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import io.sarl.lang.ui.labeling.SARLDiagnosticLabelDecorator;
import io.sarl.lang.ui.labeling.SARLHyperLinkingLabelProvider;
import io.sarl.lang.ui.labeling.SARLLabelProvider;
import io.sarl.lang.ui.macro.SarlJdtProcessorInstanceForJvmTypeProvider;
import io.sarl.lang.ui.outline.SARLBehaviorUnitOutlineFilter;
import io.sarl.lang.ui.outline.SARLFieldOutlineFilter;
import io.sarl.lang.ui.outline.SARLOperationOutlineFilter;
Expand All @@ -93,6 +94,7 @@
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;
import org.eclipse.xtend.core.macro.AbstractFileSystemSupport;
import org.eclipse.xtend.core.macro.ProcessorInstanceForJvmTypeProvider;
import org.eclipse.xtend.core.macro.declaration.IResourceChangeRegistry;
import org.eclipse.xtend.ide.XtendResourceUiServiceProvider;
import org.eclipse.xtend.ide.autoedit.AutoEditStrategyProvider;
Expand Down Expand Up @@ -538,6 +540,11 @@ public Class<? extends IImageHelper.IImageDescriptorHelper> bindIImageDescriptor
return PluginImageHelper.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings provided by SARL API]
public Class<? extends ProcessorInstanceForJvmTypeProvider> bindProcessorInstanceForJvmTypeProvider() {
return SarlJdtProcessorInstanceForJvmTypeProvider.class;
}

// contributed by io.sarl.lang.mwe2.binding.InjectionFragment2 [Bindings provided by SARL API]
public Class<? extends XtendJavaDocContentAssistProcessor> bindXtendJavaDocContentAssistProcessor() {
return SARLJavaDocContentAssistProcessor.class;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2018 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.lang.ui.macro;

import com.google.inject.Inject;
import org.eclipse.xtend.ide.macro.JdtBasedProcessorProvider;
import org.eclipse.xtext.common.types.JvmType;
import org.eclipse.xtext.xbase.typesystem.util.CommonTypeComputationServices;

import io.sarl.lang.macro.SarlProcessorInstanceForJvmTypeProvider;

/** Processor for the {@code @Accessors} active annotations.
*
* <p>This processor ensures that the visibility of the generated functions is not higher
* than the visility allowed into the containing type.
*
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
* @since 0.9
*/
public class SarlJdtProcessorInstanceForJvmTypeProvider extends JdtBasedProcessorProvider {

@Inject
private CommonTypeComputationServices services;

@Override
public Object getProcessorInstance(JvmType type) {
return super.getProcessorInstance(SarlProcessorInstanceForJvmTypeProvider.filterActiveProcessorType(type, this.services));
}

}
2 changes: 2 additions & 0 deletions main/coreplugins/io.sarl.lang/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Require-Bundle: io.sarl.lang.core;bundle-version="0.9.0";visibility:=reexport,
Bundle-Activator: io.sarl.lang.SARLLangActivator
Export-Package: io.sarl.lang,
io.sarl.lang.bugfixes.pending.bug621,
io.sarl.lang.bugfixes.pending.bug868,
io.sarl.lang.bugfixes.unpublished,
io.sarl.lang.codebuilder,
io.sarl.lang.codebuilder.appenders,
Expand All @@ -40,6 +41,7 @@ Export-Package: io.sarl.lang,
io.sarl.lang.extralanguage.validator,
io.sarl.lang.formatting2,
io.sarl.lang.jvmmodel,
io.sarl.lang.macro,
io.sarl.lang.parser.antlr,
io.sarl.lang.parser.antlr.internal,
io.sarl.lang.sarl,
Expand Down
Loading

0 comments on commit efc3585

Please sign in to comment.