Can I check for usage of lombok.experimental.* annotations with ArchUnit?" - testing

As the question suggests, how can I check for certain imports with archUnit.
So I want the test to fail, when the tested class itself imports lombok.experimental.*.
I understand how to check for packages and stuff like that, but the approach doesnt seem to work for imports. Any suggestions?
My Code:
package com.nikita.Nikitos;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.noClasses;
import com.tngtech.archunit.core.domain.JavaClasses;
import com.tngtech.archunit.core.importer.ClassFileImporter;
public class AppTest
{
#Test
public void keineKlassenAusLombokExperimental() {
JavaClasses classes = new ClassFileImporter()
.importPackages("com.nikita..");
noClasses().should().dependOnClassesThat()
.resideInAPackage("lombok.experimental..").check(classes);
}
}
The class that I want to test:
package com.nikita.Nikitos;
import lombok.experimental.UtilityClass;
#UtilityClass
public class App
{
static int hd;
}

Lombok acts as an annotation processor that modifies your class.
In case of #lombok.experimental.UtilityClass (and probably other lombok annotations as well), the final byte code doesn't actually contain the annotation anymore:
#lombok.experimental.UtilityClass
public class App {
static int hd;
}
is compiled (transformed) to
public final class App
flags: (0x0031) ACC_PUBLIC, ACC_FINAL, ACC_SUPER
this_class: #5 // App
super_class: #6 // java/lang/Object
interfaces: 0, fields: 1, methods: 1, attributes: 1
Constant pool:
#1 = Methodref #6.#15 // java/lang/Object."<init>":()V
#2 = Class #16 // java/lang/UnsupportedOperationException
#3 = String #17 // This is a utility class and cannot be instantiated
#4 = Methodref #2.#18 // java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
#5 = Class #19 // App
#6 = Class #20 // java/lang/Object
#7 = Utf8 hd
#8 = Utf8 I
#9 = Utf8 <init>
#10 = Utf8 ()V
#11 = Utf8 Code
#12 = Utf8 LineNumberTable
#13 = Utf8 SourceFile
#14 = Utf8 App.java
#15 = NameAndType #9:#10 // "<init>":()V
#16 = Utf8 java/lang/UnsupportedOperationException
#17 = Utf8 This is a utility class and cannot be instantiated
#18 = NameAndType #9:#21 // "<init>":(Ljava/lang/String;)V
#19 = Utf8 App
#20 = Utf8 java/lang/Object
#21 = Utf8 (Ljava/lang/String;)V
{
static int hd;
descriptor: I
flags: (0x0008) ACC_STATIC
private App();
descriptor: ()V
flags: (0x0002) ACC_PRIVATE
Code:
stack=3, locals=1, args_size=1
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: new #2 // class java/lang/UnsupportedOperationException
7: dup
8: ldc #3 // String This is a utility class and cannot be instantiated
10: invokespecial #4 // Method java/lang/UnsupportedOperationException."<init>":(Ljava/lang/String;)V
13: athrow
LineNumberTable:
line 4: 0
}
which could also have been produced from this plain Java code:
public final class App {
static int hd;
private App() {
throw new UnsupportedOperationException("This is a utility class and cannot be instantiated");
}
}
If you want to detect such a pattern in the bytecode with ArchUnit, you'd probably have to reverse-engineer what Lombok does and e.g. search for private constructors in final classes that call the UnsupportedOperationException(String) constructor:
ArchRule no_UtilityClass = noConstructors()
.should().bePrivate()
.andShould().beDeclaredInClassesThat().haveModifier(JavaModifier.FINAL)
.andShould(new ArchCondition<JavaCodeUnit>("call new UnsupportedOperationException(String)") {
#Override
public void check(JavaCodeUnit codeUnit, ConditionEvents events) {
boolean satisfied = codeUnit.getCallsFromSelf().stream().anyMatch(call ->
call.getTargetOwner().isEquivalentTo(UnsupportedOperationException.class)
&& call.getName().equals(JavaConstructor.CONSTRUCTOR_NAME)
&& call.getTarget().getRawParameterTypes().size() == 1
&& call.getTarget().getRawParameterTypes().get(0).isEquivalentTo(String.class)
);
String message = String.format("%s %s `new UnsupportedOperationException(String)` in %s",
codeUnit.getDescription(), satisfied ? "calls" : "does not call", codeUnit.getSourceCodeLocation()
);
events.add(new SimpleConditionEvent(codeUnit, satisfied, message));
}
});
If you instead want to forbid the usage of lombok.experimental.* in the source code, you'll unfortunately need another tool; ArchUnit (currently) only analyzes bytecode.

