Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,13 @@ private static Function<CallSiteSpecification, CallSiteResult> generateAdviceClo
return spec -> {
final CallSiteResult result = adviceGenerator.generate(spec);
if (result.isSuccess()) {
Extension.EXTENSIONS.stream()
.filter(ext -> ext.appliesTo(spec))
.forEach(
ext -> {
try {
ext.apply(configuration, result);
} catch (final Throwable e) {
result.addError(e, ErrorCode.EXTENSION_ERROR, ext.getClass());
}
});
Extension.EXTENSIONS.stream().filter(ext -> ext.appliesTo(spec)).forEach(ext -> {
try {
ext.apply(configuration, result);
} catch (final Throwable e) {
result.addError(e, ErrorCode.EXTENSION_ERROR, ext.getClass());
}
});
}
return result;
};
Expand All @@ -74,17 +71,15 @@ private static List<CallSiteSpecification> searchForCallSites(final Configuratio
final SpecificationBuilder builder = specificationBuilder();
final List<CallSiteSpecification> result = new ArrayList<>();
final Pattern pattern = Pattern.compile(".*" + configuration.suffix + "\\.class$");
Files.walkFileTree(
configuration.classesFolder,
new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
if (Files.isRegularFile(file)
&& pattern.matcher(file.getFileName().toString()).matches()) {
builder.build(file.toFile()).ifPresent(result::add);
}
return FileVisitResult.CONTINUE;
}
});
Files.walkFileTree(configuration.classesFolder, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) {
if (Files.isRegularFile(file)
&& pattern.matcher(file.getFileName().toString()).matches()) {
builder.build(file.toFile()).ifPresent(result::add);
}
return FileVisitResult.CONTINUE;
}
});
return result;
} catch (IOException e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,33 +206,28 @@ private void addAdviceLambda(
type = "AROUND";
writeAdviceMethodCall(spec, adviceBody);
}
body.addStatement(
new MethodCallExpr()
.setScope(new NameExpr("container"))
.setName("addAdvice")
.setArguments(
new NodeList<>(
new FieldAccessExpr()
.setScope(
new TypeExpr(new ClassOrInterfaceType().setName(ADVICE_TYPE_CLASS)))
.setName(type),
new StringLiteralExpr(pointCut.getOwner().getInternalName()),
new StringLiteralExpr(pointCut.getMethodName()),
new StringLiteralExpr(pointCut.getMethodType().getDescriptor()),
advice)));
body.addStatement(new MethodCallExpr()
.setScope(new NameExpr("container"))
.setName("addAdvice")
.setArguments(new NodeList<>(
new FieldAccessExpr()
.setScope(new TypeExpr(new ClassOrInterfaceType().setName(ADVICE_TYPE_CLASS)))
.setName(type),
new StringLiteralExpr(pointCut.getOwner().getInternalName()),
new StringLiteralExpr(pointCut.getMethodName()),
new StringLiteralExpr(pointCut.getMethodType().getDescriptor()),
advice)));
}

private static void addHelpersInvocation(final Type[] helpers, final BlockStmt body) {
if (helpers != null && helpers.length > 0) {
final List<Expression> helperTypes =
Arrays.stream(helpers)
.map(type -> new StringLiteralExpr(type.getClassName()))
.collect(Collectors.toList());
body.addStatement(
new MethodCallExpr()
.setScope(new NameExpr("container"))
.setName("addHelpers")
.setArguments(new NodeList<>(helperTypes)));
final List<Expression> helperTypes = Arrays.stream(helpers)
.map(type -> new StringLiteralExpr(type.getClassName()))
.collect(Collectors.toList());
body.addStatement(new MethodCallExpr()
.setScope(new NameExpr("container"))
.setName("addHelpers")
.setArguments(new NodeList<>(helperTypes)));
}
}

