1b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang/*
2b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * ProGuard -- shrinking, optimization, obfuscation, and preverification
3b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang *             of Java bytecode.
4b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang *
5b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * Copyright (c) 2002-2013 Eric Lafortune (eric@graphics.cornell.edu)
6b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang *
7b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * This program is free software; you can redistribute it and/or modify it
8b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * under the terms of the GNU General Public License as published by the Free
9b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * Software Foundation; either version 2 of the License, or (at your option)
10b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * any later version.
11b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang *
12b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * This program is distributed in the hope that it will be useful, but WITHOUT
13b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * more details.
16b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang *
17b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * You should have received a copy of the GNU General Public License along
18b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * with this program; if not, write to the Free Software Foundation, Inc.,
19b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang */
21b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangpackage proguard.evaluation;
22b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
23b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangimport proguard.classfile.*;
24b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangimport proguard.classfile.constant.*;
25b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangimport proguard.classfile.constant.visitor.ConstantVisitor;
26b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangimport proguard.classfile.util.SimplifiedVisitor;
27b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangimport proguard.evaluation.value.*;
28b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
29b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang/**
30b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * This class creates Value instance that correspond to specified constant pool
31b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * entries.
32b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang *
33b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang * @author Eric Lafortune
34b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang */
35b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangpublic class ConstantValueFactory
36b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangextends      SimplifiedVisitor
37b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wangimplements   ConstantVisitor
38b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang{
39b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    protected final ValueFactory valueFactory;
40b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
41b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    // Field acting as a parameter for the ConstantVisitor methods.
42b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    protected Value value;
43b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
44b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
45b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public ConstantValueFactory(ValueFactory valueFactory)
46b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
47b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        this.valueFactory = valueFactory;
48b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
49b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
50b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
51b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    /**
52b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang     * Returns the Value of the constant pool element at the given index.
53b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang     */
54b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public Value constantValue(Clazz clazz,
55b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                               int   constantIndex)
56b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
57b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        // Visit the constant pool entry to get its return value.
58b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        clazz.constantPoolEntryAccept(constantIndex, this);
59b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
60b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        return value;
61b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
62b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
63b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
64b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    // Implementations for ConstantVisitor.
65b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
66b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant)
67b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
68b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        value = valueFactory.createIntegerValue(integerConstant.getValue());
69b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
70b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
71b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public void visitLongConstant(Clazz clazz, LongConstant longConstant)
72b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
73b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        value = valueFactory.createLongValue(longConstant.getValue());
74b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
75b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
76b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant)
77b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
78b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        value = valueFactory.createFloatValue(floatConstant.getValue());
79b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
80b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
81b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant)
82b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
83b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        value = valueFactory.createDoubleValue(doubleConstant.getValue());
84b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
85b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
86b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
87b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
88b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        value = valueFactory.createReferenceValue(ClassConstants.INTERNAL_NAME_JAVA_LANG_STRING,
89b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                                                  stringConstant.javaLangStringClass,
90b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                                                  false);
91b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
92b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
93b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public void visitMethodHandleConstant(Clazz clazz, MethodHandleConstant methodHandleConstant)
94b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
95b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        value = valueFactory.createReferenceValue(ClassConstants.INTERNAL_NAME_JAVA_LANG_INVOKE_METHOD_HANDLE,
96b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                                                  methodHandleConstant.javaLangInvokeMethodHandleClass,
97b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                                                  false);
98b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
99b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
100b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
101b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
102b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        value = valueFactory.createReferenceValue(classConstant.getName(clazz),
103b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                                                  classConstant.referencedClass,
104b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                                                  false);
105b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
106b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang
107b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    public void visitMethodTypeConstant(Clazz clazz, MethodTypeConstant methodTypeConstant)
108b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    {
109b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang        value = valueFactory.createReferenceValue(ClassConstants.INTERNAL_NAME_JAVA_LANG_INVOKE_METHOD_TYPE,
110b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                                                  methodTypeConstant.javaLangInvokeMethodTypeClass,
111b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang                                                  false);
112b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang    }
113b9cc48a43ed984587c939d02fba5316bf5c0df6eYing Wang}