Android Open Source - ascent Font Processor






From Project

Back to project page ascent.

License

The source code is released under:

Apache License

If you think the Android project ascent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.jamesbaca.ascent.internal;
// w w w . j a v a 2s  . c om
import com.google.auto.service.AutoService;
import com.google.common.collect.Sets;

import net.jamesbaca.ascent.Font;
import net.jamesbaca.ascent.InjectedAscent;

import java.io.IOException;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.TypeElement;
import javax.annotation.processing.Messager;
import javax.lang.model.util.Elements;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic;

/**
 * Created by jamesbaca on 9/27/14.
 */
@AutoService(Processor.class)
public final class FontProcessor extends AbstractProcessor {

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment environment) {
        long startTime = System.currentTimeMillis();

        for (TypeElement annotation : annotations) {
            write(classesWithFieldsAnnotatedWith(environment.getElementsAnnotatedWith(annotation)));
        }

        long elapsedTime = System.currentTimeMillis() - startTime;
        messager().printMessage(Diagnostic.Kind.NOTE,
                "AscentProcessor took " + elapsedTime + " milliseconds");
        return true;
    }

    @Override public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    @Override public Set<String> getSupportedAnnotationTypes() {
        return Sets.newHashSet(Font.class.getName());
    }

    private void write(Map<EnclosingClass, Collection<AnnotatedField>> fieldsByEnclosingClass) {
        WriterFactory
                writerFactory = new WriterFactory(elementUtils(), typeUtils(), filer(), InjectedAscent.SUFFIX);

        for (EnclosingClass enclosingClass : fieldsByEnclosingClass.keySet()) {
            try {
                writerFactory.writeClass(enclosingClass)
                        .withFields(fieldsByEnclosingClass.get(enclosingClass));
            } catch (IOException e) {
                messager().printMessage(Diagnostic.Kind.ERROR,
                        "Error generating helper for class " + enclosingClass.getClassName()
                                + ". Reason: " + e.getMessage());
            }
        }
    }

    private Map<EnclosingClass, Collection<AnnotatedField>> classesWithFieldsAnnotatedWith(
            Set<? extends Element> annotatedElements) {
        return new AnnotationsConverter(messager(), elementUtils(), typeUtils())
                .convert(annotatedElements);
    }

    private Messager messager() {
        return processingEnv.getMessager();
    }

    private Elements elementUtils() {
        return processingEnv.getElementUtils();
    }

    private Types typeUtils() {
        return processingEnv.getTypeUtils();
    }

    private Filer filer() {
        return processingEnv.getFiler();
    }
}




Java Source Code List

net.jamesbaca.ascent.Ascent.java
net.jamesbaca.ascent.FontHelper.java
net.jamesbaca.ascent.Font.java
net.jamesbaca.ascent.InjectedAscent.java
net.jamesbaca.ascent.demo.ApplicationTest.java
net.jamesbaca.ascent.demo.MainActivity.java
net.jamesbaca.ascent.internal.AnnotatedField.java
net.jamesbaca.ascent.internal.AnnotationsConverter.java
net.jamesbaca.ascent.internal.EnclosingClass.java
net.jamesbaca.ascent.internal.FontClassWriter.java
net.jamesbaca.ascent.internal.FontProcessor.java
net.jamesbaca.ascent.internal.WriterFactory.java
net.jamesbaca.ascent.mymodule.app.ApplicationTest.java
net.jamesbaca.ascent.mymodule.app.DemoApplicationModule.java
net.jamesbaca.ascent.mymodule.app.DemoApplication.java
net.jamesbaca.ascent.mymodule.app.MainActivity.java