src/share/classes/com/sun/tools/javah/TypeSignature.java

Print this page


   1 /*
   2  * Copyright (c) 2002, 2010, 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 
  26 
  27 package com.sun.tools.javah;
  28 
  29 import java.util.*;
  30 import javax.lang.model.element.Name;
  31 import javax.lang.model.element.TypeElement;
  32 import javax.lang.model.type.ArrayType;
  33 import javax.lang.model.type.DeclaredType;
  34 import javax.lang.model.type.NoType;
  35 import javax.lang.model.type.PrimitiveType;
  36 import javax.lang.model.type.TypeKind;
  37 import javax.lang.model.type.TypeMirror;
  38 import javax.lang.model.type.TypeVariable;
  39 import javax.lang.model.type.TypeVisitor;
  40 import javax.lang.model.util.Elements;
  41 import javax.lang.model.util.SimpleTypeVisitor7;
  42 
  43 /**
  44  * Returns internal type signature.
  45  *
  46  * <p><b>This is NOT part of any supported API.
  47  * If you write code that depends on this, you do so at your own
  48  * risk.  This code and its internal interfaces are subject to change
  49  * or deletion without notice.</b></p>
  50  *
  51  * @author Sucheta Dambalkar
  52  */
  53 
  54 public class TypeSignature {
  55     static class SignatureException extends Exception {
  56         private static final long serialVersionUID = 1L;
  57         SignatureException(String reason) {
  58             super(reason);
  59         }
  60     }
  61 


 228                         String classname = classNameDoc.getQualifiedName().toString();
 229                         String newclassname = classname.replace('.', '/');
 230                         JVMSig += "L";
 231                         JVMSig += newclassname;
 232                         JVMSig += ";";
 233                     }
 234                 }
 235             }
 236         }
 237         return JVMSig;
 238     }
 239 
 240     int dimensions(TypeMirror t) {
 241         if (t.getKind() != TypeKind.ARRAY)
 242             return 0;
 243         return 1 + dimensions(((ArrayType) t).getComponentType());
 244     }
 245 
 246 
 247     String qualifiedTypeName(TypeMirror type) {
 248         TypeVisitor<Name, Void> v = new SimpleTypeVisitor7<Name, Void>() {
 249             @Override
 250             public Name visitArray(ArrayType t, Void p) {
 251                 return t.getComponentType().accept(this, p);
 252             }
 253 
 254             @Override
 255             public Name visitDeclared(DeclaredType t, Void p) {
 256                 return ((TypeElement) t.asElement()).getQualifiedName();
 257             }
 258 
 259             @Override
 260             public Name visitPrimitive(PrimitiveType t, Void p) {
 261                 return elems.getName(t.toString());
 262             }
 263 
 264             @Override
 265             public Name visitNoType(NoType t, Void p) {
 266                 if (t.getKind() == TypeKind.VOID)
 267                     return elems.getName("void");
 268                 return defaultAction(t, p);
   1 /*
   2  * Copyright (c) 2002, 2011, 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 
  26 
  27 package com.sun.tools.javah;
  28 
  29 import java.util.*;
  30 import javax.lang.model.element.Name;
  31 import javax.lang.model.element.TypeElement;
  32 import javax.lang.model.type.ArrayType;
  33 import javax.lang.model.type.DeclaredType;
  34 import javax.lang.model.type.NoType;
  35 import javax.lang.model.type.PrimitiveType;
  36 import javax.lang.model.type.TypeKind;
  37 import javax.lang.model.type.TypeMirror;
  38 import javax.lang.model.type.TypeVariable;
  39 import javax.lang.model.type.TypeVisitor;
  40 import javax.lang.model.util.Elements;
  41 import javax.lang.model.util.SimpleTypeVisitor8;
  42 
  43 /**
  44  * Returns internal type signature.
  45  *
  46  * <p><b>This is NOT part of any supported API.
  47  * If you write code that depends on this, you do so at your own
  48  * risk.  This code and its internal interfaces are subject to change
  49  * or deletion without notice.</b></p>
  50  *
  51  * @author Sucheta Dambalkar
  52  */
  53 
  54 public class TypeSignature {
  55     static class SignatureException extends Exception {
  56         private static final long serialVersionUID = 1L;
  57         SignatureException(String reason) {
  58             super(reason);
  59         }
  60     }
  61 


 228                         String classname = classNameDoc.getQualifiedName().toString();
 229                         String newclassname = classname.replace('.', '/');
 230                         JVMSig += "L";
 231                         JVMSig += newclassname;
 232                         JVMSig += ";";
 233                     }
 234                 }
 235             }
 236         }
 237         return JVMSig;
 238     }
 239 
 240     int dimensions(TypeMirror t) {
 241         if (t.getKind() != TypeKind.ARRAY)
 242             return 0;
 243         return 1 + dimensions(((ArrayType) t).getComponentType());
 244     }
 245 
 246 
 247     String qualifiedTypeName(TypeMirror type) {
 248         TypeVisitor<Name, Void> v = new SimpleTypeVisitor8<Name, Void>() {
 249             @Override
 250             public Name visitArray(ArrayType t, Void p) {
 251                 return t.getComponentType().accept(this, p);
 252             }
 253 
 254             @Override
 255             public Name visitDeclared(DeclaredType t, Void p) {
 256                 return ((TypeElement) t.asElement()).getQualifiedName();
 257             }
 258 
 259             @Override
 260             public Name visitPrimitive(PrimitiveType t, Void p) {
 261                 return elems.getName(t.toString());
 262             }
 263 
 264             @Override
 265             public Name visitNoType(NoType t, Void p) {
 266                 if (t.getKind() == TypeKind.VOID)
 267                     return elems.getName("void");
 268                 return defaultAction(t, p);