private CodeAnnotationMirror createUnusedAnnotationMirror() { CodeAnnotationMirror mirror = new CodeAnnotationMirror(unusedAnnotation); mirror.setElementValue(mirror.findExecutableElement("value"), new CodeAnnotationValue("unused")); return mirror; }
private CodeAnnotationMirror createExplodeLoop() { DeclaredType explodeLoopType = context.getDeclaredType(ExplodeLoop.class); CodeAnnotationMirror explodeLoop = new CodeAnnotationMirror(explodeLoopType); DeclaredType loopExplosionKind = context.getDeclaredType(ExplodeLoop.LoopExplosionKind.class); if (loopExplosionKind != null) { VariableElement kindValue = ElementUtils.findVariableElement(loopExplosionKind, "FULL_EXPLODE_UNTIL_RETURN"); if (kindValue != null) { explodeLoop.setElementValue(ElementUtils.findExecutableElement(explodeLoopType, "kind"), new CodeAnnotationValue(kindValue)); } } return explodeLoop; }
private static CodeVariableElement createNodeChild(ProcessorContext context, TypeMirror type, String name) { CodeVariableElement var = new CodeVariableElement(ElementUtils.modifiers(Modifier.PRIVATE), type, name); var.addAnnotationMirror(new CodeAnnotationMirror((DeclaredType) context.getType(Child.class))); return var; }
private CodeVariableElement createNodeField(Modifier visibility, TypeMirror type, String name, Class<?> annotationType) { CodeVariableElement childField = new CodeVariableElement(modifiers(), type, name); childField.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(annotationType))); setVisibility(childField.getModifiers(), visibility); return childField; }
private CodeTypeElement generateWrapperAndFactory(ProcessorContext context, Element e) { CodeTypeElement wrapper = generateWrapper(context, e, false); if (wrapper == null) { return null; } CodeTypeElement factory = generateFactory(context, e, wrapper); // add @SuppressWarnings("deprecation") DeclaredType suppressWarnings = context.getDeclaredType(SuppressWarnings.class); CodeAnnotationMirror suppressWarningsAnnotation = new CodeAnnotationMirror(suppressWarnings); suppressWarningsAnnotation.setElementValue(ElementUtils.findExecutableElement(suppressWarnings, "value"), new CodeAnnotationValue(Arrays.asList(new CodeAnnotationValue("deprecation")))); factory.getAnnotationMirrors().add(suppressWarningsAnnotation); wrapper.getModifiers().add(Modifier.STATIC); factory.add(wrapper); assertNoErrorExpected(e); return factory; }
private CodeAnnotationMirror createUnusedAnnotationMirror() { CodeAnnotationMirror mirror = new CodeAnnotationMirror(unusedAnnotation); mirror.setElementValue(mirror.findExecutableElement("value"), new CodeAnnotationValue("unused")); return mirror; }
private Element createFastPathExecuteMethod(SpecializationData specialization, ExecutableTypeData executedType, List<ExecutableTypeData> allTypes) { LocalContext currentLocals = LocalContext.load(this, executedType, Integer.MAX_VALUE); CodeExecutableElement executable = createExecuteMethod(specialization, executedType, currentLocals, false, Integer.MAX_VALUE); CodeTreeBuilder builder = executable.createBuilder(); if (specialization == null) { if (executedType.getDelegatedTo() == null) { executable.getModifiers().add(ABSTRACT); } } else { executable.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(Override.class))); } builder.tree(createFastPath(builder, specialization, executedType, allTypes, currentLocals)); return executable; }
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); } }
@Override public Void visitExecutable(CodeExecutableElement e, Void p) { if (!e.getModifiers().contains(Modifier.STATIC) && !e.getModifiers().contains(Modifier.PRIVATE)) { String name = e.getSimpleName().toString(); TypeMirror[] params = e.getParameterTypes(); for (AnnotationMirror mirror : e.getAnnotationMirrors()) { if (ElementUtils.typeEquals(overrideType, mirror.getAnnotationType())) { // already declared (may happen if method copied from super class) return super.visitExecutable(e, p); } } if (isDeclaredMethodInSuperType(e.getEnclosingClass(), name, params)) { e.addAnnotationMirror(new CodeAnnotationMirror(overrideType)); } } return super.visitExecutable(e, p); }
private static void setFieldCompilationFinal(CodeVariableElement field, int dimensions) { if (field.getModifiers().contains(Modifier.FINAL) && dimensions <= 0) { // no need for the compilation final annotation. return; } CodeAnnotationMirror annotation = new CodeAnnotationMirror(ProcessorContext.getInstance().getDeclaredType(CompilationFinal.class)); if (dimensions > 0 || field.getType().getKind() == TypeKind.ARRAY) { annotation.setElementValue(annotation.findExecutableElement("dimensions"), new CodeAnnotationValue(dimensions < 0 ? 0 : dimensions)); } field.getAnnotationMirrors().add(annotation); }
@Override public Void visitExecutable(CodeExecutableElement e, Void p) { if (!e.getModifiers().contains(Modifier.STATIC) && !e.getModifiers().contains(Modifier.PRIVATE)) { String name = e.getSimpleName().toString(); TypeMirror[] params = e.getParameterTypes(); for (AnnotationMirror mirror : e.getAnnotationMirrors()) { if (ElementUtils.typeEquals(overrideType, mirror.getAnnotationType())) { // already declared (may happen if method copied from super class) return super.visitExecutable(e, p); } } if (isDeclaredMethodInSuperType(e.getEnclosingClass(), name, params)) { e.addAnnotationMirror(new CodeAnnotationMirror(overrideType)); } } return super.visitExecutable(e, p); }
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); } }
private static CodeVariableElement createNodeField(Modifier visibility, TypeMirror type, String name, Class<?> annotationClass, Modifier... modifiers) { CodeVariableElement childField = new CodeVariableElement(modifiers(modifiers), type, name); if (annotationClass != null) { if (annotationClass == CompilationFinal.class) { setFieldCompilationFinal(childField, 0); } else { childField.getAnnotationMirrors().add(new CodeAnnotationMirror(ProcessorContext.getInstance().getDeclaredType(annotationClass))); } } setVisibility(childField.getModifiers(), visibility); return childField; }
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; }
private Element createCreatePolymorphic(Map<SpecializationData, CodeTypeElement> generatedSpecializationClasses) { SpecializationData polymorphic = node.getPolymorphicSpecialization(); CodeTypeElement generatedPolymorphic = generatedSpecializationClasses.get(polymorphic); if (generatedPolymorphic == null) { return null; } TypeMirror returnType = getType(SpecializationNode.class); CodeExecutableElement method = new CodeExecutableElement(modifiers(PROTECTED, FINAL), returnType, "createPolymorphic"); method.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(Override.class))); method.createBuilder().startReturn().tree(createCallCreateMethod(polymorphic, null, null)).end(); return method; }
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; }
private Element createGetSuppliedChildrenMethod() { ArrayType nodeArray = context.getEnvironment().getTypeUtils().getArrayType(getType(Node.class)); CodeExecutableElement method = new CodeExecutableElement(modifiers(PROTECTED, FINAL), nodeArray, "getSuppliedChildren"); method.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(Override.class))); CodeTreeBuilder builder = method.createBuilder(); builder.startReturn().tree(createGetSuppliedChildren()).end(); return method; }
public CodeTypeElement create() { Modifier visibility = ElementUtils.getVisibility(node.getTemplateType().getModifiers()); TypeMirror nodeFactory = ElementUtils.getDeclaredType(ElementUtils.fromTypeMirror(context.getType(NodeFactory.class)), node.getNodeType()); CodeTypeElement clazz = GeneratorUtils.createClass(node, null, modifiers(), factoryClassName(node), null); if (visibility != null) { clazz.getModifiers().add(visibility); } clazz.getModifiers().add(Modifier.FINAL); if (createdFactoryElement != null) { clazz.getImplements().add(nodeFactory); CodeAnnotationMirror supressWarnings = new CodeAnnotationMirror(context.getDeclaredType(SuppressWarnings.class)); supressWarnings.setElementValue(supressWarnings.findExecutableElement("value"), new CodeAnnotationValue(Arrays.asList(new CodeAnnotationValue("unchecked"), new CodeAnnotationValue("rawtypes")))); clazz.getAnnotationMirrors().add(supressWarnings); clazz.add(createNodeFactoryConstructor()); clazz.add(createCreateGetNodeClass()); clazz.add(createCreateGetExecutionSignature()); clazz.add(createCreateGetNodeSignatures()); clazz.add(createCreateNodeMethod()); clazz.add(createGetInstanceMethod(visibility)); clazz.add(createInstanceConstant(clazz.asType())); createFactoryMethods(clazz); } return clazz; }
private CodeVariableElement createSingleton(CodeTypeElement clazz) { CodeVariableElement field = new CodeVariableElement(modifiers(PUBLIC, STATIC, FINAL), clazz.asType(), singletonName(typeSystem)); field.createInitBuilder().startNew(clazz.asType()).end(); CodeAnnotationMirror annotationMirror = new CodeAnnotationMirror((DeclaredType) context.getType(Deprecated.class)); field.getAnnotationMirrors().add(annotationMirror); return field; }
private Element createCreateFallback(Map<SpecializationData, CodeTypeElement> generatedSpecializationClasses) { SpecializationData fallback = node.getGenericSpecialization(); if (fallback == null) { return null; } CodeTypeElement generatedType = generatedSpecializationClasses.get(fallback); if (generatedType == null) { return null; } TypeMirror returnType = getType(SpecializationNode.class); CodeExecutableElement method = new CodeExecutableElement(modifiers(PROTECTED, FINAL), returnType, "createFallback"); method.getAnnotationMirrors().add(new CodeAnnotationMirror(context.getDeclaredType(Override.class))); method.createBuilder().startReturn().tree(createCallCreateMethod(fallback, null, null)).end(); return method; }