private static void addGeneratedBy(ProcessorContext context, CodeTypeElement generatedType, TypeElement generatedByType) { DeclaredType generatedBy = (DeclaredType) context.getType(GeneratedBy.class); // only do this if generatedBy is on the classpath. if (generatedBy != null) { CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror(generatedBy); generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(generatedByType.asType())); generatedType.addAnnotationMirror(generatedByAnnotation); } }
private void avoidFindbugsProblems(CodeTypeElement clazz) { TypeElement type = context.getEnvironment().getElementUtils().getTypeElement(SuppressFBWarnings.class.getName()); boolean foundComparison = false; outer: for (SpecializationData specialization : node.getSpecializations()) { for (GuardExpression guard : specialization.getGuards()) { if (guard.getExpression().containsComparisons()) { foundComparison = true; break outer; } } } if (foundComparison) { CodeAnnotationMirror annotation = new CodeAnnotationMirror((DeclaredType) type.asType()); annotation.setElementValue(annotation.findExecutableElement("value"), new CodeAnnotationValue("SA_LOCAL_SELF_COMPARISON")); clazz.addAnnotationMirror(annotation); } }
static CodeTypeElement createClass(Template sourceModel, TemplateMethod sourceMethod, Set<Modifier> modifiers, String simpleName, TypeMirror superType) { TypeElement templateType = sourceModel.getTemplateType(); ProcessorContext context = ProcessorContext.getInstance(); PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(templateType); CodeTypeElement clazz = new CodeTypeElement(modifiers, ElementKind.CLASS, pack, simpleName); TypeMirror resolvedSuperType = superType; if (resolvedSuperType == null) { resolvedSuperType = context.getType(Object.class); } clazz.setSuperClass(resolvedSuperType); CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror((DeclaredType) context.getType(GeneratedBy.class)); generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(templateType.asType())); if (sourceMethod != null && sourceMethod.getMethod() != null) { generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("methodName"), new CodeAnnotationValue(sourceMethod.createReferenceName())); } clazz.addAnnotationMirror(generatedByAnnotation); return clazz; }
public static CodeTypeElement createClass(Template sourceModel, TemplateMethod sourceMethod, Set<Modifier> modifiers, String simpleName, TypeMirror superType) { TypeElement templateType = sourceModel.getTemplateType(); ProcessorContext context = ProcessorContext.getInstance(); PackageElement pack = context.getEnvironment().getElementUtils().getPackageOf(templateType); CodeTypeElement clazz = new CodeTypeElement(modifiers, ElementKind.CLASS, pack, simpleName); TypeMirror resolvedSuperType = superType; if (resolvedSuperType == null) { resolvedSuperType = context.getType(Object.class); } clazz.setSuperClass(resolvedSuperType); CodeAnnotationMirror generatedByAnnotation = new CodeAnnotationMirror((DeclaredType) context.getType(GeneratedBy.class)); generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("value"), new CodeAnnotationValue(templateType.asType())); if (sourceMethod != null && sourceMethod.getMethod() != null) { generatedByAnnotation.setElementValue(generatedByAnnotation.findExecutableElement("methodName"), new CodeAnnotationValue(sourceMethod.createReferenceName())); } clazz.addAnnotationMirror(generatedByAnnotation); return clazz; }