1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2014 Eric Lafortune (eric@graphics.cornell.edu)
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21package proguard.classfile.visitor;
22
23import proguard.classfile.Clazz;
24import proguard.classfile.constant.*;
25import proguard.classfile.constant.visitor.ConstantVisitor;
26import proguard.classfile.util.*;
27
28/**
29 * This ConstantVisitor lets a given ClassVisitor visit all the referenced
30 * classes that are returned by the invoke dynamic constants that it visits.
31 *
32 * @author Eric Lafortune
33 */
34public class DynamicReturnedClassVisitor
35extends      SimplifiedVisitor
36implements   ConstantVisitor
37{
38    protected final ClassVisitor classVisitor;
39
40
41    public DynamicReturnedClassVisitor(ClassVisitor classVisitor)
42    {
43        this.classVisitor = classVisitor;
44    }
45
46
47    // Implementations for ConstantVisitor.
48
49    public void visitAnyConstant(Clazz clazz, Constant constant) {}
50
51
52    public void visitInvokeDynamicConstant(Clazz clazz, InvokeDynamicConstant invokeDynamicConstant)
53    {
54        // Is the method returning a class type?
55        Clazz[] referencedClasses = invokeDynamicConstant.referencedClasses;
56        if (referencedClasses != null    &&
57            referencedClasses.length > 0 &&
58            ClassUtil.isInternalClassType(ClassUtil.internalMethodReturnType(invokeDynamicConstant.getType(clazz))))
59        {
60            // Let the visitor visit the return type class, if any.
61            Clazz referencedClass = referencedClasses[referencedClasses.length - 1];
62            if (referencedClass != null)
63            {
64                referencedClass.accept(classVisitor);
65            }
66        }
67    }
68}
69