|
Java example source code file (PrintingProcessor.java)
The PrintingProcessor.java Java example source code/* * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ package com.sun.tools.javac.processing; import javax.annotation.processing.*; import javax.lang.model.*; import javax.lang.model.element.*; import static javax.lang.model.element.ElementKind.*; import static javax.lang.model.element.NestingKind.*; import javax.lang.model.type.*; import javax.lang.model.util.*; import java.io.PrintWriter; import java.io.Writer; import java.util.*; /** * A processor which prints out elements. Used to implement the * -Xprint option; the included visitor class is used to implement * Elements.printElements. * * <p>This is NOT part of any supported API. * If you write code that depends on this, you do so at your own risk. * This code and its internal interfaces are subject to change or * deletion without notice.</b> */ @SupportedAnnotationTypes("*") @SupportedSourceVersion(SourceVersion.RELEASE_8) public class PrintingProcessor extends AbstractProcessor { PrintWriter writer; public PrintingProcessor() { super(); writer = new PrintWriter(System.out); } public void setWriter(Writer w) { writer = new PrintWriter(w); } @Override public boolean process(Set<? extends TypeElement> tes, RoundEnvironment renv) { for(Element element : renv.getRootElements()) { print(element); } // Just print the elements, nothing more to do. return true; } void print(Element element) { new PrintingElementVisitor(writer, processingEnv.getElementUtils()). visit(element).flush(); } /** * Used for the -Xprint option and called by Elements.printElements */ public static class PrintingElementVisitor extends SimpleElementVisitor8<PrintingElementVisitor, Boolean> { int indentation; // Indentation level; final PrintWriter writer; final Elements elementUtils; public PrintingElementVisitor(Writer w, Elements elementUtils) { super(); this.writer = new PrintWriter(w); this.elementUtils = elementUtils; indentation = 0; } @Override protected PrintingElementVisitor defaultAction(Element e, Boolean newLine) { if (newLine != null && newLine) writer.println(); printDocComment(e); printModifiers(e); return this; } @Override public PrintingElementVisitor visitExecutable(ExecutableElement e, Boolean p) { ElementKind kind = e.getKind(); if (kind != STATIC_INIT && kind != INSTANCE_INIT) { Element enclosing = e.getEnclosingElement(); // Don't print out the constructor of an anonymous class if (kind == CONSTRUCTOR && enclosing != null && NestingKind.ANONYMOUS == // Use an anonymous class to determine anonymity! (new SimpleElementVisitor7<NestingKind, Void>() { @Override public NestingKind visitType(TypeElement e, Void p) { return e.getNestingKind(); } }).visit(enclosing)) return this; defaultAction(e, true); printFormalTypeParameters(e, true); switch(kind) { case CONSTRUCTOR: // Print out simple name of the class writer.print(e.getEnclosingElement().getSimpleName()); break; case METHOD: writer.print(e.getReturnType().toString()); writer.print(" "); writer.print(e.getSimpleName().toString()); break; } writer.print("("); printParameters(e); writer.print(")"); AnnotationValue defaultValue = e.getDefaultValue(); if (defaultValue != null) writer.print(" default " + defaultValue); printThrows(e); writer.println(";"); } return this; } @Override public PrintingElementVisitor visitType(TypeElement e, Boolean p) { ElementKind kind = e.getKind(); NestingKind nestingKind = e.getNestingKind(); if (NestingKind.ANONYMOUS == nestingKind) { // Print out an anonymous class in the style of a // class instance creation expression rather than a // class declaration. writer.print("new "); // If the anonymous class implements an interface // print that name, otherwise print the superclass. List<? extends TypeMirror> interfaces = e.getInterfaces(); if (!interfaces.isEmpty()) writer.print(interfaces.get(0)); else writer.print(e.getSuperclass()); writer.print("("); // Anonymous classes that implement an interface can't // have any constructor arguments. if (interfaces.isEmpty()) { // Print out the parameter list from the sole // constructor. For now, don't try to elide any // synthetic parameters by determining if the // anonymous class is in a static context, etc. List<? extends ExecutableElement> constructors = ElementFilter.constructorsIn(e.getEnclosedElements()); if (!constructors.isEmpty()) printParameters(constructors.get(0)); } writer.print(")"); } else { if (nestingKind == TOP_LEVEL) { PackageElement pkg = elementUtils.getPackageOf(e); if (!pkg.isUnnamed()) writer.print("package " + pkg.getQualifiedName() + ";\n"); } defaultAction(e, true); switch(kind) { case ANNOTATION_TYPE: writer.print("@interface"); break; default: writer.print(kind.toString().toLowerCase()); } writer.print(" "); writer.print(e.getSimpleName()); printFormalTypeParameters(e, false); // Print superclass information if informative if (kind == CLASS) { TypeMirror supertype = e.getSuperclass(); if (supertype.getKind() != TypeKind.NONE) { TypeElement e2 = (TypeElement) ((DeclaredType) supertype).asElement(); if (e2.getSuperclass().getKind() != TypeKind.NONE) writer.print(" extends " + supertype); } } printInterfaces(e); } writer.println(" {"); indentation++; if (kind == ENUM) { List<Element> enclosedElements = new ArrayList<Element>(e.getEnclosedElements()); // Handle any enum constants specially before other entities. List<Element> enumConstants = new ArrayList Other Java examples (source code examples)Here is a short list of links related to this Java PrintingProcessor.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.