This question already has answers here:
Groovy, get enclosing function's name?
(5 answers)
Closed 8 years ago.
Is it possible to get the name of the current method in Groovy code?
def myMethod() {
// want to get the string myMethod here
}
Thanks
Tim_yates gives you a link to general solution.
This is shorter way to get method name. May be optimized, leawed as is for the sake of clarity.
class A {
def myMethod() {
for (m in new Throwable().stackTrace) {
if (m.className == this.class.name)
return m.methodName
}
}
}
println new A().myMethod()
Related
This question already has answers here:
How do I make the first letter of a string uppercase in JavaScript?
(96 answers)
Closed 2 years ago.
I want to define a property on the string constructor, and I want then to access this property from any file in my project. for example:
file1.js
String.capitalize = function(){
this.//...etc
}
file2.js
"Hello nice to meet you".capitalize()
You can define a new String prototype function like in the example below
String.prototype.capitalize = function(){
return this.toUpperCase();
};
console.log("Hello nice to meet you".capitalize());
This question already has answers here:
Retrofit 2 example tutorial but GsonConverterFactory display error "Cannot resolve symbol"
(7 answers)
Closed 5 months ago.
I followed this example https://futurestud.io/tutorials/oauth-2-on-android-with-retrofit
public static <S> S createService(
Class<S> serviceClass, final String authToken) {
if (!TextUtils.isEmpty(authToken)) {
AuthenticationInterceptor interceptor =
new AuthenticationInterceptor(authToken);
if (!httpClient.interceptors().contains(interceptor)) {
httpClient.addInterceptor(interceptor);
builder.client(httpClient.build());
retrofit = builder.build();
}
}
return retrofit.create(serviceClass);
}
but at retrofit = builder.build() I got cannot resolve symbol. Please help.
Sometimes when you download projects from a version control system the files get locked, so in my case to solve this i had to uncheck the "only read" box then apply and accept.
This question already has answers here:
ES6 modules: Export single class of static methods OR multiple individual methods
(2 answers)
Closed 4 years ago.
due to my learning, I usually use static class methods and OOP more than pure function and functional programming. Some of my work mates dont understand why and I dont know wich way is better. This is a piece of code for example:
const DynamoDbHelper = class DynamoDbHelper {
static getTableProperties(tableName) {
...
}
static async updateTableReadAndWriteCapacities(tableName, readCapacityUnits, writeCapacityUnits) {
...
}
}
module.exports.DynamoDbHelper = DynamoDbHelper;
can also be write:
module.exports.getTableProperties = (tableName) => {
...
}
module.exports.updateTableReadAndWriteCapacities = async(tableName, readCapacityUnits, writeCapacityUnits) => {
...
}
Wich solution is the better in this case?
Static-only class is antipattern in JavaScript. It's valid only in languages that don't support functions as independent entities.
If a class isn't intended to be instantiated and acts as a namespace, this is what modules are for. The second snippet is how this should be done.
This question already has answers here:
Mockito: InvalidUseOfMatchersException
(8 answers)
Closed 5 years ago.
I have a question what about Argument Matcher.
class A(){
public B method(Class T,String str){}
}
I Stub the method and want pass the method.but .
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
A a = new A();
B b = new B();
Mockito.doReturn(b).when(a).method(argThat(new IsClass)), "111");
Class IsClass:
class IsClass extends ArgumentMatcher<Class> {
public boolean matches(Object obj) {
return true;
}
}
So, how should I do, can pass this method.Thankyou.
The full exception message should tell you what is wrong:
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
So, if your isClass() were a valid ArgumentMatcher then you would stub like this:
Mockito.doReturn(b).when(a).method(argThat(new IsClass()), eq("111"));
//note how the second parameter of method uses the argument matcher
//"eq" rather than the raw string "111"
Moreover, if you merely want to match "any class object" you can do it like this without having to write your own custom matcher:
Mockito.doReturn(b).when(a).method(any(Class.class), eq("111"));
Finally, you can only stub for mocks. So your test would have to include some setup code like this:
A a = Mockito.mock(A.class);
B b = new B();
Mockito.doReturn(...
Consider spending some time reading through the documentation to get a better handle of how to test using Mockito.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
For Instance, I have a class like this:
class firstOne{
....
def A (){
}
}
class secondOne{
// I need to call and use method A from class firstOne
// even I get error if I try to follow Java like calls
// firstOne method = new firstOne();
// method.A()
}
I already tried http://groovy.codehaus.org/Scripts+and+Classes and http://groovy.codehaus.org/Groovy+Beans but no way. Any kind of suggestion or examples would be really helpful.
I don't see any problem in this:
class FirstOne {
def a() {
println "a"
}
}
class SecondOne {
def b() {
new FirstOne().a()
println "b"
}
}
new FirstOne().a()
println("")
new SecondOne().b()
Output:
a
a
b
This is not specific to Groovy/Grails:
firstOne first = new firstOne()
first.A()
Also you should capitalize the first letter of classes, but not methods (as is best practice in Java).