Package se.arnetheduck.j2c.snippets

Source Code of se.arnetheduck.j2c.snippets.ReplaceInvocation

package se.arnetheduck.j2c.snippets;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.IMethodBinding;
import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.MethodInvocation;
import org.eclipse.jdt.core.dom.TypeLiteral;

import se.arnetheduck.j2c.transform.CName;
import se.arnetheduck.j2c.transform.EmptySnippet;
import se.arnetheduck.j2c.transform.ImplWriter;
import se.arnetheduck.j2c.transform.Transformer;

public class ReplaceInvocation extends EmptySnippet {

  @Override
  public boolean node(Transformer ctx, ImplWriter w, ASTNode node) {
    if (node instanceof MethodInvocation) {
      return replace(w, (MethodInvocation) node);
    }

    return super.node(ctx, w, node);
  }

  private boolean replace(ImplWriter w, MethodInvocation node) {
    if (replaceEnsureClassInitialized(w, node)) {
      return false;
    }

    return true;
  }

  private boolean replaceEnsureClassInitialized(ImplWriter w,
      MethodInvocation node) {
    if (!node.getName().getIdentifier().equals("ensureClassInitialized")) {
      return false;
    }

    if (node.arguments().size() != 1
        || !(node.arguments().get(0) instanceof TypeLiteral)) {
      return false;
    }

    IMethodBinding mb = node.resolveMethodBinding();
    if (!mb.getDeclaringClass().getQualifiedName()
        .equals("sun.misc.Unsafe")) {
      return false;
    }

    TypeLiteral tl = (TypeLiteral) node.arguments().get(0);
    ITypeBinding tb = tl.getType().resolveBinding();
    w.hardDep(tb);
    w.print(CName.relative(tb, w.type, true) + "::clinit()");

    return true;
  }
}
TOP

Related Classes of se.arnetheduck.j2c.snippets.ReplaceInvocation

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.