DynamicNewInstanceNode.java revision 12651:6ef01bd40ce2
1261411Sbr/*
2261411Sbr * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
3261411Sbr * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4261411Sbr *
5261411Sbr * This code is free software; you can redistribute it and/or modify it
6261411Sbr * under the terms of the GNU General Public License version 2 only, as
7261411Sbr * published by the Free Software Foundation.
8261411Sbr *
9261411Sbr * This code is distributed in the hope that it will be useful, but WITHOUT
10261411Sbr * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11261411Sbr * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12261411Sbr * version 2 for more details (a copy is included in the LICENSE file that
13261411Sbr * accompanied this code).
14261411Sbr *
15261411Sbr * You should have received a copy of the GNU General Public License version
16261411Sbr * 2 along with this work; if not, write to the Free Software Foundation,
17261411Sbr * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18261411Sbr *
19261411Sbr * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20261411Sbr * or visit www.oracle.com if you need additional information or have any
21261411Sbr * questions.
22261411Sbr */
23261411Sbrpackage org.graalvm.compiler.nodes.java;
24266331Sian
25266331Sianimport java.lang.reflect.Modifier;
26261411Sbr
27import org.graalvm.compiler.core.common.type.StampFactory;
28import org.graalvm.compiler.graph.Node;
29import org.graalvm.compiler.graph.NodeClass;
30import org.graalvm.compiler.graph.spi.Canonicalizable;
31import org.graalvm.compiler.graph.spi.CanonicalizerTool;
32import org.graalvm.compiler.nodeinfo.NodeInfo;
33import org.graalvm.compiler.nodes.FrameState;
34import org.graalvm.compiler.nodes.ValueNode;
35
36import jdk.vm.ci.meta.MetaAccessProvider;
37import jdk.vm.ci.meta.ResolvedJavaType;
38
39@NodeInfo
40public class DynamicNewInstanceNode extends AbstractNewObjectNode implements Canonicalizable {
41    public static final NodeClass<DynamicNewInstanceNode> TYPE = NodeClass.create(DynamicNewInstanceNode.class);
42
43    @Input ValueNode clazz;
44
45    /**
46     * Class pointer to class.class needs to be exposed earlier than this node is lowered so that it
47     * can be replaced by the AOT machinery. If it's not needed for lowering this input can be
48     * ignored.
49     */
50    @OptionalInput ValueNode classClass;
51
52    public DynamicNewInstanceNode(ValueNode clazz, boolean fillContents) {
53        this(TYPE, clazz, fillContents, null);
54    }
55
56    protected DynamicNewInstanceNode(NodeClass<? extends DynamicNewInstanceNode> c, ValueNode clazz, boolean fillContents, FrameState stateBefore) {
57        super(c, StampFactory.objectNonNull(), fillContents, stateBefore);
58        this.clazz = clazz;
59    }
60
61    public ValueNode getInstanceType() {
62        return clazz;
63    }
64
65    @Override
66    public Node canonical(CanonicalizerTool tool) {
67        if (clazz.isConstant()) {
68            ResolvedJavaType type = tool.getConstantReflection().asJavaType(clazz.asConstant());
69            if (type != null && type.isInitialized() && !throwsInstantiationException(type, tool.getMetaAccess())) {
70                return createNewInstanceNode(type);
71            }
72        }
73        return this;
74    }
75
76    /** Hook for subclasses to instantiate a subclass of {@link NewInstanceNode}. */
77    protected NewInstanceNode createNewInstanceNode(ResolvedJavaType type) {
78        return new NewInstanceNode(type, fillContents(), stateBefore());
79    }
80
81    public static boolean throwsInstantiationException(Class<?> type, Class<?> classClass) {
82        return type.isPrimitive() || type.isArray() || type.isInterface() || Modifier.isAbstract(type.getModifiers()) || type == classClass;
83    }
84
85    public static boolean throwsInstantiationException(ResolvedJavaType type, MetaAccessProvider metaAccess) {
86        return type.isPrimitive() || type.isArray() || type.isInterface() || Modifier.isAbstract(type.getModifiers()) || type.equals(metaAccess.lookupJavaType(Class.class));
87    }
88
89    public ValueNode getClassClass() {
90        return classClass;
91    }
92
93    public void setClassClass(ValueNode newClassClass) {
94        updateUsages(classClass, newClassClass);
95        classClass = newClassClass;
96    }
97}
98