SpringAOP Unbound pointcut parameter error - spring-aop

The method 'GetRankList' parameter is String, i want to get the parameter by method 'LoginLog'. but the annotation '#Before' has a error,'Unbound pointcut parameter 'str''.
so what should i do? please!
following code:
#PointCut(value="execution(* *.GetRankList(String))")
public void Login(){}
#Before(value="Login()")
public void LoginLog(String str) throws Throwable{
//todo
}

Related

How take always first parameter when requested array type param in spring mvc using #RequestParam

I wrote this code.
#GetMapping("/test")
public Response search(#RequestParam String value) {
System.out.println(value);
return new Response(value)
}
Some body request like
/test?value=a&value=b&value=c
value binded a,b,c
I want always bind first parmeter. Take a, ignore b, c.
Is there way using #RequestParam?
Or have to use HttpServletRequest and parsing parameter?
In this case you can use #RequestParam List<String> value instead of #RequestParam String value, and get the first value value.get(0) ignore the rest of them
For Example
http://rentacar.com/api/v1/search?make=audi&model=A8&type=6&type=11&type=12&color=RED&color=GREY
Method
public List<Vehicle> search(
#RequestParam(value="make", required=false) String make,
#RequestParam(value="model", required=false) String model,
#RequestParam(value="type", required=false) List<String> types,
#RequestParam(value="color", required=false) List<String> colors)
{
....
}
Great question!
I wrote this code to find out how this works. I included it in the test packages.
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#ActiveProfiles("test")
public class ControllerTest {
#LocalServerPort
private int port;
private URL url;
#Autowired
private TestRestTemplate template;
#Before
public void setUp() throws Exception {
this.url = new URL("http://localhost:" + port + "/test?value=a&value=b&value=c");
}
#Test
public void getHello() throws Exception {
ResponseEntity<String> response = template.getForEntity(url.toString(),
String.class);
Assert.assertEquals(HttpStatus.OK, response.getStatusCode());
Assert.assertEquals(response.getBody(), "a");
System.out.println("response = " + response);
}
}
I then modified your code to accept an array of strings, and only pass the first element to your Response Constructor.
Notice the changes in your code in the signature and return statement.
#GetMapping("/test")
public String search(#RequestParam String[] value) {
System.out.println(value);
return new Response(value[0]);
}
With your test, you can now explore using a List type for your request param and quickly see how the behaviour has changed.

How to pass a list element as function parameter?

I have a char array: my_array={"a","b","c"}
And I need to get 1 specific element and put it as function parameter
For example: function myFunction({"b"}){}
I tried this but got an error:
unexpected character
I learned from TTCN-3 and I try this on it.
This will help you. Use this kind of code will help you to pass one parameter each time.
public class MyClass {
public static void main(String args[]) {
MyClass mc = new MyClass();
char my_array[]={'a','b','c'};
char returnValue;
for(int i=0;i<my_array.length;i++){
returnValue = mc.charReturn(my_array[i]);
System.out.println("Got it from method:"+returnValue);
}
}
public char charReturn(char fromClass){
return fromClass;
}
}

Inheriting interface in Vala - incompatible with base method

I'm trying to implement a Gtk.StyleProvider in Vala. The "base class" (in C) looks like:
GtkIconFactory * gtk_style_provider_get_icon_factory ()
GtkStyleProperties * gtk_style_provider_get_style ()
gboolean gtk_style_provider_get_style_property ()
and in VAPI:
[CCode (cheader_filename = "gtk/gtk.h")]
public interface StyleProvider {
public abstract unowned Gtk.IconFactory get_icon_factory (Gtk.WidgetPath path);
public abstract unowned Gtk.StyleProperties get_style (Gtk.WidgetPath path);
public abstract bool get_style_property (Gtk.WidgetPath path, Gtk.StateFlags state, GLib.ParamSpec pspec, GLib.Value value);
}
Where the first two methods should only return NULL according to the documentation for GtkStyleProvider.
Thus, I wrote some Vala like this:
public class DerivedStyleProvider : Gtk.StyleProvider
{
public Gtk.IconFactory? get_icon_factory (Gtk.WidgetPath path)
{
return null;
}
public Gtk.StyleProperties? get_style (Gtk.WidgetPath path)
{
return null;
}
bool get_style_property (Gtk.WidgetPath path,
Gtk.StateFlags state,
GLib.ParamSpec pspec,
out GLib.Value value)
{
return false; //TODO
}
}
I have a problem with the first two methods. If I have them as written here (with a ?), then I get the following error:
error: overriding method `DerivedStyleProvider.get_icon_factory' is incompatible
with base method `Gtk.StyleProvider.get_icon_factory': Base method expected
return type `Gtk.IconFactory', but `Gtk.IconFactory?' was provided.
public Gtk.IconFactory? get_icon_factory (Gtk.WidgetPath path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The gtk_style_provider_get_style() method is the same.
If I remove the ?, I get the following two errors per method:
error: overriding method `DerivedsStyleProvider.get_icon_factory'
is incompatible with base method `Gtk.StyleProvider.get_icon_factory': Base
method expected return type `Gtk.IconFactory', but `Gtk.IconFactory' was provided.
public Gtk.IconFactory get_icon_factory (Gtk.WidgetPath path)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
src/Preferences.vala:138.3-138.14: warning: `null' incompatible with
return type `Gtk.IconFactory`
return null;
^^^^^^^^^^^
The first error especially is a bit strange to me, as the upshot is "error: expected TYPE, got TYPE"!
Adding unowned to the first two methods still results in similar errors.
How should I implement a Gtk.StyleProvider interface in Vala?
This compiles without errors or warnings on my system (Vala 0.32.1):
public class DerivedStyleProvider : GLib.Object, Gtk.StyleProvider
{
public unowned Gtk.IconFactory get_icon_factory (Gtk.WidgetPath path)
{
// Evil cast to work around buggy declaration in VAPI file
return (Gtk.IconFactory) null;
}
public Gtk.StyleProperties get_style (Gtk.WidgetPath path)
{
// Evil cast to work around buggy declaration in VAPI file
return (Gtk.StyleProperties) null;
}
bool get_style_property (Gtk.WidgetPath path,
Gtk.StateFlags state,
GLib.ParamSpec pspec,
out GLib.Value value)
{
// I just assigned something here to make the compiler happy, you should make sure to use a correct value
value = Value (typeof (string));
return false; //TODO
}
}
I made these changes:
Derive from GLib.Object in addition to the interface.
Use unowned on the first method.
Remove the nullable from the return types.
Cast null into the actual class types. (Which is not pretty, but the problem is with the vapi file.)
Assign a dummy value to the out parameter to make compiling warning free ;)

what's the difference between object and primitive type when using matchers in EasyMock

//service to mock
public interface ServiceToMock {
public void operateDouble(Double dbValue);
public void operateCar(Car car);
}
//class under test
public class ClassUnderTest {
ServiceToMock service;
public void operateDouble(Double dbValue){
service.operateDouble(dbValue);
}
public void operateObject(Car car){
service.operateCar(car);
}
}
//unit test class
#RunWith(EasyMockRunner.class)
public class TestEasyMockMatcherUnderTest {
#TestSubject
private final ClassUnderTest easyMockMatcherUnderTest = new ClassUnderTest();
#Mock
private ServiceToMock mock;
#Test
public void testOperateCar() {
//record
mock.operateCar(EasyMock.anyObject(Car.class));
EasyMock.expectLastCall();
// replay
EasyMock.replay(mock);
//matcher here...
easyMockMatcherUnderTest.operateObject(EasyMock.anyObject(Car.class));
//easyMockMatcherUnderTest.operateObject(new Car());
// verify
EasyMock.verify(mock);
}
#Test
public void testOperateDouble() {
// record
mock.operateDouble(EasyMock.anyDouble());
EasyMock.expectLastCall();
// replay
EasyMock.replay(mock);
easyMockMatcherUnderTest.operateDouble(EasyMock.anyDouble());
// verify
EasyMock.verify(mock);
}
}
As the above code has shown, I intent to test two methods(operateDouble, operateObject). But things are kinda weird since everything runs fine in the operateDouble block while the compiler complaints an "Illegal state exception: 1 matchers expected, 2 recored." when runnig operateObject. And if commentting the method operateDouble out, the compaint goes away..So what is the difference between Double and my custom object Car, as the Double can also be considered as an object. And why does codes in operateObject runs well when commenting operateDouble method out?
EasyMock.anyDouble and EasyMock.anyObject are not meant to be used in replay mode. They are used to setup your expectations in record mode.
Use this in your first test (testOperateCar):
easyMockMatcherUnderTest.operateObject(new Car());
and something like this in your second (testOperateDouble):
easyMockMatcherUnderTest.operateDouble(1.0);
By the way, you don't need to call EasyMock.expectLastCall. It is only useful if you expect a void method to be called multiple times, for example:
mock.operateCar(EasyMock.anyObject(Car.class));
EasyMock.expectLastCall().times(3);

Creating a CLR Table Valued Function

Am trying to create a simple table valued CLR function, which takes a comma separated string as a parameter, splits it up and returns it as multiple rows
Following a few online tutorials I ended up at:
[SqlFunction(FillRowMethodName = "FillRow",TableDefinition="val nvarchar(1000)")]
public static IEnumerable SqlFunction1(SqlString val)
{
string[] splitStr = val.Value.Split(',');
return splitStr;
}
private static void FillRow(Object obj, out SqlString str)
{
object[] row = (object[])obj;
str = (string)row[0];
}
However, executing it using
select * from dbo.SqlFunction1('1,2,3,4,5')
Returns the following error
Msg 6260, Level 16, State 1, Line 1
An error occurred while getting new row from user defined Table Valued Function :
System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Object[]'.
System.InvalidCastException:
at UserDefinedFunctions.FillRow(Object obj, SqlString& str)
.
I'm no C# expert, I'm a SQL dev, but this code has worked for me in the past. The method accepts a parameterised delimiter too.
Sorry, I cannot directly answer your question.
I can't even give you a source and credit the original author of the code - suffice to say it wasn't me.
using System;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class UserDefinedFunctions
{
[SqlFunction(Name = "StringParserCLR",
FillRowMethodName = "FillRow",
TableDefinition = "string nvarchar(500)")]
public static IEnumerable StringParserCLR(SqlString str, SqlChars delimiter)
{
if (delimiter.Length == 0)
{
return new string[1] { str.Value };
}
return str.Value.Split(delimiter[0]);
}
public static void FillRow(object row, out SqlString str)
{
str = new SqlString((string)row);
}
};
You are taking a string reference and try to cast it to an object array, which causes the InvalidCastException.
Your SqlFunction1 method returns an array of strings, so the FillRow method will be called with a string reference. Cast the object reference back to string, and then create a SqlString value from it:
private static void FillRow(Object obj, out SqlString str) {
string row = (string)obj;
str = new SqlString(row);
}

Resources