Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[lang] Enable static and not static features into the formal paramete…
…r's default values. see #612 Signed-off-by: Stéphane Galland <galland@arakhne.org>
- Loading branch information
1 parent
af0faef
commit 6dbc11c
Showing
19 changed files
with
3,286 additions
and
95 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
103 changes: 103 additions & 0 deletions
103
main/coreplugins/io.sarl.lang/src/io/sarl/lang/jvmmodel/DefaultValueAccessDetector.java
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,103 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2021 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.jvmmodel; | ||
|
||
import java.util.List; | ||
|
||
import com.google.inject.Inject; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.xtext.EcoreUtil2; | ||
import org.eclipse.xtext.common.types.JvmConstructor; | ||
import org.eclipse.xtext.common.types.JvmExecutable; | ||
import org.eclipse.xtext.common.types.JvmField; | ||
import org.eclipse.xtext.common.types.JvmGenericType; | ||
import org.eclipse.xtext.common.types.JvmIdentifiableElement; | ||
import org.eclipse.xtext.common.types.JvmOperation; | ||
import org.eclipse.xtext.xbase.XAbstractFeatureCall; | ||
import org.eclipse.xtext.xbase.XExpression; | ||
import org.eclipse.xtext.xbase.XFeatureCall; | ||
import org.eclipse.xtext.xbase.XMemberFeatureCall; | ||
import org.eclipse.xtext.xbase.jvmmodel.ILogicalContainerProvider; | ||
|
||
/** Detector of the type of access to the default value of a formal parameter. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 0.12 | ||
*/ | ||
public class DefaultValueAccessDetector implements IDefaultValueAccessDetector { | ||
|
||
@Inject | ||
private ILogicalContainerProvider logicalContainerProvider; | ||
|
||
@Override | ||
public boolean isStaticFieldStorage(EObject sourceObject, XExpression defaultValue, JvmExecutable parameterContainer, JvmGenericType executableContainer) { | ||
if (executableContainer.isInterface()) { | ||
return true; | ||
} | ||
if (parameterContainer instanceof JvmConstructor) { | ||
return true; | ||
} | ||
if (defaultValue instanceof XFeatureCall || defaultValue instanceof XMemberFeatureCall) { | ||
return false; | ||
} | ||
final List<XAbstractFeatureCall> calls = EcoreUtil2.getAllContentsOfType(defaultValue, XAbstractFeatureCall.class); | ||
for (final XAbstractFeatureCall call : calls) { | ||
if (call instanceof XFeatureCall || call instanceof XMemberFeatureCall) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("checkstyle:nestedifdepth") | ||
public boolean isStaticAccess(JvmOperation operation) { | ||
if (!operation.isStatic() && operation.getDeclaringType() instanceof JvmGenericType) { | ||
final JvmGenericType type = (JvmGenericType) operation.getDeclaringType(); | ||
if (!type.isInterface()) { | ||
final XExpression body = this.logicalContainerProvider.getAssociatedExpression(operation); | ||
if (body != null) { | ||
final List<XAbstractFeatureCall> calls = EcoreUtil2.getAllContentsOfType(body, XAbstractFeatureCall.class); | ||
if (body instanceof XAbstractFeatureCall) { | ||
calls.add(0, (XAbstractFeatureCall) body); | ||
} | ||
for (final XAbstractFeatureCall call : calls) { | ||
if (call instanceof XMemberFeatureCall || call instanceof XFeatureCall) { | ||
final JvmIdentifiableElement feature = call.getFeature(); | ||
if (feature instanceof JvmField && !((JvmField) feature).isStatic()) { | ||
return false; | ||
} | ||
if (feature instanceof JvmOperation && !((JvmOperation) feature).isStatic()) { | ||
return false; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |
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
59 changes: 59 additions & 0 deletions
59
main/coreplugins/io.sarl.lang/src/io/sarl/lang/jvmmodel/IDefaultValueAccessDetector.java
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,59 @@ | ||
/* | ||
* $Id$ | ||
* | ||
* SARL is an general-purpose agent programming language. | ||
* More details on http://www.sarl.io | ||
* | ||
* Copyright (C) 2014-2021 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.jvmmodel; | ||
|
||
import com.google.inject.ImplementedBy; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.xtext.common.types.JvmExecutable; | ||
import org.eclipse.xtext.common.types.JvmGenericType; | ||
import org.eclipse.xtext.common.types.JvmOperation; | ||
import org.eclipse.xtext.xbase.XExpression; | ||
|
||
/** Detector of the type of access to the default value of a formal parameter. | ||
* | ||
* @author $Author: sgalland$ | ||
* @version $FullVersion$ | ||
* @mavengroupid $GroupId$ | ||
* @mavenartifactid $ArtifactId$ | ||
* @since 0.12 | ||
*/ | ||
@ImplementedBy(DefaultValueAccessDetector.class) | ||
public interface IDefaultValueAccessDetector { | ||
|
||
/** Replies if the given default value should be stored into a static field. | ||
* | ||
* @param sourceObject the source of the generation. | ||
* @param defaultValue the default value to test. | ||
* @param parameterContainer the container of the formal parameter. | ||
* @param executableContainer the container of the executable. | ||
* @return {@code true} if the default value should be stored into a static field. | ||
*/ | ||
boolean isStaticFieldStorage(EObject sourceObject, XExpression defaultValue, JvmExecutable parameterContainer, JvmGenericType executableContainer); | ||
|
||
/** Replies if the given operation is a default-value operation that contains only references to static features. | ||
* | ||
* @param operation the operation to test. | ||
* @return {@code true} if the default value should be accessed statically. | ||
*/ | ||
boolean isStaticAccess(JvmOperation operation); | ||
|
||
} |
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
Oops, something went wrong.