1/*
2 * ProGuard -- shrinking, optimization, obfuscation, and preverification
3 *             of Java bytecode.
4 *
5 * Copyright (c) 2002-2013 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.evaluation;
22
23import proguard.classfile.*;
24import proguard.classfile.constant.*;
25import proguard.classfile.constant.visitor.ConstantVisitor;
26import proguard.classfile.util.SimplifiedVisitor;
27import proguard.evaluation.value.*;
28
29/**
30 * This class creates Value instance that correspond to specified constant pool
31 * entries.
32 *
33 * @author Eric Lafortune
34 */
35public class ConstantValueFactory
36extends      SimplifiedVisitor
37implements   ConstantVisitor
38{
39    protected final ValueFactory valueFactory;
40
41    // Field acting as a parameter for the ConstantVisitor methods.
42    protected Value value;
43
44
45    public ConstantValueFactory(ValueFactory valueFactory)
46    {
47        this.valueFactory = valueFactory;
48    }
49
50
51    /**
52     * Returns the Value of the constant pool element at the given index.
53     */
54    public Value constantValue(Clazz clazz,
55                               int   constantIndex)
56    {
57        // Visit the constant pool entry to get its return value.
58        clazz.constantPoolEntryAccept(constantIndex, this);
59
60        return value;
61    }
62
63
64    // Implementations for ConstantVisitor.
65
66    public void visitIntegerConstant(Clazz clazz, IntegerConstant integerConstant)
67    {
68        value = valueFactory.createIntegerValue(integerConstant.getValue());
69    }
70
71    public void visitLongConstant(Clazz clazz, LongConstant longConstant)
72    {
73        value = valueFactory.createLongValue(longConstant.getValue());
74    }
75
76    public void visitFloatConstant(Clazz clazz, FloatConstant floatConstant)
77    {
78        value = valueFactory.createFloatValue(floatConstant.getValue());
79    }
80
81    public void visitDoubleConstant(Clazz clazz, DoubleConstant doubleConstant)
82    {
83        value = valueFactory.createDoubleValue(doubleConstant.getValue());
84    }
85
86    public void visitStringConstant(Clazz clazz, StringConstant stringConstant)
87    {
88        value = valueFactory.createReferenceValue(ClassConstants.INTERNAL_NAME_JAVA_LANG_STRING,
89                                                  stringConstant.javaLangStringClass,
90                                                  false);
91    }
92
93    public void visitMethodHandleConstant(Clazz clazz, MethodHandleConstant methodHandleConstant)
94    {
95        value = valueFactory.createReferenceValue(ClassConstants.INTERNAL_NAME_JAVA_LANG_INVOKE_METHOD_HANDLE,
96                                                  methodHandleConstant.javaLangInvokeMethodHandleClass,
97                                                  false);
98    }
99
100    public void visitClassConstant(Clazz clazz, ClassConstant classConstant)
101    {
102        value = valueFactory.createReferenceValue(classConstant.getName(clazz),
103                                                  classConstant.referencedClass,
104                                                  false);
105    }
106
107    public void visitMethodTypeConstant(Clazz clazz, MethodTypeConstant methodTypeConstant)
108    {
109        value = valueFactory.createReferenceValue(ClassConstants.INTERNAL_NAME_JAVA_LANG_INVOKE_METHOD_TYPE,
110                                                  methodTypeConstant.javaLangInvokeMethodTypeClass,
111                                                  false);
112    }
113}