I'm trying to fix bocker given by sonar for a Flex+ actionScript3 web application.
I was faced to this unresolvable blocker:
The managed event "ame" is either misspelled or is missing a companion
Event metadata tag
My code is as follows:
[Bindable]
[ManagedEvents(names="message")]
public class ClassName extends EventDispatcher
{
.........
}
I tried to fix this issue as follows:
[Bindable]
[Event(name="message",type="package.ClassEvent")]
[ManagedEvents(names="message")]
public class ClassName extends EventDispatcher
{
....
}
Where in package.ClassEvent is the declared event "message"
[Command(selector="message")]
public function message(evt:NameEvent):AsyncToken
{
.....
}
PS: Sonar suggest as solution :
The "ManagedEvents" metadata tag allows you to flag an event as being
managed. By definition this "ManageEvents" metadata tag should be used
in pair with an "Event" metadata tag.
Noncompliant Code Example
[Event(name="message", type="my.package.MyEvemt")]
[ManagedEvents("mes")] //This "mes" event is not defined with
the "Event" metadata tag public class MyClass {...}
Compliant Solution
[Event(name="message", type="my.package.MyEvemt")]
[ManagedEvents("message")] public class MyClass {...}
This is a bug in Sonar.
This ticket https://jira.sonarsource.com/browse/SONARFLEX-88 should fix your problem. While it's not fixed you can mark issue as false positive.
Related
I'm using PhpStorm and something that lately is bothering me a lot is this scenario. Suppose I have this setup:
// File1.php
abstract class AbstractBase {
public function __construct() {
}
}
// File2.php
class MyClass extends AbstractBase {
}
// File3.php
$var = new MyClass();
Now, if I'm reading the line if File3.php and want to go to MyClass definition if File2.php, the easiest way is to hold the Ctrl button and then click the MyClass name. This works very nicely for functions and member variables, and in PHPDoc comments, but in this case PhpStorm chooses to go to the constructor rather than the class definition. And since MyClass doesn't have a constructor of its own, it goes to the AbstractBase constructor in a completely different file.
I understand that in this case it's ambiguous what I want to do. I also know that I can right-click MyClass and then select Go To and then Type Declaration. But is there a way to configure Ctrl-Click that it would go to the class definition rather the constructor?
I'm trying to declare a customized ManagedEvent with ActionScript as follows:
[Bindable]
[ManagedEvents(names="message")]
public class ClassName extends EventDispatcher
{
.........
}
The problem came when sonar impposed to have a companion Event with that ManagedEvents.
I added the following Event metadata :
[Bindable]
[Event(name="message",type="package.ClassEvent")]
[ManagedEvents(names="message")]
public class ClassName extends EventDispatcher
{
....
}
But the problem still unsolved with sonar.
This is a link for sonar SonarAnalyzer for flex :
http://dist.sonarsource.com/reports/coverage/rules/flex_rules_coverage.html
where there is this rule : https://sonarqube.com/coding_rules#rule_key=flex%3AS1464
Each ManagedEvents metadata tag should have a companion Event metadata tag
I really need to fix this issue, have you an idea?
The problem was solved by removing the names attribut in ManagedEvents:
[Bindable]
[Event(name="message",type="package.ClassEvent")]
[ManagedEvents("message")]
public class ClassName extends EventDispatcher
{
....
}
Most of my components are registered using the code-based (fluent) approach, but there is one particular component that I need to resolve differently at runtime. This is the interface and a couple of concrete implementations:-
public interface ICommsService ...
public class SerialCommsService : ICommsService ...
public class TcpCommsService : ICommsService ...
Some of our users will need the serial service while others will need the TCP service. My current solution (which works btw) is to use a typed factory and a custom component selector - the latter reads an app.config setting to determine which implementation the typed factory will resolve and return.
First the typed factory (nothing special about this):-
public interface ICommsServiceFactory
{
ICommsService Create();
void Release(ICommsService component);
}
Next, the custom component selector, which reads the fully-qualified type name from app.config (e.g. "MyApp.SomeNamespace.TcpCommsService"):-
public class CommsFactoryComponentSelector : DefaultTypedFactoryComponentSelector
{
protected override string GetComponentName(MethodInfo method, object[] arguments)
{
return ConfigurationManager.AppSettings["commsServiceType"];
}
}
Then the registration stuff:-
var container = new WindsorContainer();
container.AddFacility<TypedFactoryFacility>();
container.Register(Component.For<ITypedFactoryComponentSelector>()
.ImplementedBy<CommsFactoryComponentSelector>());
container.Register(Component.For<ICommsFactory>()
.AsFactory(o => o.SelectedWith<CommsFactoryComponentSelector>()));
container.Register(Component.For<ICommsService>()
.ImplementedBy<SerialCommsService>().LifeStyle.Singleton);
container.Register(Component.For<ICommsService>()
.ImplementedBy<TcpCommsService>().LifeStyle.Singleton);
Finally, an example class with a dependency on ICommsService:-
public class Test
{
public Test(ICommsFactory commsFactory)
{
var commsService = commsFactory.Create();
...
}
}
As already mentioned, the above solution does work, but I don't like having to inject the factory. It would be more intuitive if I could just inject an ICommsService, and let something somewhere figure out which implementation to resolve and inject - similar to what I'm doing now but earlier in Windsor's "resolving pipeline". Is something like that possible?
You can use UsingFactoryMethod here:
container.Register(Component.For<ICommsService>().UsingFactoryMethod(kernel => kernel.Resolve<ICommsServiceFactory>().Create()));
You can inject ICommsService to any class now. ICommsServiceFactory can be a simple interface now:
interface ICommsServiceFactory
{
ICommsService Create();
}
For some irrelevant reasons I need a class:
that inherits (directly or not) from MovieClip.
that implements a particular interface (let's assume here that this interface is empty since it does not change anything to the issue).
and whose .as file declares internal classes.
The following code sums this up:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip implements EmptyInterface { }
}
class MyInnerClass { }
The problem with that code above is that it will not always compile. As soon as I use MyClass as Linkage for one of my library's item the compiler complains about MyClass not being a subclass of MovieClip. On the other hand, everything works great if I instantiate it manually and add it to the stage.
It looks like the interface and the inner class are somehow mutually exclusive in that very particular case. Indeed, if I remove the inner class I do not have that error anymore:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip implements EmptyInterface { }
}
Same thing when I remove the implemented interface but keep the inner class:
package {
import flash.display.MovieClip;
public class MyClass extends MovieClip { }
}
class MyInnerClass { }
Note that I've only tested this in Flash CS5.
I say it is a bug of compiler.
I have tested and found that private class must extend any class that is not Object. Instead of extending class it can also implement any interface.
This works same even if I put classes into deeper package.
I have tested this with Flash CS6.
If I'm reading you right, you want a public class to extend an internal class? - There is nothing that prevents you from doing this, so long as you declare your internal class as it's own packaged file.
According to the documentation:
[dynamic] [public | internal] [final] class className [ extends superClass ] [ implements interfaceName[, interfaceName... ] ] {
// class definition here
}
If it's the interface that is giving you grief, have you declared it in a separate file as well - that you would import? As eluded to in the comments, the namespace scoping is important so that the compiler understands what the escalating priority is.
Eg:
package my.example {
public interface EmptyInterface
{
}
}
So that:
package {
import flash.display.MovieClip;
import my.example.EmptyInterface;
public class MyClass extends MovieClip implements EmptyInterface { }
}
If this doesn't fix it I have another idea but try this first.
Click file
Click publish setting
Click on settings button
Uncheck Automatically declare stage instances
Click OK
say, Child class is inheriting Father class and Father class is inheriting spark TextArea class. now from an mxml file (in FLEX4), i am creating multiple objects of Child class. Father class have few static attributes whose values are set by private methods, calling from constructor. Now the question is: all these static attributes are set every time while Child class objects are being created one by one?
If answer is yes then Is it possible that Father class static attributes are set only once and not depends upon the number of Child class objects creation.
Please provide any suggestion or tips
Thanks in advance.
If you are setting static variables from an object's constructor or methods called from the constructor, then yes, they will be set every time. In order to prevent that, just check whether the variable is already set.
public class Foo {
public static var bar:Object;
public Foo(value:Object) {
if (!bar) {
bar = value;
}
}
}
First decide if those static members are really all that important to store as statics because statics are associated with a Class and not an instance it's usually a signal that you're probably doing something you shouldn't if instances are modifying or reading static members. You probably should use a factory method if you need to share that information with the instances. However, if you're sure you should do it then you can use a static initializer block to initialize the members when the class is loaded. Downside is that block throws an exception it can be hard to track down:
public class SomeObject {
private const _someStaticMember : String;
private const _someOtherStaticMember : SomeOtherObject;
static {
_someStaticMember = "foobar";
_someOtherStaticMember = new SomeOtherObject();
}
}