How can I statically import methods in code generated by Xtend active annotations? - xtend

I would like to use a statically imported method in code generated with Xtend's active annotations. As an example, I want to get this output:
import static java.util.Collections.emptyList;
#MyActiveAnnotation
public class MyTest {
public void foo() {
emptyList();
}
}
My compilation participant looks like this:
override doTransform(MutableClassDeclaration clazz, extension TransformationContext context) {
clazz.addMethod("foo", [
body = '''
«Collections».emptyList();
])
While this code works and imports Collections, it doesn't statically import the emptyList method.
How can I statically import methods using Xtend's active annotations?

According to Xtend developers, this doesn't seem possible.

Related

How to use mockito's matcher to call specified method when anyInt() not worked

I am new to mockito. when I use it with junit, I found anyInt() not working, the example code is as following:
import org.junit.Test;
import org.mockito.Mockito;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class MockitoDemo {
#Test
public void verify_doB_method_invoked() {
MockitoAnyIntMatcherDemo mockitoAnyIntMatcherDemo = Mockito.mock(MockitoAnyIntMatcherDemo.class);
when(mockitoAnyIntMatcherDemo.doA(anyInt())).thenReturn(1);
verify(mockitoAnyIntMatcherDemo, times(1)).doB(anyInt());
}
}
class MockitoAnyIntMatcherDemo {
public int doA(int a) {
return doB(a);
}
public int doB(int b) {
return b;
}
}
Any help would be appreciated。
TLDR: You never called doA in your test.
when method is used for stubbing - it is like recording expected calls and answers to them.
You successfully stubbed doA method - you recorded the expectation: "if doA is called in the test with any int argument, then return 1".
As you never called doA in your test, verify rightfully reports it was not called.
On top of that - in the example you provided you mock the object under test.
This is not what mocking is typically used for.

Is there any equivalent to ScalaTest's withClue in Junit5 /Hamcrest?

I would like to provide the same textual message for a group of assertions, something like
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
#Test
public void myTest() {
MyClass result = ...
withClue("the result {} does not conform", result.toString()) {
assertThat(result.id, notEmpty())
assertThat(result.xxxx, hasLength(33))
}
}
My expectation is that the "clue" will be shown before the first failed assertion. In Scala I'm used to ScalaTest withClue() that I usually use to show the full text representation of object (usually a JsonNode in my case) which usually allows me to understand better what went wrong.
So is there any way with vanilla JUnit, hamcrest of any other library dependency to get a message prepended to a group of assertions?

Ignoring invoking internal static call

public static ResponseBean call(Bean bean) throws Exception {
// statements...
IgnoreCall.ignoreMethodCall(bean);
// statements...
// return
}
With the code snippet above, is it possible to test the method ignoring invocation of IgnoreCall.ignoreMethod(Bean) without needing to place the entire statement under a boolean condition?
Here's the unit test code snippet:
#RunWith(PowerMockRunner.class)
#PrepareTest
public ClassHelperTest {
#Test
public void testCall() throws Excpetion {
// stubbing...
ResponseBean responseBean = ClassHelper.call(bean);
// verify/ies
// assert/s
}
}
Notes:
Refactoring ClassHelper.call(Bean) should be avoided. Even with a bad OO design, refactoring is costly.
Method signature is locked unless another pattern is applicable for replacement.
Tried using Mockito.when and PowerMockito.when on the target static method, stubbing didn't work on run-time debug.
As your comments indicate that changing your production code is not possible, you "simply" have to dive into the static-mocking aspects of PowerMock; as outlined here for example.
Basically you need to enable IgnoreCall for static mocking; and then you make calls to ignoreMethodCall() a no-op.
But as you keep asking: the core problem with your question is the fact that you want to mock out a static method that is void. I have a complete example below, but before that some explanations.
The point is: you call a method for two reasons:
It has a side effect
It returns a value, and maybe, causes a side effect, too
A void method can only be called for side effects. And the thing is: when you do static mocking, then that works on class level.
Meaning: you instruct PowerMock to "prevent" any of the static methods of some class from execution; you simply "erase" the side effects of all those static methods! So, by telling PowerMock to do those static mocks, all void methods are already "gone".
But as said, you might also call methods for their return value. And then is when the when() method of Mockito kicks in. You use that method to say: when that value-returning method is invoked, then do this or that.
Long story short; here is a [mcve] using the elements you asked for:
package ghostcat.test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
class IgnoreCall {
public static void ignoreMethodCall(Object o) {
System.out.println("SHOULD NOT SHOW UP: " + o);
}
}
class CuT {
public static Object call(Object bean) {
System.out.println("statement1");
IgnoreCall.ignoreMethodCall(bean);
System.out.println("statement2");
return "whatever";
}
}
#RunWith(PowerMockRunner.class)
#PrepareForTest(IgnoreCall.class)
public class PMTest {
#Test
public void test() {
PowerMockito.mockStatic(IgnoreCall.class);
CuT.call("yeha");
}
}
As in your example ... there is IgnoreCall; used within that a static method that I just called "call".
This prints:
statement1
statement2
When I go in and comment out
// PowerMockito.mockStatic(IgnoreCall.class);
It prints:
statement1
SHOULD NOT SHOW UP: yeha
statement2
So, a simple example that should tell you exactly what you need to do.
I worked with eclipse neon, IBM java8 JDK, and simply imported all the JARs from powermock-mockito-junit-1.6.6.zip into my test project.

Reading all classes under a package or reading classes with same Metadata in Actionscript 3.0

I am doing an Actionscript 3.0 project which involves introspection. I am wondering if there is a way to get all the classes within a given package structure.
For e.g. Say there are three as3 classes:
com.example.test.classOne
com.example.test.classTwo
com.example.test.classThree
I want to be able to say
getClassesUnderPackageName("com.example.test");
and get back
"com.example.test::classOne"
"com.example.test::classTwo"
"com.example.test::classThree".
Is there a way to do that?
If this is not possible, is there a way to read classes which have the same metadata?
E.g. If all the mentioned classes have the same metadata [MetadataName(type="example")] defined, is there a way to say
getClassesWithSameMetadata("MetadataName");
and get back
"com.example.test::classOne"
"com.example.test::classTwo"
"com.example.test::classThree".
Thank you.
you can use flash.utils.describeType to return XML data containing this information. it works differently on base classes, like flash.display.Sprite, but for custom classes/directories, you can write something like this:
package branchA.branchB.branchC
{
//Imports
import flash.utils.describeType;
//Class
public class Test
{
//Constructor
public function Test()
{
trace(describeType(this).#name);
}
}
}
//OUTPUT: branchA.branchB.branchC::Test
if you wanted to return the base class, you could write something like this:
package
{
//Imports
import flash.display.Sprite;
import flash.utils.describeType;
//Class
public class Test extends Sprite
{
//Constructor
public function Test()
{
trace(describeType(this).#base);
}
}
}
//OUTPUT: flash.display::Sprite
there is lots of other useful information you can get by parsing the returned XML data of describeType.
Update:
class objects do not need to have been instantiated first in order to retrieve their information via describeType(). you could build a public static function (or whatever) which accepts an array of your class objects and returns an array of strings containing the required describeType data.
something like this:
package
{
import flash.utils.describeType;
final public class Describe
{
public static function packageNames(classObjects:Array):Vector.<String>
{
var names:Vector.<String> = new Vector.<String>();
for each (var classObject in classObjects)
names.push(describeType(classObject.#name.toString()));
return names;
}
}
}
then from anywhere in your program, you can pass an array of all the classes like this:
var names:Vector.<String> = Describe.packageNames(new Array(classOne, classTwo, classThree));
trace(names);
//Output:
//com.example.test::classOne
//com.example.test::classTwo
//com.example.test::classThree
There's no inbuilt mechanism for finding classes without already knowing the class name. :(
However if you load in a SWF as a ByteArray then it's possible to iterate through the classes in it.
This might be overkill for what you want.
http://www.bytearray.org/?p=175
Take a look at AS3 Commons Bytecode. It allows you to do Bytecode based reflection. You can list all classes (you'll need to filter those if you just want a particular package), list classes with certain metadata or classes that implement a certain interface.

making a static/class function outside class

how to make a class function with actionscript, I need to have a few static tool functions that are easily used from other classes like
test = tools.rtrim(xx);
e.g. this does not compile:
package com.my.tools
{
static function rtrim(string:String):String {
return string.replace(/\s+$/,"");
}
}
It needs to be attached to a class type not a package
Try
package com.my.tools
{
public class Tools
{
public static function rtrim(string:String):String
{
return string.replace(/\s+$/,"");
}
}
}
You can then use it through Tools.rtrim("yourString");
In case your collection of tools gets big it may be useful to use top-level functions as well.
Specially if you want to reuse a tiny selection of your "tools" in other projects without wasting file size by compiling the unused ones (which happens if you include them all in the same class).
In order to do so, in your package folder you will have to create one file per function. Each file should be named the same way as its related function.
e.g. the content of each file named rtrim.as would look like this :
package com.my.tools {
public function rtrim(str:String) : String {
return string.replace(/\s+$/,"");
}
}
Then you will just have to import the top-level function where you need it :
package my {
import com.my.tools.rtrim;
public class Test
{
rtrim("bla bla");
}
}