Expand All @@ -246,11 +241,10 @@ private static void addEnabledCheck(
final List<Expression> parameters =
enabled.getArguments().stream().map(StringLiteralExpr::new).collect(Collectors.toList());

final Expression enabledCheckExpression =
new MethodCallExpr()
.setScope(new NameExpr(ownerPackage + "." + ownerClassName))
.setName(method.getMethodName())
.setArguments(new NodeList<>(parameters));
final Expression enabledCheckExpression = new MethodCallExpr()
.setScope(new NameExpr(ownerPackage + "." + ownerClassName))
.setName(method.getMethodName())
.setArguments(new NodeList<>(parameters));

type.addMethod("isEnabled", PUBLIC)
.setType(boolean.class)
Expand All @@ -263,21 +257,17 @@ private static void writeStackOperations(final AdviceSpecification advice, final
final boolean instanceMethod = !advice.isStaticPointcut();
final AllArgsSpecification allArgsSpec = advice.findAllArguments();
if (allArgsSpec == null && advice.isPositionalArguments()) {
final List<Expression> parameterIndicesValues =
advice
.getArguments()
.map(argSpec -> intLiteral(argSpec.getIndex()))
.collect(Collectors.toList());
final VariableDeclarator parameterIndices =
new VariableDeclarator()
.setName("parameterIndices")
.setType(new ArrayType(new PrimitiveType(PrimitiveType.Primitive.INT)))
final List<Expression> parameterIndicesValues = advice
.getArguments()
.map(argSpec -> intLiteral(argSpec.getIndex()))
.collect(Collectors.toList());
final VariableDeclarator parameterIndices = new VariableDeclarator()
.setName("parameterIndices")
.setType(new ArrayType(new PrimitiveType(PrimitiveType.Primitive.INT)))
.setInitializer(new ArrayCreationExpr()
.setElementType(int.class)
.setInitializer(
new ArrayCreationExpr()
.setElementType(int.class)
.setInitializer(
new ArrayInitializerExpr()
.setValues(new NodeList<>(parameterIndicesValues))));
new ArrayInitializerExpr().setValues(new NodeList<>(parameterIndicesValues))));
body.addStatement(new VariableDeclarationExpr().addVariable(parameterIndices));
final MethodCallExpr dupMethod = new MethodCallExpr().setScope(new NameExpr("handler"));
if (advice.includeThis()) {
Expand Down Expand Up @@ -310,10 +300,9 @@ private static void writeStackOperations(final AdviceSpecification advice, final
mode = "APPEND_ARRAY";
}
}
dupMethod.addArgument(
new FieldAccessExpr()
.setScope(new TypeExpr(new ClassOrInterfaceType().setName(STACK_DUP_MODE_CLASS)))
.setName(mode));
dupMethod.addArgument(new FieldAccessExpr()
.setScope(new TypeExpr(new ClassOrInterfaceType().setName(STACK_DUP_MODE_CLASS)))
.setName(mode));
body.addStatement(dupMethod);
}
}
Expand All @@ -322,53 +311,49 @@ private static void writeAdviceMethodCall(
final AdviceSpecification advice, final BlockStmt body) {
final MethodType method = advice.getAdvice();
if (advice instanceof AroundSpecification && advice.isInvokeDynamic()) {
final Expression newHandle =
new ObjectCreationExpr()
.setType("Handle")
.addArgument(opCode("H_INVOKESTATIC"))
.addArgument(new StringLiteralExpr(method.getOwner().getInternalName()))
.addArgument(new StringLiteralExpr(method.getMethodName()))
.addArgument(new StringLiteralExpr(method.getMethodType().getDescriptor()))
.addArgument(new BooleanLiteralExpr(false));
final MethodCallExpr invokeDynamic =
new MethodCallExpr()
.setScope(new NameExpr("handler"))
.setName("invokeDynamic")
.addArgument(new NameExpr("name"))
.addArgument(new NameExpr("descriptor"))
.addArgument(newHandle)
.addArgument(new NameExpr("bootstrapMethodArguments"));
final Expression newHandle = new ObjectCreationExpr()
.setType("Handle")
.addArgument(opCode("H_INVOKESTATIC"))
.addArgument(new StringLiteralExpr(method.getOwner().getInternalName()))
.addArgument(new StringLiteralExpr(method.getMethodName()))
.addArgument(new StringLiteralExpr(method.getMethodType().getDescriptor()))
.addArgument(new BooleanLiteralExpr(false));
final MethodCallExpr invokeDynamic = new MethodCallExpr()
.setScope(new NameExpr("handler"))
.setName("invokeDynamic")
.addArgument(new NameExpr("name"))
.addArgument(new NameExpr("descriptor"))
.addArgument(newHandle)
.addArgument(new NameExpr("bootstrapMethodArguments"));
body.addStatement(invokeDynamic);
} else {
if (advice.isInvokeDynamic() && advice.findInvokeDynamicConstants() != null) {
// we should add the boostrap method constants before the method call
final MethodCallExpr loadConstantArray =
new MethodCallExpr()
.setScope(new NameExpr("handler"))
.setName("loadConstantArray")
.addArgument(new NameExpr("bootstrapMethodArguments"));
final MethodCallExpr loadConstantArray = new MethodCallExpr()
.setScope(new NameExpr("handler"))
.setName("loadConstantArray")
.addArgument(new NameExpr("bootstrapMethodArguments"));
body.addStatement(loadConstantArray);
}
final MethodCallExpr invokeStatic =
new MethodCallExpr()
.setScope(new NameExpr("handler"))
.setName("advice")
.addArgument(new StringLiteralExpr(method.getOwner().getInternalName()))
.addArgument(new StringLiteralExpr(method.getMethodName()))
.addArgument(new StringLiteralExpr(method.getMethodType().getDescriptor()));
final MethodCallExpr invokeStatic = new MethodCallExpr()
.setScope(new NameExpr("handler"))
.setName("advice")
.addArgument(new StringLiteralExpr(method.getOwner().getInternalName()))
.addArgument(new StringLiteralExpr(method.getMethodName()))
.addArgument(new StringLiteralExpr(method.getMethodType().getDescriptor()));
body.addStatement(invokeStatic);
}
if (requiresCast(advice)) {
final MethodType pointcut = advice.getPointcut();
final Type expectedReturn =
pointcut.isConstructor() ? pointcut.getOwner() : pointcut.getMethodType().getReturnType();
final Type expectedReturn = pointcut.isConstructor()
? pointcut.getOwner()
: pointcut.getMethodType().getReturnType();
if (!expectedReturn.equals(method.getMethodType().getReturnType())) {
body.addStatement(
new MethodCallExpr()
.setScope(new NameExpr("handler"))
.setName("instruction")
.addArgument(opCode("CHECKCAST"))
.addArgument(new StringLiteralExpr(expectedReturn.getInternalName())));
body.addStatement(new MethodCallExpr()
.setScope(new NameExpr("handler"))
.setName("instruction")
.addArgument(opCode("CHECKCAST"))
.addArgument(new StringLiteralExpr(expectedReturn.getInternalName())));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,9 @@ public AnnotationVisitor visitParameterAnnotation(
if (parameterCtor != null) {
ParameterSpecification parameterSpec = parameterCtor.build();
if (parameterSpec instanceof ArgumentSpecification) {
final long index =
parameters.values().stream()
.filter(it -> it instanceof ArgumentSpecification)
.count();
final long index = parameters.values().stream()
.filter(it -> it instanceof ArgumentSpecification)
.count();
((ArgumentSpecification) parameterSpec)
.setIndex((int) index); // can change in annotation visitor
}
Expand All @@ -273,13 +272,9 @@ public void visit(final String key, final Object value) {

@Override
public void visitEnd() {
adviceData.forEach(
(adviceCtor, list) ->
list.stream()
.map(
data ->
adviceCtor.build(advice, parameters, data.signature, data.invokeDynamic))
.forEach(spec.advices::add));
adviceData.forEach((adviceCtor, list) -> list.stream()
.map(data -> adviceCtor.build(advice, parameters, data.signature, data.invokeDynamic))
.forEach(spec.advices::add));
}
}

Expand Down
Loading
Loading