src/share/classes/com/sun/tools/javap/TypeAnnotationWriter.java

Tue, 17 Dec 2013 10:55:59 +0100

author
jlahoda
date
Tue, 17 Dec 2013 10:55:59 +0100
changeset 2413
fe033d997ddf
parent 1521
71f35e4b93a5
child 2525
2eb010b6cb22
permissions
-rw-r--r--

8029800: Flags.java uses String.toLowerCase without specifying Locale
Summary: Introducing StringUtils.toLowerCase/toUpperCase independent on the default locale, converting almost all usages of String.toLowerCase/toUpperCase to use the new methods.
Reviewed-by: jjg, bpatel

jjg@1521 1 /*
jjg@1521 2 * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
jjg@1521 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1521 4 *
jjg@1521 5 * This code is free software; you can redistribute it and/or modify it
jjg@1521 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1521 7 * published by the Free Software Foundation. Oracle designates this
jjg@1521 8 * particular file as subject to the "Classpath" exception as provided
jjg@1521 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1521 10 *
jjg@1521 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1521 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1521 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1521 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1521 15 * accompanied this code).
jjg@1521 16 *
jjg@1521 17 * You should have received a copy of the GNU General Public License version
jjg@1521 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1521 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1521 20 *
jjg@1521 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1521 22 * or visit www.oracle.com if you need additional information or have any
jjg@1521 23 * questions.
jjg@1521 24 */
jjg@1521 25 package com.sun.tools.javap;
jjg@1521 26
jjg@1521 27 import com.sun.tools.classfile.Attribute;
jjg@1521 28 import com.sun.tools.classfile.Code_attribute;
jjg@1521 29 import com.sun.tools.classfile.TypeAnnotation;
jjg@1521 30 import com.sun.tools.classfile.TypeAnnotation.Position;
jjg@1521 31 import com.sun.tools.classfile.Instruction;
jjg@1521 32 import com.sun.tools.classfile.Method;
jjg@1521 33 import com.sun.tools.classfile.RuntimeInvisibleTypeAnnotations_attribute;
jjg@1521 34 import com.sun.tools.classfile.RuntimeTypeAnnotations_attribute;
jjg@1521 35 import com.sun.tools.classfile.RuntimeVisibleTypeAnnotations_attribute;
jjg@1521 36 import java.util.ArrayList;
jjg@1521 37 import java.util.HashMap;
jjg@1521 38 import java.util.List;
jjg@1521 39 import java.util.Map;
jlahoda@2413 40 import com.sun.tools.javac.util.StringUtils;
jjg@1521 41
jjg@1521 42 /**
jjg@1521 43 * Annotate instructions with details about type annotations.
jjg@1521 44 *
jjg@1521 45 * <p><b>This is NOT part of any supported API.
jjg@1521 46 * If you write code that depends on this, you do so at your own risk.
jjg@1521 47 * This code and its internal interfaces are subject to change or
jjg@1521 48 * deletion without notice.</b>
jjg@1521 49 */
jjg@1521 50 public class TypeAnnotationWriter extends InstructionDetailWriter {
jjg@1521 51 public enum NoteKind { VISIBLE, INVISIBLE };
jjg@1521 52 public static class Note {
jjg@1521 53 Note(NoteKind kind, TypeAnnotation anno) {
jjg@1521 54 this.kind = kind;
jjg@1521 55 this.anno = anno;
jjg@1521 56 }
jjg@1521 57 public final NoteKind kind;
jjg@1521 58 public final TypeAnnotation anno;
jjg@1521 59 }
jjg@1521 60
jjg@1521 61 static TypeAnnotationWriter instance(Context context) {
jjg@1521 62 TypeAnnotationWriter instance = context.get(TypeAnnotationWriter.class);
jjg@1521 63 if (instance == null)
jjg@1521 64 instance = new TypeAnnotationWriter(context);
jjg@1521 65 return instance;
jjg@1521 66 }
jjg@1521 67
jjg@1521 68 protected TypeAnnotationWriter(Context context) {
jjg@1521 69 super(context);
jjg@1521 70 context.put(TypeAnnotationWriter.class, this);
jjg@1521 71 annotationWriter = AnnotationWriter.instance(context);
jjg@1521 72 classWriter = ClassWriter.instance(context);
jjg@1521 73 }
jjg@1521 74
jjg@1521 75 public void reset(Code_attribute attr) {
jjg@1521 76 Method m = classWriter.getMethod();
jjg@1521 77 pcMap = new HashMap<Integer, List<Note>>();
jjg@1521 78 check(NoteKind.VISIBLE, (RuntimeVisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeVisibleTypeAnnotations));
jjg@1521 79 check(NoteKind.INVISIBLE, (RuntimeInvisibleTypeAnnotations_attribute) m.attributes.get(Attribute.RuntimeInvisibleTypeAnnotations));
jjg@1521 80 }
jjg@1521 81
jjg@1521 82 private void check(NoteKind kind, RuntimeTypeAnnotations_attribute attr) {
jjg@1521 83 if (attr == null)
jjg@1521 84 return;
jjg@1521 85
jjg@1521 86 for (TypeAnnotation anno: attr.annotations) {
jjg@1521 87 Position p = anno.position;
jjg@1521 88 Note note = null;
jjg@1521 89 if (p.offset != -1)
jjg@1521 90 addNote(p.offset, note = new Note(kind, anno));
jjg@1521 91 if (p.lvarOffset != null) {
jjg@1521 92 for (int i = 0; i < p.lvarOffset.length; i++) {
jjg@1521 93 if (note == null)
jjg@1521 94 note = new Note(kind, anno);
jjg@1521 95 addNote(p.lvarOffset[i], note);
jjg@1521 96 }
jjg@1521 97 }
jjg@1521 98 }
jjg@1521 99 }
jjg@1521 100
jjg@1521 101 private void addNote(int pc, Note note) {
jjg@1521 102 List<Note> list = pcMap.get(pc);
jjg@1521 103 if (list == null)
jjg@1521 104 pcMap.put(pc, list = new ArrayList<Note>());
jjg@1521 105 list.add(note);
jjg@1521 106 }
jjg@1521 107
jjg@1521 108 @Override
jjg@1521 109 void writeDetails(Instruction instr) {
jjg@1521 110 String indent = space(2); // get from Options?
jjg@1521 111 int pc = instr.getPC();
jjg@1521 112 List<Note> notes = pcMap.get(pc);
jjg@1521 113 if (notes != null) {
jjg@1521 114 for (Note n: notes) {
jjg@1521 115 print(indent);
jjg@1521 116 print("@");
jjg@1521 117 annotationWriter.write(n.anno, false, true);
jjg@1521 118 print(", ");
jlahoda@2413 119 println(StringUtils.toLowerCase(n.kind.toString()));
jjg@1521 120 }
jjg@1521 121 }
jjg@1521 122 }
jjg@1521 123
jjg@1521 124 private AnnotationWriter annotationWriter;
jjg@1521 125 private ClassWriter classWriter;
jjg@1521 126 private Map<Integer, List<Note>> pcMap;
jjg@1521 127 }

mercurial