An import does not generate any signature in the bytecode, so ArchUnit cannot detect it directly.
Isn't it sufficient to check that your code does not depend on that package?
ArchRule lombok_experimental_is_not_used = noClasses()
.should().dependOnClassesThat().resideInAPackage("lombok.experimental..");
If you only wanted to detect the star import, then you unfortunately need to use another tool.

Related

Around annotion executed twice using WebFlux

I'm facing a weird behaviour while using AOP with AspectJ.
Basically the #Around method its called either once either twice and while trying to debugging I can't find the reason why it's being executing twice (I mean what triggers the second execution of the method)
here is some code :
#Aspect
#Slf4j
public class ReactiveRedisCacheAspect {
#Pointcut("#annotation(com.xxx.xxx.cache.aop.annotations.ReactiveRedisCacheable)")
public void cacheablePointCut() {}
#Around("cacheablePointCut()")
public Object cacheableAround(final ProceedingJoinPoint proceedingJoinPoint) {
log.debug("ReactiveRedisCacheAspect cacheableAround.... - {}", proceedingJoinPoint);
MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
Method method = methodSignature.getMethod();
Class<?> returnTypeName = method.getReturnType();
Duration duration = Duration.ofHours(getDuration(method));
String redisKey = getKey(method, proceedingJoinPoint);
if (returnTypeName.isAssignableFrom(Flux.class)) {
log.debug("returning Flux");
return cacheRepository.hasKey(redisKey)
.filter(found -> found)
.flatMapMany(found -> cacheRepository.findByKey(redisKey))
.flatMap(found -> saveFlux(proceedingJoinPoint, redisKey, duration));
} else if (returnTypeName.isAssignableFrom(Mono.class)) {
log.debug("Returning Mono");
return cacheRepository.hasKey(redisKey)
.flatMap(found -> {
if (found) {
return cacheRepository.findByKey(redisKey);
} else {
return saveMono(proceedingJoinPoint, redisKey, duration);
}
});
} else {
throw new RuntimeException("non reactive object supported (Mono,Flux)");
}
}
private String getKey(final Method method, final ProceedingJoinPoint proceedingJoinPoint) {
ReactiveRedisCacheable annotation = method.getAnnotation(ReactiveRedisCacheable.class);
String cacheName = annotation.cacheName();
String key = annotation.key();
cacheName = (String) AspectSupportUtils.getKeyValue(proceedingJoinPoint, cacheName);
key = (String) AspectSupportUtils.getKeyValue(proceedingJoinPoint, key);
return cacheName + "_" + key;
}
}
public class AspectSupportUtils {
private static final ExpressionEvaluator evaluator = new ExpressionEvaluator();
public static Object getKeyValue(JoinPoint joinPoint, String keyExpression) {
if (keyExpression.contains("#") || keyExpression.contains("'")) {
return getKeyValue(joinPoint.getTarget(), joinPoint.getArgs(), joinPoint.getTarget().getClass(),
((MethodSignature) joinPoint.getSignature()).getMethod(), keyExpression);
}
return keyExpression;
}
private static Object getKeyValue(Object object, Object[] args, Class<?> clazz, Method method, String keyExpression) {
if (StringUtils.hasText(keyExpression)) {
EvaluationContext evaluationContext = evaluator.createEvaluationContext(object, clazz, method, args);
AnnotatedElementKey methodKey = new AnnotatedElementKey(method, clazz);
return evaluator.key(keyExpression, methodKey, evaluationContext);
}
return SimpleKeyGenerator.generateKey(args);
}
}
#Target({ElementType.METHOD})
#Retention(RetentionPolicy.RUNTIME)
#Documented
public #interface ReactiveRedisCacheable {
String key();
String cacheName();
long duration() default 1L;
}
#RestController
#RequestMapping("api/pub/v1")
public class TestRestController{
#ReactiveRedisCacheable(cacheName = "test-cache", key = "#name", duration = 1L)
#GetMapping(value = "test")
public Mono<String> getName(#RequestParam(value = "name") String name){
return Mono.just(name);
}
}
#Configuration
public class Config {
#Bean
public ReactiveRedisCacheAspect reactiveRedisCache (ReactiveRedisCacheAspect reactiveRedisCacheAspect) {
return reactiveRedisCacheAspect;
}
}
logs:
ReactiveRedisCacheAspect cacheableAround.... - {}execution(Mono com.abc.def.xxx.rest.TestRestcontroller.getName(String))
2021-06-04 15:36:23.096 INFO [fo-bff,f688025287be7e7c,f688025287be7e7c] 20060 --- [ctor-http-nio-3] c.m.s.c.a.i.ReactiveRedisCacheAspect : Returning Mono
2021-06-04 15:36:23.097 INFO [fo-bff,f688025287be7e7c,f688025287be7e7c] 20060 --- [ctor-http-nio-3] c.m.s.c.repository.CacheRepositoryImpl : searching key: (bff_pippo)
ReactiveRedisCacheAspect cacheableAround.... - {}execution(Mono com.abc.def.xxx.rest.TestRestcontroller.getName(String))
2021-06-04 15:36:23.236 INFO [fo-bff,f688025287be7e7c,f688025287be7e7c] 20060 --- [ioEventLoop-7-2] c.m.s.c.a.i.ReactiveRedisCacheAspect : Returning Mono
2021-06-04 15:36:23.236 INFO [fo-bff,f688025287be7e7c,f688025287be7e7c] 20060 --- [ioEventLoop-7-2] c.m.s.c.repository.CacheRepositoryImpl : searching key: (bff_pippo)
2021-06-04 15:36:23.250 INFO [fo-bff,f688025287be7e7c,f688025287be7e7c] 20060 --- [ioEventLoop-7-2] c.m.s.c.repository.CacheRepositoryImpl : saving obj: (key:bff_pippo) (expiresIn:3600s)
2021-06-04 15:36:23.275 INFO [fo-bff,f688025287be7e7c,f688025287be7e7c] 20060 --- [ioEventLoop-7-2] c.m.s.c.repository.CacheRepositoryImpl : saving obj: (key:bff_pippo) (expiresIn:3600s)
So far I would have expected the cacheableAround would be executed only once, but what happens its a bit weird, if the object is present on redis the method is executed only once but if is not present the method is executed twice which it doesn't make sense, moreover it should be the business logic to manage what to do inside the method.
Thanks in advance!
You did not mention whether you use native AspectJ via load- or compile-time weaving or simply Spring AOP. Because I see not #Component annotation on your aspect, it might as well be native AspectJ, unless you configure your beans via #Bean factory methods in a configuration class or XML.
Assuming that you are using full AspectJ, a common problem newbies coming from Spring AOP have, is that they are not used to the fact that AspectJ not only intercepts execution joinpoints, but also call ones. This leads to the superficial perception that the same joinpoint is intercepted twice. But in reality, it is once the method call (in the class from which the call is made) and once the method execution (in the class where the target method resides). This is easy to determine if at the beginning of your advice method you simply log the joinpoint. In your case:
System.out.println(proceedingJoinPoint);
If then on the console you see something like
call(public void org.acme.MyClass.myMethod())
execution(public void org.acme.MyClass.myMethod())
then you know what is happening.
In case you use Spring AOP, probably it is an issue with the aspect or the Redis caching behaviour that is different from your expectation.

