1 /*
   2  * Copyright (c) 2013, 2014, 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.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.nodes.java;
  24 
  25 import java.lang.reflect.Modifier;
  26 
  27 import org.graalvm.compiler.core.common.type.StampFactory;
  28 import org.graalvm.compiler.graph.Node;
  29 import org.graalvm.compiler.graph.NodeClass;
  30 import org.graalvm.compiler.graph.spi.Canonicalizable;
  31 import org.graalvm.compiler.graph.spi.CanonicalizerTool;
  32 import org.graalvm.compiler.nodeinfo.NodeInfo;
  33 import org.graalvm.compiler.nodes.FrameState;
  34 import org.graalvm.compiler.nodes.ValueNode;
  35 
  36 import jdk.vm.ci.meta.MetaAccessProvider;
  37 import jdk.vm.ci.meta.ResolvedJavaType;
  38 
  39 @NodeInfo
  40 public 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 }