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

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

mercurial