AspectJ: Trace objects that their class type has been tagged with the Java annotation #logging - aspectj

I am new to aspectJ and i need to create an aspect which traces objects with the #logging annotation.
I have created a pointcut but i get a warning on the #logging annotation.
pointcut tracedObjects(Object v): target(v) && call(#logging new(..)) && !within(BasicLogger);
The warning is "no match for this type name: logging [Xlint:invalidAbsoluteTypeName]"
Any ideas why?

It looks like you need to use the absolute path to #logging, so something more along the lines of
pointcut tracedObjects(Object v): target(v) && call(#com.yourpackage.logging new(..)) && !within(BasicLogger);
Its likely you'll need to do this for BasicLogger as well.

Related

AspectJ with Akka systems

Just for reference, this question continues with question named : Error when working with AspectJ and Scala
I am trying to use AspectJ with Scala, specifically working with Akka actor systems.
I have been producing a pointcut of this type :
#Around("\"execution (* akka.actor.ActorRef.!(..)) \" + \"&& args(message, sender)\", argNames = \"(message, sender)\")")
def printSample() {
println("receive message ")
}
However an error crops up saying :
Invalid pointcut '"execution (* akka.actor.ActorRef.!(..)) " + "&& args(message, sender)", argNames = "(message, sender)")': org.aspectj.weaver.
patterns.ParserException: identifier at position 0
[error] org.aspectj.bridge.AbortException: AspectJ failed
Your pointcut is probably a copy & paste error, you quoted it:
#Around("\"execution (* akka.actor.ActorRef.!(..)) \" + \"&& args(message, sender)\", argNames = \"(message, sender)\")")
If you fix that to read
#Around(
"execution (* akka.actor.ActorRef.!(..)) && args(message, sender)",
argNames = "(message, sender)"
)
then you have the two annotation parameters you probably intended to use. But the ActorRef.!(..) with the ! still looks strange, you probably mean *. If the argument names in the advice method are identical to the ones used in args(), you can also omit that part completely and avoid boilerplate:
#Around("execution(* akka.actor.ActorRef.*(..)) && args(message, sender)")
That should do it.
Bottom line: Your problem is about basic Java annotations and AspectJ usage, not about Scala or Akka or whatever you thought was the problem.

How to check SV class property existance

I want to check if my class parent class has such property. And if yes, than access to it.
if ( $cast(this.get_parent(), agent_inst) && agent_inst != "NULL" )
if (agent_inst.vitf != "NULL")
vitf = agent_inst.vitf;
Now if agent_inst does not have vitf property, the simulator will give an error.
So how I can check if agent_inst has the vitf property?
Thanks
You have the arguments to $cast backwards; the first argument is the target variable, the second is source. The way $cast works, you should have declared agent_inst with a class type that has a vitf property. The $cast only succeeds at run-time if the source object is type compatible with the target. Your code will not compile unless agent_inst.vitf exists.

Getting error in eclipse for spock's #Shared annotation

As a try, I created a simple groovy class in eclipse and wrote a simple spock test method.
I created one object with #Shared annotation and eclipse is complaining like:
Multiple markers at this line
- Groovy:unable to resolve class Shared , unable to find class
for annotation
- Groovy:class Shared is not an annotation in #Shared
I googled a little but did not find the solution. Does anyone know why this error is occurring? Below is the sample code:
class SimpleSpockTestExampleSpec extends Specification {
#Shared
MyObject obj;
def "length of Spock's and his friends' names"()
{
expect:"Replaces when-then block"
name.size() == length
where:
name << ["zzzzz","xxx","yyy"]
length << [5,6,7]
}
}
Pease ignore the line numbers in the image.
It seems that you haven't imported appropriate package. Do you have the following statement in the code:
import spock.lang.Shared
?

Xtext linking service and derived state

I have this grammar:
Feature returns ecore::EStructuralFeature:
{Feature} name=ID ':' (fp_many?='*')? eType=[ecore::EClassifier];
And the EClass:
Class returns ecore::EClass:
{EClassClass}
name=ID (interface?=':Api')?
(BEGIN
(eStructuralFeatures+=Feature)*
(eOperations+=Operation)*
END)?;
My goal is to have a DSL for textual Ecore mm with a YAML like syntax, so I need to convert the Feature object depending on its EType to either an EAttribute or an EReference.
I have tried to hook the afterModelLinked in LazyLinker like this:
Queue<Feature> ftrs = Queues.newArrayDeque(features);
Feature f = null;
while ((f = ftrs.poll()) != null) {
if (f.getEType() == null)
continue;
if (f.getEType() instanceof EDataType) {
createEAttribute(eClazz, f);
} else if (f.getEType() instanceof EClass) {
createEReference(eClazz, f);
}
eClazz.getEStructuralFeatures().remove(f);
}
This code does converts the feature to the appropriate type but I get an error with the validation service an here the stack trace:
org.eclipse.emf.common.util.WrappedException: java.lang.NullPointerException
at org.eclipse.xtext.linking.lazy.LazyLinkingResource.getEObject(LazyLinkingResource.java:233)
at org.eclipse.xtext.resource.persistence.StorageAwareResource.getEObject(StorageAwareResource.java:124)
at org.eclipse.xtext.linking.lazy.LazyLinkingResource.doResolveLazyCrossReference(LazyLinkingResource.java:192)
at org.eclipse.xtext.linking.lazy.LazyLinkingResource.resolveLazyCrossReference(LazyLinkingResource.java:151)
at org.eclipse.xtext.linking.lazy.LazyLinkingResource.resolveLazyCrossReferences(LazyLinkingResource.java:137)
at org.eclipse.xtext.EcoreUtil2.resolveLazyCrossReferences(EcoreUtil2.java:528)
at org.eclipse.xtext.validation.ResourceValidatorImpl.resolveProxies(ResourceValidatorImpl.java:163)
at org.eclipse.xtext.validation.ResourceValidatorImpl.validate(ResourceValidatorImpl.java:75)
at org.eclipse.xtext.ui.editor.validation.ValidationJob$1.exec(ValidationJob.java:91)
at org.eclipse.xtext.ui.editor.validation.ValidationJob$1.exec(ValidationJob.java:1)
at org.eclipse.xtext.util.concurrent.CancelableUnitOfWork.exec(CancelableUnitOfWork.java:26)
at org.eclipse.xtext.resource.OutdatedStateManager.exec(OutdatedStateManager.java:121)
at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.internalReadOnly(XtextDocument.java:520)
at org.eclipse.xtext.ui.editor.model.XtextDocument$XtextDocumentLocker.readOnly(XtextDocument.java:492)
at org.eclipse.xtext.ui.editor.model.XtextDocument.readOnly(XtextDocument.java:133)
at org.eclipse.xtext.ui.editor.validation.ValidationJob.createIssues(ValidationJob.java:86)
at org.eclipse.xtext.ui.editor.validation.ValidationJob.run(ValidationJob.java:67)
at org.eclipse.core.internal.jobs.Worker.run(Worker.java:54)
Caused by: java.lang.NullPointerException
at org.eclipse.xtext.linking.impl.ImportedNamesAdapter.find(ImportedNamesAdapter.java:34)
at org.eclipse.xtext.linking.impl.DefaultLinkingService.getImportedNamesAdapter(DefaultLinkingService.java:95)
at com.eacg.dsl.faml.linker.FamlLinkingService.getImportedNamesAdapter(FamlLinkingService.java:53)
at org.eclipse.xtext.linking.impl.DefaultLinkingService.registerImportedNamesAdapter(DefaultLinkingService.java:86)
at org.eclipse.xtext.linking.impl.DefaultLinkingService.registerImportedNamesAdapter(DefaultLinkingService.java:90)
at org.eclipse.xtext.linking.impl.DefaultLinkingService.registerImportedNamesAdapter(DefaultLinkingService.java:80)
at org.eclipse.xtext.linking.impl.DefaultLinkingService.getScope(DefaultLinkingService.java:58)
at com.eacg.dsl.faml.linker.FamlLinkingService.getScope(FamlLinkingService.java:47)
at org.eclipse.xtext.linking.impl.DefaultLinkingService.getLinkedObjects(DefaultLinkingService.java:119)
at org.eclipse.xtext.linking.lazy.LazyLinkingResource.getEObject(LazyLinkingResource.java:250)
at org.eclipse.xtext.linking.lazy.LazyLinkingResource.getEObject(LazyLinkingResource.java:225)
When debugging I found that it's still using in context the object Feature even if I have removed it when I created the mapping.
My question is this: How to safely replace the object Feature without corrupting my model. I've also tried to implement IDerivedStateComputer but got the some error.
I think the underlying problem here is that EMF is a graph-based format; classes can be features of other classes, arguments to operations, etc. In general, this graph of relations can contain loops, cycles and knots. So anything that tries to modify things in-place is going to be tricky, requiring a full-blown graph traversal algorithm to make sure you don't change something depended on by something you haven't processed yet.
The alternative is to let the model load and link in it's native form, and then transform it as a single pass. This is the way xcore implements things; the equivalent declaration is:
XClass:
{XClass}
(annotations+=XAnnotation)*
((abstract?='abstract'? 'class') | interface?= 'interface') name = ID
('<' typeParameters+=XTypeParameter (',' typeParameters+=XTypeParameter)* '>')?
('extends' superTypes+=XGenericType (',' superTypes+=XGenericType)*)?
('wraps' instanceType=JvmTypeReference) ?
'{'
(members+=XMember)*
'}'
;
Note all the X's, those are local model classes. Then later on, there is just a function:
protected EClass getEClass(final XClass xClass)

Scala - unbound wildcard exception (Play Framework 2.3 Template)

I am using Play Framework 2.3 I am using the scala template engine to create my views and Java elsewhere.
My model extends an abstract parameterised object like so... (pseudo code)
Abstract object:
public abstract class MyObject<T> {
// various bits
public class MyInnerObject {
// more stuff
}
}
Model object (singleton)
public class SomeModel extends MyObject<SomeBean> {
public static SomeModel getInstance() {
if (instance == null)
instance = new SomeModel();
return instance;
}
// more bits
}
I then pass the model to the view from another view helper:
#MyHelper(SomeModel.getInstance())
MyHelper scala view template:
#*******************************************
* My helper
*******************************************#
#(myObj: some.namespace.MyObject[_])
#import some.namespace.MyObject
#doSomething(myInnerObj: MyObject[_]#MyInnerObject) = {
#* do some stuff *#
}
#for(myInnerObj <- myObj.getInnerObjects()) {
#doSomething(myInnerObj)
}
However I get an error on the line #doSomething(myInnerObj: MyObject[_]#MyInnerObject) stating
unbound wildcard exception
I am not sure the correct Scala syntax to avoid this error I had naively assumed that I could use the _ to specify arbitrary tyope but it won't let me do this.
What is the correct syntax?
UPDATE 1
Changing the method definition to:
#doSomething[T](myInnerObj: MyObject[T]#MyInnerObject)
gives further errors:
no type parameters for method doSomething: (myInnerObj:[T]#MyInnerObject)play.twirl.api.HtmlFormat.Appendable exist so that it can be applied to arguments (myObj.MyInnerObject)
--- because ---
argument expression's type is not compatible with formal parameter type;
found : myObj.MyInnerObject
required: MyObject[?T]#MyInnerObject
It would seem that the Twirl templating engine does not support this syntax currently, although I'm not 100% sure.
I can solve the problem by removing the doSomething method completely...
#*******************************************
* My helper
*******************************************#
#(myObj: some.namespace.MyObject[_])
#import some.namespace.MyObject
#for(myInnerObj <- myObj.getInnerObjects()) {
<div>#myInnerObj.getSomeProperty()</div>
}
But I am bout 10% happy with the solution... It works at least but it feels very restricting that I cannot delegate to methods to help keep my code maintainable. By the look of the comments the problem seems to be a limitation in Twirl, not allowing type arguments for functions in views.
Note: I have accepted this answer as it removes the original problem of the exception however this is only because the solution I want doesn't exist... yet.