001/*
002 * Copyright (c) 2012-2021 Institut National des Sciences Appliquées de Lyon (INSA Lyon) and others
003 *
004 * This program and the accompanying materials are made available under the
005 * terms of the Eclipse Public License 2.0 which is available at
006 * http://www.eclipse.org/legal/epl-2.0.
007 *
008 * SPDX-License-Identifier: EPL-2.0
009 */
010
011package gololang.ir;
012
013/**
014 * A reference to a class.
015 *
016 * <p>Used to represent a literal class notation.
017 */
018public final class ClassReference {
019  private final String name;
020
021  private ClassReference(String name) {
022    this.name = java.util.Objects.requireNonNull(name);
023  }
024
025  public static ClassReference of(Object o) {
026    if (o instanceof ClassReference) {
027      return (ClassReference) o;
028    }
029    if (o instanceof Class<?>) {
030      return new ClassReference(((Class<?>) o).getCanonicalName());
031    }
032    return new ClassReference(o.toString());
033  }
034
035  public String getName() {
036    return this.name;
037  }
038
039  public String toJVMType() {
040    return this.name.replaceAll("\\.", "#");
041  }
042
043  public Class<?> dereference() throws ClassNotFoundException {
044    return Class.forName(this.name, true, gololang.Runtime.classLoader());
045  }
046
047  @Override
048  public String toString() {
049    return "Class<" + name + ">";
050  }
051}