/*******************************************************************************
 * Copyright (c) 2015, 2016 Google, Inc and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Stefan Xenos (Google) - Initial implementation
 *******************************************************************************/
package org.aspectj.org.eclipse.jdt.internal.core.nd.java;

import org.aspectj.org.eclipse.jdt.internal.core.nd.Nd;
import org.aspectj.org.eclipse.jdt.internal.core.nd.NdStruct;
import org.aspectj.org.eclipse.jdt.internal.core.nd.field.FieldManyToOne;
import org.aspectj.org.eclipse.jdt.internal.core.nd.field.StructDef;

public class NdMethodException extends NdStruct {

	public static final FieldManyToOne<NdTypeSignature> EXCEPTION_TYPE;

	@SuppressWarnings("hiding")
	public static StructDef<NdMethodException> type;

	static {
		type = StructDef.create(NdMethodException.class);
		EXCEPTION_TYPE = FieldManyToOne.create(type, NdTypeSignature.USED_AS_EXCEPTION);
		type.done();
	}

	public NdMethodException(Nd nd, long address) {
		super(nd, address);
	}

	public void setExceptionType(NdTypeSignature signature) {
		EXCEPTION_TYPE.put(getNd(), this.address, signature);
	}

	public NdTypeSignature getExceptionType() {
		return EXCEPTION_TYPE.get(getNd(), this.address);
	}

	@Override
	public String toString() {
		try {
			return getExceptionType().toString();
		} catch (RuntimeException e) {
			// This is called most often from the debugger, so we want to return something meaningful even
			// if the code is buggy, the database is corrupt, or we don't have a read lock.
			return super.toString();
		}
	}
}
