I want to add an inner class using Mono.Cecil
public class MyClass
{
private class InternalClass
{
}
}
How can I do that?
The solution is
internalType = new TypeDefinition(
"", "InternalClass",
TypeAttributes.NestedPrivate,
module.Import(typeof(object)));
type.NestedTypes.Add(internalType);
Related
I want to attach public method to the class.
This is called extension method in C#.
package extensionMethods
class A {
def testA() {}
//def testB() {} Need to add a public method to this class A but we don't have access to the class
}
class B {
def test() {
def a = new A();
a.testA()
a.testB() //Need to add a public method to the Class A without defining the method in the class A
}
}
//In C# way -> Extension method
class C {
/* void testB(this A a) {
}*/
}
How can we achieve the similar approach in Groovy?
In the above example I want to attach method testB() to class A
You will want something like this:
package something
class SomeExtensionClass {
static void testB(A self) {
// ...
}
}
Then your META-INF/services/org.codehaus.groovy.runtime.ExtensionModule extension descriptor...
moduleName=Test module for specifications
moduleVersion=1.0-test
extensionClasses=something.SomeExtensionClass
See http://groovy-lang.org/metaprogramming.html#_extension_modules for more info.
I have a question regarding one of the feature of mockito. On several blogs I have read that mocking constructor is not possible through mockito.
For one of my test case, currently it is done through powermockito but I want to remove it due to some performance issues.
Currently the code looks something like this:
Actual class:
public class TestClass {
private ClassB classB;
public TestClass(ClassB classB) {
this.classB = classB;
}
}
In my test class, I have code like this:
TestClass testClass = Mockito.mock(TestClass.class);
PowerMockito.whenNew(TestClass.class).withArguments(this.classB)
.thenReturn(testClass);
So could anyone suggest me, is there any other way possible by which I can achieve the same thing through mockito? Also on some blogs, I found that injection a public method with constructor of the class inside and then mocking that method can do the trick. But wanted to know all other options to analyze.
Thanks
-Sam
I am not sure if that can help you.
class MyClass {
private final MySecondClass clazz;
MyClass(MySecondClass clazz) {
this.clazz = clazz;
}
public boolean executeDoSomething() {
return clazz.doSomething();
}
}
And in the test you could mock the inner class:
#RunWith(MockitoJUnitRunner.class)
public class MyClassTest {
#Test
public void MyClassTest() {
MySecondClass mockedPerformer = Mockito.mock(MySecondClass.class);
MyClass clazz = new MyClass(mockedPerformer);
clazz.executeDoSomething();
}
}
I hope this helps you.
I am trying to override a class DefaultScreenNameValidator that implements ScreenNameValidator interface. For this , I copied the class and put it into another module. One change that I made is in annotation that is as follows:-
#Component(
property = {
"service.ranking:Integer=500"
}
)
I got a successful build using this. But when I tried to deploy the project, I got error as java.lang.NoClassDefFoundError: com/liferay/portal/kernel/security/auth/ScreenNameValidator.Can you suggest me how to eradicate this error. Thanx in advance..
I'm wondering, wouldn't it be better to instead create a module that also implements the ScreenNameValidator interface, and define your custom logic in there? Then you can just simply tell Liferay to use that validator instead of the DefaultScreenNameValidator.
For example, a minimalistic implementation:
import com.liferay.portal.kernel.security.auth.ScreenNameValidator;
import org.osgi.service.component.annotations.Component;
#Component(
immediate = true,
service = ScreenNameValidator.class
)
public class CustomScreenNameValidator implements ScreenNameValidator {
#Override
public boolean validate(long companyId, String screenName) {
// Your custom logic
}
}
make sure you have the dependency to portal-kernel in the build.gradle
dependencies {
compile 'com.liferay.portal:com.liferay.portal.kernel:2.0.0'
I made a screenNameValidator using blade-cli you can see the projet at https://github.com/bruinen/liferay-blade-samples/tree/master/liferay-workspace/modules/blade.screenname.validator
import com.liferay.portal.kernel.security.auth.ScreenNameValidator;
import org.osgi.service.component.annotations.Component;
import java.util.Locale;
#Component(
immediate = true,
property = {"service.ranking:Integer=100"},
service = ScreenNameValidator.class
)
public class CustomScreenNameValidator implements ScreenNameValidator {
#Override
public String getAUIValidatorJS() {
return "function(val) {return !(val.indexOf(\"admin\") !==-1)}";
}
#Override
public String getDescription(Locale locale) {
return "The screenName contains reserved words";
}
#Override
public boolean validate(long companyId, String screenName) {
return !screenName.contains("admin");
}
}
Normally I would just use Type.getClassName(Type.getClass(this), but obviously that doesn't work because there is no this. Any ideas?
If that is a static method, since there is no static member inheritance in Haxe, you already know what class it belongs to. So I would recommend hard-coding the class.
Or, you can use... macros!
import haxe.macro.Context;
import haxe.macro.Expr;
class ClassNameHelper {
macro static public function getClassName():ExprOf<String> {
return { expr: EConst(CString(Context.getLocalClass().toString())), pos: Context.currentPos() }
}
}
class Test {
public static function main() {
trace(ClassNameHelper.getClassName()); //Test
}
}
I create a class library to put my repositories, domain model and my DTO.
When a user call ClienteRepository.GetById(1) for exemple, it should get the Client domain model and transform into a ClientDTO to return this, example:
public class ClientRepository{
public ClientDTO GetById(int id){
var clientDto = Mapper.Map<Client, ClientDTO>(_db.Client.Find(id));
return clientDto;
}
}
the problem is that Mapper.Map doesn't work because I did not create the map (Mapper.CreateMap<Client, ClientDTO>()).
My question: How can I do this in a class library if I dont have global.asax to create it?
You don't need a Global.asax for Automapper.
It's just the better way to do mapping init for a web project.
Just put your init code in a static constructor
static MyStaticCtor()
{
//samples
//Mapper.CreateMap<AccountViewModel, Account>();
//Mapper.CreateMap<AccountSettingViewModel, AccountSetting>()
Mapper.AssertConfigurationIsValid();
}
or even, you can simply do this in the constructor of your Repository.
I solved my problem using https://github.com/davidebbo/WebActivator. Just create a new class and put this code:
[assembly: WebActivator.PostApplicationStartMethod(typeof (MapsInit), "Activate")]
namespace Database
{
public static class MapsInit
{
public static void Activate()
{
Mapper.CreateMap<ClienteDto, Cliente>();
Mapper.CreateMap<Cliente, ClienteDto>();
}
}
}