Skip to content

Commit

Permalink
Reimplement annotation literals without private APIs (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
basil authored Apr 25, 2023
1 parent f4f6f8c commit 2e41b8a
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
<artifactId>annotation-indexer</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package com.cloudbees.sdk.extensibility;

import java.lang.annotation.Annotation;
import java.lang.reflect.Proxy;
import java.util.Collections;
import java.util.Map;
import org.apache.commons.lang3.AnnotationUtils;

/**
* Factory for annotation objects.
Expand All @@ -41,4 +44,26 @@ public static <T extends Annotation> T of(Class<T> type, Object value) {
public static <T extends Annotation> T of(Class<T> type, String key, Object value) {
return of(type, Collections.singletonMap(key, value));
}

public static <T extends Annotation> T of(Class<T> type, final Map<String, Object> values) {
return type.cast(Proxy.newProxyInstance(type.getClassLoader(), new Class<?>[] {type}, (proxy, method, args) -> {
Annotation annotation = (Annotation) proxy;
String methodName = method.getName();
switch (methodName) {
case "equals":
return AnnotationUtils.equals(annotation, (Annotation) args[0]);
case "toString":
return AnnotationUtils.toString(annotation);
case "hashCode":
return AnnotationUtils.hashCode(annotation);
case "annotationType":
return type;
default:
if (!values.containsKey(methodName)) {
throw new NoSuchMethodException("Missing value for annotation key: " + methodName);
}
return values.get(methodName);
}
}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2023, CloudBees Inc.
*
* 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.cloudbees.sdk.extensibility;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;

import javax.inject.Named;
import org.junit.Test;

/**
* @author Basil Crow
*/
public class AnnotationLiteralTest {
@Test
public void smokes() {
Named a = AnnotationLiteral.of(Named.class, "cat");
assertEquals("@javax.inject.Named(value=cat)", a.toString());
assertEquals(Named.class, a.annotationType());

Named cat = Cat.class.getAnnotation(Named.class);
assertEquals(a, cat);
assertEquals(a.hashCode(), cat.hashCode());

Named dog = Dog.class.getAnnotation(Named.class);
assertNotEquals(a, dog);
assertNotEquals(a.hashCode(), dog.hashCode());
}
}

0 comments on commit 2e41b8a

Please sign in to comment.