"Pure" dispinterface marshaling

Update 2021-04-20: The code presented here is for illustration purposes only. As pointed out by Simon Mourier, for marshaling in-process of such a simple class there is no need for all the TLB shenanigans. In reality, the TLB is provided by a third-party, with the interface in question serving for callbacks.
The object calling the interface resides in another process, however, so I really do have to marshal the interface after implementing it. As demonstrating the whole inter-process flow is tedious, I opted for something simpler - in-process inter-apartment marshaling.
Suppose I have the following type library:
import "oaidl.idl";
import "ocidl.idl";
[
uuid(99CF9EB9-9B6E-4D44-B73C-6BB8FCD45B82),
version(1.0),
]
library IsThisRealMarshal
{
[
uuid(80997EA1-0144-41EC-ABCF-5FAD08D5A498),
nonextensible,
]
dispinterface IMyInterface
{
properties:
methods:
[id(1)]
void Method();
};
};
I would like to marshal IMyInterface to another apartment. Since it's a dispinterface, I would like to use the OLE marshaler for this. And so, I register the type library:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Classes\TypeLib\{99CF9EB9-9B6E-4D44-B73C-6BB8FCD45B82}]
[HKEY_CURRENT_USER\SOFTWARE\Classes\TypeLib\{99CF9EB9-9B6E-4D44-B73C-6BB8FCD45B82}\1.0]
[HKEY_CURRENT_USER\SOFTWARE\Classes\TypeLib\{99CF9EB9-9B6E-4D44-B73C-6BB8FCD45B82}\1.0\0]
[HKEY_CURRENT_USER\SOFTWARE\Classes\TypeLib\{99CF9EB9-9B6E-4D44-B73C-6BB8FCD45B82}\1.0\0\win32]
#="path\\to\\library.tlb"
And the interface (setting the proxy CLSID to that of the OLE marshaler):
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\SOFTWARE\Classes\Interface\{80997EA1-0144-41EC-ABCF-5FAD08D5A498}]
[HKEY_CURRENT_USER\SOFTWARE\Classes\Interface\{80997EA1-0144-41EC-ABCF-5FAD08D5A498}\ProxyStubClsid32]
#="{00020424-0000-0000-C000-000000000046}"
[HKEY_CURRENT_USER\SOFTWARE\Classes\Interface\{80997EA1-0144-41EC-ABCF-5FAD08D5A498}\TypeLib]
#="{99CF9EB9-9B6E-4D44-B73C-6BB8FCD45B82}"
"Version"="1.0"
And I try to marshal (error-checking omitted for brevity):
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
CComPtr<IMyInterface> object {};
object.Attach(new MyObject);
CComPtr<IGlobalInterfaceTable> git {};
git.CoCreateInstance(CLSID_StdGlobalInterfaceTable, nullptr, CLSCTX_INPROC_SERVER);
DWORD cookie = 0;
git->RegisterInterfaceInGlobal(object, __uuidof(IMyInterface), &cookie);
auto thread = std::thread([cookie]
{
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
CComPtr<IGlobalInterfaceTable> git {};
git.CoCreateInstance(CLSID_StdGlobalInterfaceTable, nullptr, CLSCTX_INPROC_SERVER);
CComPtr<IMyInterface> object {};
git->GetInterfaceFromGlobal(cookie, __uuidof(IMyInterface), (void **)&object);
});
thread.join();
Where the MyObject class implements the bare minimum COM functionality:
class MyObject : public IMyInterface
{
private:
std::atomic<ULONG> _refcount = 1;
public:
MyObject() = default;
MyObject(MyObject const &) = delete;
MyObject & operator=(MyObject const &) = delete;
HRESULT QueryInterface(const IID& riid, void** ppvObject) override
{
if (nullptr == ppvObject)
{
return E_POINTER;
}
if (riid == __uuidof(IUnknown))
{
*ppvObject = static_cast<IUnknown *>(this);
}
else if (riid == __uuidof(IDispatch))
{
*ppvObject = static_cast<IDispatch *>(this);
}
else if (riid == __uuidof(IMyInterface))
{
*ppvObject = static_cast<IMyInterface *>(this);
}
else
{
*ppvObject = nullptr;
return E_NOINTERFACE;
}
static_cast<IUnknown *>(*ppvObject)->AddRef();
return S_OK;
}
ULONG AddRef() override
{
return ++_refcount;
}
ULONG Release() override
{
auto const new_refcount = --_refcount;
if (0 == new_refcount)
{
delete this;
}
return new_refcount;
}
HRESULT GetTypeInfoCount(UINT* pctinfo) override
{
return E_NOTIMPL;
}
HRESULT GetTypeInfo(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo) override
{
return E_NOTIMPL;
}
HRESULT GetIDsOfNames(const IID& riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId) override
{
return E_NOTIMPL;
}
HRESULT Invoke(DISPID dispIdMember, const IID& riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams,
VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr) override
{
return E_NOTIMPL;
}
};
Unfortunately, the call to GetInterfaceFromGlobal fails with E_FAIL.
Debugging reveals that none of the IDispatch methods are called, only the IUnknown ones. Additionally, it appears that the E_FAIL originates from combase!CheckTypeInfo. First, this function uses ITypeInfo::GetTypeAttr to retrieve information about IMyInterface:
It then proceeds to check whether the flags TYPEFLAG_FDUAL (0x40) or TYPEFLAG_FOLEAUTOMATION (0x100) are present in the wTypeFlags field of the TYPEATTR structure:
Since neither of these flags are present (the field has the value 0x1080, and indeed the IDL doesn't mark the interface as either [oleautomation] or [dual]), the function fails with E_FAIL.
What am I doing wrong? And if the OLE marshaler indeed cannot marshal this interface, is there anything I can do apart from implementing IMarshal myself, assuming I cannot modify the IDL?
With the help of Simon Mourier's code, I managed to find the problem. The problem was that I used the PSOAInterface proxy ({00020424-0000-0000-C000-000000000046}). Since IMyInterface is not an OLE Automation interface (i.e. not marked with [oleautomation]), this rightly failed.
The solution is to use the PSDispatch proxy ({00020420-0000-0000-C000-000000000046}), which is capable of marshaling pure IDispatch interfaces.

Qt5 signal capture to slot generates runtime error "signal does not exist"

I have a program in Qt5 which simply creates and runs my class (I think it has to be a class to take advantage of the signal/slot mechanism but I'm not sure that's relevant for my specific problem):
int main(int argc, char *argv[]) {
MyApp myApp;
return myApp.run(argc, argv);
}
In the class itself, I instantiate a QGuiApplication, then try to capture its state-change signal:
class MyApp : public QObject {
public:
MyApp() { }
~MyApp() { }
int run(int argc, char *argv[]) {
QGuiApplication app(argc, argv);
connect(
app, SIGNAL(applicationStateChanged(Qt::ApplicationState)),
this, SLOT(stateChanged(Qt::ApplicationState)));
// blah blah blah
return app.exec();
}
public slots:
void stateChanged(Qt::ApplicationState newState) {
std::cout << "State changed to " << newState << '\n';
}
};
Now, from what I understand, this should connect the signal from the QGuiApplication object through to the slot function in the MyApp object. But I'm obviously doing something wrong since it's complaining at run time that the signal does not exist:
QObject::connect:
No such signal QObject::applicationStateChanged(Qt::ApplicationState)
in Prog.cpp:16
The Qobject:: in the signal name is a bit of a worry. I would have thought it would be QGuiApplication but, since I'm new at this Qt stuff, I'm unsure if this is a problem.
What is the correct way to connect that signal so that a change in the application state would result in stateChanged being called?
For signals to work, one need to use the Q_OBJECT macro at the beginning of the first private section of a QObject-derived class.
class MyApp : public QObject {
Q_OBJECT
public:
// ...
}
See Qt 5.x Docs on Q_OBJECT

jmap -permstat takes long time and hangs

We started seeing 'java.lang.OutOfMemoryError: PermGen space'. In order to findout what is held in perm space, tried running
'/usr/j2sdk1.6.0_13/bin/jmap -permstat 20476 -J-mx1280m > /tmp/permstats20476.txt &'
This command is taking long time ..... in between it gave below exception:
finding class loader instances ..252829 intern Strings occupying 30781792 bytes.
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
Finding object size using Printezis bits and skipping over...
done.
computing per loader stat ..done.
please wait.. computing liveness...................Exception in thread "Thread-1" java.lang.OutOfMemoryError: GC overhead limit exceeded
at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.readBytesFromProcess0(Native Method)
at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.access$1000(LinuxDebuggerLocal.java:51)
at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$1ReadBytesFromProcessTask.doit(LinuxDebuggerLocal.java:558)
at sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.run(LinuxDebuggerLocal.java:127)
but it is not completing ...
[svcmig2#app430 ~]$ uname -a
Linux app430... 2.6.18-194.el5 #1 SMP Tue Mar 16 21:52:39 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux
is there any other alternates to jmap ? so that i get the perm stats fast
is there any other alternates to jmap ? so that i get the perm stats fast
You can make one yourself! This is really easy with the aid of Serviceability Agent.
Here is an example for your case:
import sun.jvm.hotspot.memory.*;
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.tools.Tool;
public class PermTool extends Tool {
#Override
public void run() {
// Show PermGen object histogram
ObjectHistogram histo = new ObjectHistogram();
VM.getVM().getObjectHeap().iteratePerm(histo);
histo.print();
// List all classes in PermGen with their ClassLoaders
VM.getVM().getObjectHeap().iteratePerm(new DefaultHeapVisitor() {
#Override
public boolean doObj(Oop obj) {
if (obj instanceof InstanceKlass) {
obj.printValue();
Oop loader = ((InstanceKlass) obj).getClassLoader();
if (loader == null) {
System.out.println(" -- loaded by Bootstrap ClassLoader");
} else {
System.out.print(" -- loaded by ");
loader.printValue();
System.out.println();
}
}
return false;
}
});
}
public static void main(String[] args) {
new PermTool().start(args);
}
}
Just compile and run it using the same JDK as your target process.
Make sure jdk/lib/sa-jdi.jar is on the CLASSPATH.
You can set the JVM args: -XX:+HeapDumpOnOutOfMemoryError to get .hprof file when the oom arise, then use mat to analysis the duplicate loaded classes.
I have write a tool class with SA for your question. My JDK version is 1.8.0_151.
Here is source code:
import sun.jvm.hotspot.oops.*;
import sun.jvm.hotspot.runtime.*;
import sun.jvm.hotspot.tools.Tool;
/**
* #author duqi
* #createTime 2018/9/1 下午11:55
**/
public class PermTool extends Tool {
#Override
public void run() {
// Show PermGen object histogram
ObjectHistogram histo = new ObjectHistogram();
VM.getVM().getObjectHeap().iterate(histo);
histo.print();
// List all classes in PermGen with their ClassLoaders
VM.getVM().getObjectHeap().iterate(new DefaultHeapVisitor() {
#Override
public boolean doObj(Oop obj) {
if (obj.getKlass() instanceof InstanceKlass) {
obj.printValue();
Oop loader = ((InstanceKlass) obj.getKlass()).getClassLoader();
if (loader == null) {
System.out.println(" -- loaded by Bootstrap ClassLoader");
} else {
System.out.print(" -- loaded by ");
loader.printValue();
System.out.println();
}
}
return false;
}
});
}
public static void main(String[] args) {
new PermTool().execute(args);
}
}

SFML Input system problem

So I was porting my game engine from SDL to SFML, and now I have a problem with my input system.
Input.h
#ifndef BULLWHIP_INPUT_H
#define BULLWHIP_INPUT_H
#include
class bc_Input
{
public:
bool bm_KeyHit(sf::Key::Code key);
bool bm_KeyDown(sf::Key::Code key);
int bm_MouseX();
int bm_MouseY();
void bm_init(sf::RenderWindow app);
private:
sf::RenderWindow App;
const sf::Input& input;
};
#endif
Input.cpp
#include "Input.h"
bool bc_Input::bm_KeyDown(sf::Key::Code key)
{
return in.IsKeyDown(key)
}
bool bc_Input::bm_KeyHit(sf::Key::Code key)
{
sf::Event event;
while(App.GetEvent(event) && event.Type == sf::Event::KeyPressed)
{
switch(event.Key.Code)
{
case key: return true; break;
default:
break;
}
}
}
void bc_Input::bm_init(sf::RenderWindow app)
{
App = app;
in = App.GetInput();
}
int bc_Input::bm_MouseX()
{
return in.GetMouseX();
}
int bc_Input::bm_MouseY()
{
return in.GetMouseY();
}
I get these errors from this:
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: 'bc_Input::App' cannot appear in a constant-expression
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: '.' cannot appear in a constant-expression
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: a function call cannot appear in a constant-expression
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: ISO C++ forbids initialization of member 'input'
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: making 'input' static
C:\c++\sdl\bullwhip\lib\Bullwhip\/Input.h:15: error: invalid in-class initialization of static data member of non-integral type 'sf::Input&'
c:\program files (x86)\codeblocks\mingw\bin../lib/gcc/mingw32/4.4.0/../../../../include/SFML/System/NonCopyable.hpp:57: error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)' is private
c:\program files (x86)\codeblocks\mingw\bin../lib/gcc/mingw32/4.4.0/../../../../include/SFML/Window/Window.hpp:56: error: within this context
You're calling the copy constructor here:
void bc_Input::bm_init(sf::RenderWindow app)
{
App = app;
in = App.GetInput();
}
Note one of your error messages:
error: 'sf::NonCopyable::NonCopyable(const sf::NonCopyable&)
In order to avoid this problem, you should use an actual constructor for your sf::Input object along with an initialization list.
Alternatively, if you've already initialized a sf::RenderWindow in a higher subsystem (as is likely the case), simply changing your member variable App to a reference should do the trick.
//header
sf::RenderWindow& App;
...
//source
void bc_Input::bm_init(sf::RenderWindow& app)
{
App = app;
in = App.GetInput();
}