"This advice advises no methods"
Answered
Here's the working code. I have the test, and the aspect is applied (see the log message). However, the IDE doesn't think so and insists on telling me "this advice advises no methods". Looks like something you JetBrains folks may be interested in
package org.example;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.example.annotations.ManageRequestRate;
import org.example.exceptions.CrptException;
import org.example.models.Item;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
@RequiredArgsConstructor
public class CrptAPI {
private final ObjectMapper objectMapper;
@ManageRequestRate
public String createDocument(Item item, String signature) {
Map<String, Object> documentMap = getDocumentMap(item, signature);
String document;
try {
document = objectMapper.writeValueAsString(documentMap);
} catch (JsonProcessingException e) {
throw new CrptException(e);
}
return document;
}
private Map<String, Object> getDocumentMap(Item item, String signature) {
Map<String, Object> documentMap = new HashMap<>();
documentMap.put(item.getClass().getSimpleName().toLowerCase(), item);
documentMap.put("signature", signature);
return documentMap;
}
}
package org.example.models;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import java.util.UUID;
@Getter
@Setter
@RequiredArgsConstructor
public class Item {
private final UUID id;
private final String productName;
private final String brand;
private final String agentTaxIdNumber;
private final String agentName;
}
package org.example.exceptions;
public class CrptException extends RuntimeException {
public CrptException(Throwable cause) {
super(cause);
}
}
package org.example.configurations;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.paramnames.ParameterNamesModule;
import org.example.requestRateManagers.RequestRateManager;
import org.example.requestRateManagers.RequestRateManagerImpl;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import java.util.concurrent.TimeUnit;
@Configuration
@ComponentScan(basePackages = "org.example")
@EnableAspectJAutoProxy
public class SpringConfig {
@Bean
public RequestRateManager requestRateManager() {
return new RequestRateManagerImpl(TimeUnit.MINUTES, 100);
}
@Bean
public ObjectMapper objectMapper() {
return new ObjectMapper().registerModule(new ParameterNamesModule());
}
}
package org.example.aspects;
import lombok.RequiredArgsConstructor;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.example.requestRateManagers.RequestRateManager;
import org.springframework.stereotype.Component;
@Component
@Aspect
@RequiredArgsConstructor
public class RequestRateAspect {
private final RequestRateManager requestRateManager;
@Pointcut("within(org.example..*) && @annotation(org.example.annotations.ManageRequestRate)")
public void annotationManageRequests() {
}
@Before("annotationManageRequests()")
public void requestRateAdvice() {
System.out.println("Running requestRateAdvice()...");
requestRateManager.ensureRequestRate();
}
}
package org.example.annotations;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ManageRequestRate {
}
package org.example;
import org.example.configurations.SpringConfig;
import org.example.models.Item;
import org.json.JSONException;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import java.util.UUID;
public class CrptAPIIntegrationTest {
private final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
@Test
public void testCreateDocument() throws JSONException {
Item item = new Item(UUID.randomUUID(), "test product name",
"test brand", "1234-5678", "test agent name");
UUID id = item.getId();
CrptAPI crptAPI = context.getBean(CrptAPI.class);
String documentJson = crptAPI.createDocument(item, "test signature");
String expectedJson = """
{
"item":
{
"id":""" + id + "," + """
"productName":"test product name",
"brand":"test brand",
"agentTaxIdNumber":"1234-5678",
"agentName":"test agent name"
},
"signature":"test signature"}
""";
JSONAssert.assertEquals(expectedJson, documentJson, JSONCompareMode.STRICT);
}
}

Please sign in to leave a comment.
Hello, this seems to be a known issue, you can vote for and follow it here: https://youtrack.jetbrains.com/issue/IDEA-132663