1 /*
   2  * Copyright (c) 1997, 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 package com.sun.tools.internal.ws.processor.modeler.annotation;
  27 
  28 import javax.annotation.processing.ProcessingEnvironment;
  29 import javax.lang.model.element.TypeElement;
  30 import javax.lang.model.type.DeclaredType;
  31 import javax.lang.model.type.NoType;
  32 import javax.lang.model.type.TypeMirror;
  33 import javax.lang.model.util.SimpleTypeVisitor6;
  34 import javax.lang.model.util.Types;
  35 import java.util.Collection;
  36 import java.util.Map;
  37 
  38 /**
  39  *
  40  * @author dkohlert
  41  */
  42 public class MakeSafeTypeVisitor extends SimpleTypeVisitor6<TypeMirror, Types> {
  43 
  44     TypeElement collectionType;
  45     TypeElement mapType;
  46 
  47     /**
  48      * Creates a new instance of MakeSafeTypeVisitor
  49      */
  50     public MakeSafeTypeVisitor(ProcessingEnvironment processingEnvironment) {
  51         collectionType = processingEnvironment.getElementUtils().getTypeElement(Collection.class.getName());
  52         mapType = processingEnvironment.getElementUtils().getTypeElement(Map.class.getName());
  53     }
  54 
  55     @Override
  56     public TypeMirror visitDeclared(DeclaredType t, Types types) {
  57         if (TypeModeler.isSubElement((TypeElement) t.asElement(), collectionType)
  58                 || TypeModeler.isSubElement((TypeElement) t.asElement(), mapType)) {
  59             Collection<? extends TypeMirror> args = t.getTypeArguments();
  60             TypeMirror[] safeArgs = new TypeMirror[args.size()];
  61             int i = 0;
  62             for (TypeMirror arg : args) {
  63                 safeArgs[i++] = visit(arg, types);
  64             }
  65             return types.getDeclaredType((TypeElement) t.asElement(), safeArgs);
  66         }
  67         return types.erasure(t);
  68     }
  69 
  70     @Override
  71     public TypeMirror visitNoType(NoType type, Types types) {
  72         return type;
  73     }
  74     @Override
  75     protected TypeMirror defaultAction(TypeMirror e, Types types) {
  76         return types.erasure(e);
  77     }
  78 }