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.ObjectStamp; 28 import org.graalvm.compiler.core.common.type.StampFactory; 29 import org.graalvm.compiler.graph.Node; 30 import org.graalvm.compiler.graph.NodeClass; 31 import org.graalvm.compiler.graph.spi.Canonicalizable; 32 import org.graalvm.compiler.graph.spi.CanonicalizerTool; 33 import org.graalvm.compiler.nodeinfo.NodeInfo; 34 import org.graalvm.compiler.nodes.FrameState; 35 import org.graalvm.compiler.nodes.ValueNode; 36 37 import jdk.vm.ci.meta.MetaAccessProvider; 38 import jdk.vm.ci.meta.ResolvedJavaType; 39 40 @NodeInfo 41 public class DynamicNewInstanceNode extends AbstractNewObjectNode implements Canonicalizable { 42 public static final NodeClass<DynamicNewInstanceNode> TYPE = NodeClass.create(DynamicNewInstanceNode.class); 43 44 @Input ValueNode clazz; 45 46 /** 47 * Class pointer to class.class needs to be exposed earlier than this node is lowered so that it 48 * can be replaced by the AOT machinery. If it's not needed for lowering this input can be 49 * ignored. 50 */ 51 @OptionalInput ValueNode classClass; 52 53 public DynamicNewInstanceNode(ValueNode clazz, boolean fillContents) { 54 this(TYPE, clazz, fillContents, null); 55 } 56 57 protected DynamicNewInstanceNode(NodeClass<? extends DynamicNewInstanceNode> c, ValueNode clazz, boolean fillContents, FrameState stateBefore) { 58 super(c, StampFactory.objectNonNull(), fillContents, stateBefore); 59 this.clazz = clazz; 60 assert ((ObjectStamp) clazz.stamp()).nonNull(); 61 } 62 63 public ValueNode getInstanceType() { 64 return clazz; 65 } 66 67 @Override 68 public Node canonical(CanonicalizerTool tool) { 69 if (clazz.isConstant()) { 70 ResolvedJavaType type = tool.getConstantReflection().asJavaType(clazz.asConstant()); 71 if (type != null && type.isInitialized() && !throwsInstantiationException(type, tool.getMetaAccess())) { 72 return createNewInstanceNode(type); 73 } 74 } 75 return this; 76 } 77 78 /** Hook for subclasses to instantiate a subclass of {@link NewInstanceNode}. */ 79 protected NewInstanceNode createNewInstanceNode(ResolvedJavaType type) { 80 return new NewInstanceNode(type, fillContents(), stateBefore()); 81 } 82 83 public static boolean throwsInstantiationException(Class<?> type, Class<?> classClass) { 84 return type.isPrimitive() || type.isArray() || type.isInterface() || Modifier.isAbstract(type.getModifiers()) || type == classClass; 85 } 86 87 public static boolean throwsInstantiationException(ResolvedJavaType type, MetaAccessProvider metaAccess) { 88 return type.isPrimitive() || type.isArray() || type.isInterface() || Modifier.isAbstract(type.getModifiers()) || type.equals(metaAccess.lookupJavaType(Class.class)); 89 } 90 91 public ValueNode getClassClass() { 92 return classClass; 93 } 94 95 public void setClassClass(ValueNode newClassClass) { 96 updateUsages(classClass, newClassClass); 97 classClass = newClassClass; 98 } 99 }