Java Aspectj Usage getSourceLine(InstructionHandle ih)

Here you can find the source of getSourceLine(InstructionHandle ih)

Description

returns -1 if no source line attribute

License

Open Source License

Declaration

















public static int getSourceLine(InstructionHandle ih) 

Method Source Code

//package com.java2s;
/* *******************************************************************
 * Copyright (c) 2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. /*  w  w w .j  a  v a 2 s .c om*/
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     PARC     initial implementation 
 * ******************************************************************/

import java.util.Iterator;

import org.aspectj.apache.bcel.generic.InstructionHandle;

import org.aspectj.apache.bcel.generic.InstructionTargeter;
import org.aspectj.apache.bcel.generic.LineNumberTag;

public class Main {
    /** returns -1 if no source line attribute */
    // this naive version overruns the JVM stack size, if only Java understood
    // tail recursion...
    // public static int getSourceLine(InstructionHandle ih) {
    // if (ih == null) return -1;
    //
    // InstructionTargeter[] ts = ih.getTargeters();
    // if (ts != null) {
    // for (int j = ts.length - 1; j >= 0; j--) {
    // InstructionTargeter t = ts[j];
    // if (t instanceof LineNumberTag) {
    // return ((LineNumberTag)t).getLineNumber();
    // }
    // }
    // }
    // return getSourceLine(ih.getNext());
    // }
    public static int getSourceLine(InstructionHandle ih) {// ,boolean
        // goforwards) {
        int lookahead = 0;
        // arbitrary rule that we will never lookahead more than 100
        // instructions for a line #
        while (lookahead++ < 100) {
            if (ih == null) {
                return -1;
            }
            Iterator<InstructionTargeter> tIter = ih.getTargeters().iterator();
            while (tIter.hasNext()) {
                InstructionTargeter t = tIter.next();
                if (t instanceof LineNumberTag) {
                    return ((LineNumberTag) t).getLineNumber();
                }
            }
            // if (goforwards) ih=ih.getNext(); else
            ih = ih.getPrev();
        }
        // System.err.println("no line information available for: " + ih);
        return -1;
    }
}

Related

  1. getMethodArgByIndex(final JoinPoint joinPoint, final int index)
  2. getMethodName(ProceedingJoinPoint pjp)
  3. getPackagesHelper(IProgramElement node, IProgramElement.Kind kind, String prename, List matches)
  4. getPackagesInModel(AsmManager modl)
  5. getSignature(Signature signature, String nameFromAnnotation, boolean absolute)
  6. getTargetClass(JoinPoint jp)
  7. getTargets(IProgramElement node, IRelationship.Kind kind)
  8. isAnonymous(IProgramElement node)
  9. isConstantPushInstruction(Instruction i)