Path: blob/master/test/hotspot/jtreg/vmTestbase/vm/mlvm/tools/StratumAP.java
41155 views
/*1* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package vm.mlvm.tools;2425import java.io.IOException;26import java.io.Writer;27import java.util.Set;28import java.io.File;2930import javax.annotation.processing.AbstractProcessor;31import javax.annotation.processing.Filer;32import javax.annotation.processing.ProcessingEnvironment;33import javax.annotation.processing.RoundEnvironment;34import javax.annotation.processing.SupportedAnnotationTypes;35import javax.annotation.processing.SupportedSourceVersion;36import javax.lang.model.SourceVersion;37import javax.lang.model.element.Element;38import javax.lang.model.element.TypeElement;39import javax.tools.FileObject;40import javax.tools.StandardLocation;4142import nsk.share.jdi.sde.SmapGenerator;43import nsk.share.jdi.sde.SmapStratum;44import vm.mlvm.share.Stratum;45import vm.mlvm.tools.StratumAPTreeVisitor.StratumLineInfo;4647import com.sun.source.tree.CompilationUnitTree;48import com.sun.source.util.TreePath;49import com.sun.source.util.Trees;5051@SupportedAnnotationTypes("vm.mlvm.share.Stratum")52public class StratumAP extends AbstractProcessor {5354public static final String SMAP_EXT = ".smap";55private boolean verbose = false;5657private Trees trees;58@Override59public synchronized void init(ProcessingEnvironment processingEnv) {60super.init(processingEnv);61trees = Trees.instance(processingEnv);62verbose = Boolean.parseBoolean(processingEnv.getOptions().get("verbose"));63}6465public SourceVersion getSupportedSourceVersion() {66return SourceVersion.latest();67}6869@Override70public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {71if (roundEnv.processingOver()) {72return true;73}7475for (Element e : roundEnv.getElementsAnnotatedWith(Stratum.class)) {76Stratum s = e.getAnnotation(Stratum.class);77if (s == null) {78continue;79}8081TreePath tp = trees.getPath(e);8283StratumAPTreeVisitor visitor = new StratumAPTreeVisitor();84visitor.scan(tp, trees);8586String stratumName = s.stratumName();87String stratumSourceFileName = s.stratumSourceFileName();8889SmapStratum st = new SmapStratum(stratumName);90st.addFile(stratumSourceFileName);9192Set<StratumLineInfo> lines = visitor.strata.get(stratumName);93StringBuffer stratumSource = new StringBuffer();94if (lines != null) {95int curStratumLine = 1;96for (StratumLineInfo lineInfo : lines) {97for (int i = -1; i <= 1; i++)98st.addLineData(curStratumLine,99stratumSourceFileName,1001,101lineInfo.getJavaLineNum() + i,1021);103104stratumSource.append(lineInfo.getStratumSourceLine()).append("\n");105++curStratumLine;106}107}108109if (verbose) {110System.out.println("Strata:\n" + visitor.strata + "\n\nSource:\n" + stratumSource);111}112113CompilationUnitTree compUnit = tp.getCompilationUnit();114String pkgName = compUnit.getPackageName().toString();115Filer filer = processingEnv.getFiler();116117SmapGenerator gen = new SmapGenerator();118gen.addStratum(st, false);119120try {121FileObject stratumFile = filer.createResource(StandardLocation.CLASS_OUTPUT, pkgName, stratumSourceFileName, e);122123if (verbose) {124System.out.println("Writing " + stratumFile.toUri());125}126127Writer writer = stratumFile.openWriter();128try {129writer.append(stratumSource);130} finally {131writer.close();132}133} catch (IOException ioe) {134ioe.printStackTrace();135return false;136}137138String sourceFileName =139compUnit.getSourceFile().getName()140.replaceAll("^.*\\" + File.separatorChar, "");141142gen.setOutputFileName(sourceFileName);143144if (verbose) {145System.out.println(gen.getString() + "\n");146}147148String smapFileName =149sourceFileName150.replaceAll("\\..*$", "")151+ SMAP_EXT;152153try {154FileObject smapFile = filer.createResource(StandardLocation.CLASS_OUTPUT, pkgName, smapFileName, e);155156if (verbose) {157System.out.println("Writing " + smapFile.toUri());158}159160Writer writer = smapFile.openWriter();161try {162writer.append(gen.getString());163} finally {164writer.close();165}166} catch (IOException ioe) {167ioe.printStackTrace();168return false;169}170}171172return true;173}174}175176177