Skip to content

Commit

Permalink
Use delegate-processors (#1766)
Browse files Browse the repository at this point in the history
  • Loading branch information
pkoenig10 authored Aug 3, 2023
1 parent 813f19f commit 8905fae
Show file tree
Hide file tree
Showing 35 changed files with 1,045 additions and 1,010 deletions.
17 changes: 17 additions & 0 deletions .palantir/revapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,20 @@ acceptedBreaks:
old: "class com.palantir.tritium.metrics.registry.MetricName.Builder"
new: "class com.palantir.tritium.metrics.registry.MetricName.Builder"
justification: "MetricName.Builder is final"
"0.71.0":
com.palantir.tritium:tritium-processor:
- code: "java.method.movedToSuperClass"
old: "method com.google.common.collect.ImmutableSet<java.lang.String> com.palantir.tritium.processor.TritiumAnnotationProcessor::getSupportedAnnotationTypes()"
new: "method java.util.Set<java.lang.String> com.palantir.delegate.processors.DelegateProcessor::getSupportedAnnotationTypes()\
\ @ com.palantir.tritium.processor.TritiumAnnotationProcessor"
justification: "Migrating to delegate-processors"
- code: "java.method.nowFinalInFinalClass"
old: "method com.google.common.collect.ImmutableSet<java.lang.String> com.palantir.tritium.processor.TritiumAnnotationProcessor::getSupportedAnnotationTypes()"
new: "method java.util.Set<java.lang.String> com.palantir.delegate.processors.DelegateProcessor::getSupportedAnnotationTypes()\
\ @ com.palantir.tritium.processor.TritiumAnnotationProcessor"
justification: "Migrating to delegate-processors"
- code: "java.method.returnTypeChanged"
old: "method com.google.common.collect.ImmutableSet<java.lang.String> com.palantir.tritium.processor.TritiumAnnotationProcessor::getSupportedAnnotationTypes()"
new: "method java.util.Set<java.lang.String> com.palantir.delegate.processors.DelegateProcessor::getSupportedAnnotationTypes()\
\ @ com.palantir.tritium.processor.TritiumAnnotationProcessor"
justification: "Migrating to delegate-processors"
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,35 @@ dependencies {
}
```

Apply the `@Instrument` annotation to an interface. If the interface is defined externally, a new interface may be defined which extends the target interface, and that interface may be annotated.
Apply the `@Instrument` annotation to methods on an interface. Only the annotated methods will be instrumented.
```java
interface Service {
@Instrument
String getGreeting();
}
```

You can also apply the `@Instrument` annotation to the interface itself to instrument all methods.
```java
@Instrument
interface Service {
String getGreeting();
}
```

If the interface is defined externally, a new interface may be defined which extends the target interface. The `@Instrument` annotation may be applied to either individual methods or the interface itself.
```java
interface DelegateSupplier<T> extends Supplier<T> {
@Instrument
@Override
String get();
}
```
```java
@Instrument
interface DelegateSupplier<T> extends Supplier<T> {}
```

This generates the `InstrumentedService` wrapper implementation equivalent to the legacy dynamic proxy, with less overhead.
```java
Service interestingService = ...
Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1766.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: feature
feature:
description: The `@Instrument` annotation processor now supports applying the `@Instrument`
annotation to individual methods.
links:
- https://github.com/palantir/tritium/pull/1766
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
* When type parameters are present, the empty interface must define and pass along type parameters which
* exactly match the target interface.
*/
@Target(ElementType.TYPE)
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.SOURCE)
public @interface Instrument {}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public final class Handlers {

private static final SafeLogger log = SafeLoggerFactory.get(Handlers.class);

/**
* Returns a disabled {@link InvocationContext}. This should only be used as an argument to other {@link Handlers}
* methods.
*/
public static InvocationContext disabled() {
return DisabledHandlerSentinel.INSTANCE;
}

/**
* The caller is expected to check {@link InvocationEventHandler#isEnabled()} prior to calling this method,
* allowing argument array allocation to be avoided when the handler is not enabled.
Expand Down
11 changes: 4 additions & 7 deletions tritium-processor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ apply plugin: 'com.palantir.external-publish-jar'
dependencies {
api project(':tritium-annotations')
api project(':tritium-api')
api project(':tritium-registry')
api project(':tritium-core')
api project(':tritium-registry')

api 'com.squareup:javapoet'
api 'com.google.guava:guava'
api 'com.google.code.findbugs:jsr305'
api 'com.palantir.safe-logging:preconditions'
api 'com.palantir.goethe:goethe'
implementation 'com.google.guava:guava'
implementation 'com.palantir.delegate.processors:delegate-processors'
implementation 'com.squareup:javapoet'

testImplementation 'com.google.testing.compile:compile-testing'
testImplementation 'com.google.guava:guava'
testImplementation 'org.assertj:assertj-core'
testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.junit.jupiter:junit-jupiter-api'
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* (c) Copyright 2021 Palantir Technologies Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.tritium.processor;

import com.google.common.base.CaseFormat;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.squareup.javapoet.ArrayTypeName;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeVariableName;
import com.squareup.javapoet.WildcardTypeName;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
import javax.lang.model.element.ExecutableElement;

enum Names {
;

// Avoid prefix collisions by choosing a delimiter that does not appear in valid UTF-8
private static final byte DELIMITER = (byte) 0xff;

static String methodFieldName(ExecutableElement method) {
String upperMethodName = CaseFormat.LOWER_CAMEL
.converterTo(CaseFormat.UPPER_UNDERSCORE)
.convert(method.getSimpleName().toString());

Hasher hasher = Hashing.murmur3_32_fixed().newHasher();
Stream.concat(
Stream.of(method.getSimpleName().toString()),
method.getParameters().stream().map(parameter -> simpleName(TypeName.get(parameter.asType()))))
.forEach(value -> {
hasher.putString(value, StandardCharsets.UTF_8);
hasher.putByte(DELIMITER);
});

return upperMethodName + "_" + hasher.hash();
}

private static String simpleName(TypeName input) {
if (input.isPrimitive()) {
return input.toString();
}
if (input instanceof ClassName) {
return ((ClassName) input).simpleName();
}
if (input instanceof ParameterizedTypeName) {
ParameterizedTypeName parameterizedTypeName = (ParameterizedTypeName) input;
return simpleName(parameterizedTypeName.rawType);
}
if (input instanceof WildcardTypeName || input instanceof TypeVariableName) {
return Object.class.getSimpleName();
}
if (input instanceof ArrayTypeName) {
ArrayTypeName arrayTypeName = (ArrayTypeName) input;
return simpleName(arrayTypeName.componentType) + "[]";
}
throw new IllegalArgumentException("Unknown type-name: " + input);
}
}
Loading

0 comments on commit 8905fae

Please sign in to comment.