@Override public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) { if (!super.isAvailable(project, editor, file)) { return false; } BindingContext context = KotlinCacheManager.getInstance(project).getDeclarationsFromProject().getBindingContext(); AnnotationDescriptor annotationDescriptor = context.get(BindingContext.ANNOTATION, annotationEntry); if (annotationDescriptor == null) { return false; } annotationType = annotationDescriptor.getType(); DeclarationDescriptor declarationDescriptor = annotationType.getConstructor().getDeclarationDescriptor(); if (declarationDescriptor == null) { return false; } PsiElement declaration = BindingContextUtils.descriptorToDeclaration(context, declarationDescriptor); if (declaration instanceof JetClass) { annotationClass = (JetClass) declaration; return annotationClass.isWritable(); } return false; }
@Nullable public AnnotationDescriptor resolveAnnotation( @NotNull JavaAnnotation javaAnnotation, @NotNull PostponedTasks postponedTasks) { final AnnotationDescriptor annotation = new AnnotationDescriptor(); FqName fqName = javaAnnotation.getFqName(); if (fqName == null) { return null; } // Don't process internal jet annotations and jetbrains NotNull annotations if (fqName.asString().startsWith("jet.runtime.typeinfo.") || fqName.equals(JETBRAINS_NOT_NULL_ANNOTATION) || fqName.equals(JvmAnnotationNames.KOTLIN_CLASS) || fqName.equals(JvmAnnotationNames.KOTLIN_PACKAGE)) { return null; } AnnotationDescriptor mappedClassDescriptor = JavaToKotlinClassMap.getInstance().mapToAnnotationClass(fqName); if (mappedClassDescriptor != null) { return mappedClassDescriptor; } final ClassDescriptor annotationClass = classResolver.resolveClass(fqName, INCLUDE_KOTLIN_SOURCES, postponedTasks); if (annotationClass == null) { return null; } postponedTasks.addTask( new Runnable() { @Override public void run() { annotation.setAnnotationType(annotationClass.getDefaultType()); } }); for (JavaAnnotationArgument argument : javaAnnotation.getArguments()) { CompileTimeConstant value = argumentResolver.resolveAnnotationArgument(fqName, argument, postponedTasks); if (value == null) continue; Name name = argument.getName(); ValueParameterDescriptor descriptor = DescriptorResolverUtils.getAnnotationParameterByName( name == null ? DEFAULT_ANNOTATION_MEMBER_NAME : name, annotationClass); if (descriptor != null) { annotation.setValueArgument(descriptor, value); } } return annotation; }
@Nullable private static String getMessageFromAnnotationDescriptor( @NotNull AnnotationDescriptor descriptor) { ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(descriptor.getType()); if (classDescriptor != null) { ValueParameterDescriptor parameter = DescriptorResolverUtils.getAnnotationParameterByName( DEFAULT_ANNOTATION_MEMBER_NAME, classDescriptor); if (parameter != null) { CompileTimeConstant<?> valueArgument = descriptor.getValueArgument(parameter); if (valueArgument != null) { Object value = valueArgument.getValue(); if (value instanceof String) { return String.valueOf(value); } } } } return null; }
private void renderAnnotations(@NotNull Annotated annotated, @NotNull StringBuilder builder) { if (!modifiers.contains(Modifier.ANNOTATIONS)) return; for (AnnotationDescriptor annotation : annotated.getAnnotations()) { ClassDescriptor annotationClass = (ClassDescriptor) annotation.getType().getConstructor().getDeclarationDescriptor(); assert annotationClass != null; if (!excludedAnnotationClasses.contains( DescriptorUtils.getFQName(annotationClass).toSafe())) { builder.append(renderType(annotation.getType())); if (verbose) { builder .append("(") .append( StringUtil.join(DescriptorUtils.getSortedValueArguments(annotation, this), ", ")) .append(")"); } builder.append(" "); } } }
public void serialize(AnnotationDescriptor annotation) { new TypeSerializer(sb).serialize(annotation.getType()); sb.append("("); serializeCommaSeparated(annotation.getValueArguments()); sb.append(")"); }