Example usage for org.aspectj.bridge ISourceLocation getSourceFile

List of usage examples for org.aspectj.bridge ISourceLocation getSourceFile

Introduction

In this page you can find the example usage for org.aspectj.bridge ISourceLocation getSourceFile.

Prototype

File getSourceFile();

Source Link

Usage

From source file:de.zalando.mojo.aspectj.MavenMessageHandler.java

License:Open Source License

/**
 * Copies output from the supplied message onto the active Maven Log.
 * If the message type (i.e. {@code message.getKind()}) is listed in the showDetailsForMessageKindList List,
 * the message is prefixed with location details (Class, row/line number etc.) as well.
 * <p/>/*from  w  w w  .  j a va 2 s  .  com*/
 * {@inheritDoc}
 */
public boolean handleMessage(final IMessage message) {

    // Compose the message text
    final StringBuilder builder = new StringBuilder(message.getMessage());
    if (isMessageDetailDesired(message)) {

        //
        // The AJC details are typically delivered on the format [fileName]:[lineNumber]
        // (i.e. /src/main/java/Clazz.java:16).
        //
        // Mimic this, and include the context of the message as well,
        // including guarding against NPEs.
        //
        final ISourceLocation sourceLocation = message.getSourceLocation();
        final String sourceFile = sourceLocation == null || sourceLocation.getSourceFile() == null
                ? "<unknown source file>"
                : sourceLocation.getSourceFile().getAbsolutePath();
        final String context = sourceLocation == null || sourceLocation.getContext() == null ? ""
                : sourceLocation.getContext() + "\n";
        final String line = sourceLocation == null ? "<no line information>" : "" + sourceLocation.getLine();

        builder.append("\n\t").append(sourceFile).append(":").append(line).append("\n").append(context);
    }

    final String messageText = builder.toString();

    if (isNotIgnored(message, IMessage.DEBUG) || isNotIgnored(message, IMessage.INFO)
            || isNotIgnored(message, IMessage.TASKTAG)) {

        // The DEBUG, INFO, and TASKTAG ajc message kinds are considered Maven Debug messages.
        log.debug(messageText);

    } else if (isNotIgnored(message, IMessage.WEAVEINFO)) {

        // The WEAVEINFO ajc message kind is considered Maven Info messages.
        log.info(messageText);

    } else if (isNotIgnored(message, IMessage.WARNING)) {

        // The WARNING ajc message kind is considered Maven Warn messages.
        log.warn(messageText);

    } else if (isNotIgnored(message, IMessage.ERROR) || isNotIgnored(message, IMessage.ABORT)
            || isNotIgnored(message, IMessage.FAIL)) {

        // We map ERROR, ABORT, and FAIL ajc message kinds to Maven Error messages.
        log.error(messageText);
    }

    // Delegate to normal handling.
    return super.handleMessage(message);
}

From source file:org.caesarj.compiler.aspectj.CaesarMessageHandler.java

License:Open Source License

/**
 * Handles the (AspectJ) message, by creating a KOPI-error and report it to
 * the compiler./*from   w  ww  .  j  a v a  2 s  .  co  m*/
 */
public boolean handleMessage(IMessage message) throws AbortException {
    if (isIgnoring(message.getKind())) {
        return true;
    }

    ISourceLocation location = message.getSourceLocation();

    if (message.getKind() == IMessage.WARNING) {

        if (location != null) {

            compiler.get()
                    .reportTrouble(new CWarning(
                            new TokenReference(location.getSourceFile().getPath(), location.getSourceFile(),
                                    location.getLine()),
                            new Message(CaesarMessages.ASPECTJ_WARNING, message.getMessage())));
        } else {

            compiler.get().reportTrouble(new CWarning(TokenReference.NO_REF,
                    new Message(CaesarMessages.ASPECTJ_WARNING, message.getMessage())));

        }

        return true;
    }

    if (location != null) {

        compiler.get()
                .reportTrouble(new PositionedError(
                        new TokenReference(location.getSourceFile().getPath(), location.getSourceFile(),
                                location.getLine()),
                        new Message(CaesarMessages.ASPECTJ_ERROR, message.getMessage())));

    } else {

        compiler.get().reportTrouble(new PositionedError(TokenReference.NO_REF,
                new Message(CaesarMessages.ASPECTJ_ERROR, message.getMessage())));

    }

    return true;
}

From source file:org.caesarj.ui.editor.CaesarJContentOutlinePage.java

License:Open Source License

/**
 * This method is called when the user select a node in the content tree.
 * It searches the location of this node and opens it in the editor.
 * //from www .j a  v a2  s  .  com
 * @see org.eclipse.jface.viewers.ISelectionChangedListener 
 */
public void selectionChanged(SelectionChangedEvent event) {

    super.selectionChanged(event);

    /* ignore the first selection message that comes from initialization */
    /* necessary for navigation by crosscutting links */
    if (bFirstSelection) {
        bFirstSelection = false;
        return;
    }

    ISelection selection = event.getSelection();
    if (selection.isEmpty()) {
    } else {
        Object item = ((IStructuredSelection) selection).getFirstElement();

        if (item instanceof LinkNode) {
            if (((LinkNode) item).getType() == LinkNode.LINK_NODE_RELATIONSHIP) {
                return;
            }
            item = ((LinkNode) item).getTargetElement();
        }

        IProgramElement selectedNode = (IProgramElement) item;
        ISourceLocation sourceLocation = selectedNode.getSourceLocation();

        if (sourceLocation != null) {
            try {

                IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();

                IPath path = new Path(sourceLocation.getSourceFile().getAbsolutePath());
                IResource resource = root.getFileForLocation(path);
                IMarker marker;

                if (resource != null) {
                    marker = resource.createMarker(IMarker.MARKER);
                    marker.setAttribute(IMarker.LINE_NUMBER, sourceLocation.getLine());
                    marker.setAttribute(IMarker.CHAR_START, sourceLocation.getColumn());
                    IDE.openEditor(
                            CaesarPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getActivePage(),
                            marker);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }
}

From source file:org.eclipse.ajdt.internal.ui.ajde.UIMessageHandler.java

License:Open Source License

private void showMessages(final IProject project) {

    // THIS MUST STAY IN A SEPARATE THREAD - This is because we need
    // to create and setup the marker in an atomic operation. See
    // AMC or ASC.
    IWorkspaceRunnable r = new IWorkspaceRunnable() {
        public void run(IProgressMonitor monitor) {

            try {

                Iterator<IResource> affectedResourceIterator = affectedResources.iterator();
                AJLog.log(AJLog.COMPILER, "Types affected during build = " + affectedResources.size()); //$NON-NLS-1$
                IResource ir = null;/*from  w  w  w  .j a v a 2s.c  om*/
                while (affectedResourceIterator.hasNext()) {
                    ir = (IResource) affectedResourceIterator.next();
                    try {
                        if (ir.exists()) {
                            ir.deleteMarkers(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER, false,
                                    IResource.DEPTH_INFINITE);
                            ir.deleteMarkers(IAJModelMarker.AJDT_PROBLEM_MARKER, true,
                                    IResource.DEPTH_INFINITE);
                            ir.deleteMarkers(IMarker.TASK, true, IResource.DEPTH_INFINITE);
                            // now removed markers from compilation participants
                            HashSet<String> managedMarkers = JavaModelManager
                                    .getJavaModelManager().compilationParticipants.managedMarkerTypes();
                            for (String managedMarker : managedMarkers) {
                                ir.deleteMarkers(managedMarker, true, IResource.DEPTH_INFINITE);
                            }
                        }
                    } catch (CoreException re) {
                        AJLog.log("Failed marker deletion: resource=" //$NON-NLS-1$
                                + ir.getLocation());
                        throw re;
                    }
                }

                Iterator<ProblemTracker> problemIterator = problems.iterator();
                ProblemTracker p = null;
                while (problemIterator.hasNext()) {
                    p = (ProblemTracker) problemIterator.next();
                    ir = null;
                    IMarker marker = null;
                    try {
                        if (p.location != null) {
                            ir = locationToResource(p.location, project);
                            if ((ir != null) && ir.exists()) {
                                // 128803 - only add problems to affected resources
                                if (lastBuildWasFull || affectedResources.contains(ir)
                                        || ir.getProject() != project) {
                                    int prio = getTaskPriority(p);
                                    if (prio != -1) {
                                        marker = ir.createMarker(IMarker.TASK);
                                        marker.setAttribute(IMarker.PRIORITY, prio);
                                    } else {
                                        if (p.declaredErrorOrWarning) {
                                            marker = ir.createMarker(IAJModelMarker.AJDT_PROBLEM_MARKER);
                                        } else {
                                            // create Java marker with problem id so
                                            // that quick fix is available
                                            marker = ir
                                                    .createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
                                            marker.setAttribute(IJavaModelMarker.ID, p.id);
                                        }
                                    }
                                    if ((p.start >= 0) && (p.end >= 0)) {
                                        marker.setAttribute(IMarker.CHAR_START, new Integer(p.start));
                                        marker.setAttribute(IMarker.CHAR_END, new Integer(p.end + 1));
                                    }
                                    if (!ir.getProject().equals(project)) {
                                        addOtherProjectMarker(project, marker);
                                    }
                                    if (p.location.getLine() > 0) {
                                        marker.setAttribute(IMarker.LINE_NUMBER,
                                                new Integer(p.location.getLine()));
                                    }
                                } else {
                                    AJLog.log(AJLog.COMPILER_MESSAGES,
                                            "Not adding marker for problem because it's " //$NON-NLS-1$
                                                    + "against a resource which is not in the list of affected resources" //$NON-NLS-1$
                                                    + " provided by the compiler. Resource=" + ir //$NON-NLS-1$
                                                    + " Problem message=" //$NON-NLS-1$
                                                    + p.message + " line=" + p.location.getLine()); //$NON-NLS-1$
                                }
                            }
                        } else {
                            marker = project.createMarker(IJavaModelMarker.JAVA_MODEL_PROBLEM_MARKER);
                        }
                        if (marker != null) {
                            setSeverity(marker, p.kind);

                            if ((p.extraLocs != null) && (p.extraLocs.size() > 0)) { // multiple
                                // part
                                // message
                                int relCount = 0;
                                for (Iterator<?> iter = p.extraLocs.iterator(); iter.hasNext();) {
                                    ISourceLocation sLoc = (ISourceLocation) iter.next();
                                    StringBuffer attrData = new StringBuffer();
                                    attrData.append(sLoc.getSourceFile().getAbsolutePath());
                                    attrData.append(":::"); //$NON-NLS-1$
                                    attrData.append(sLoc.getLine());
                                    attrData.append(":::"); //$NON-NLS-1$
                                    attrData.append(sLoc.getEndLine());
                                    attrData.append(":::"); //$NON-NLS-1$
                                    attrData.append(sLoc.getColumn());
                                    marker.setAttribute(
                                            AspectJUIPlugin.RELATED_LOCATIONS_ATTRIBUTE_PREFIX + (relCount++),
                                            attrData.toString());
                                }
                            }

                            setMessage(marker, p.message);
                        }
                    } catch (CoreException re) {
                        AJLog.log("Failed marker creation: resource=" //$NON-NLS-1$
                                + p.location.getSourceFile().getPath() + " line=" //$NON-NLS-1$
                                + p.location.getLine() + " message=" + p.message); //$NON-NLS-1$
                        throw re;
                    }
                }
                clearMessages();
            } catch (CoreException e) {
                AJDTErrorHandler.handleAJDTError(UIMessages.CompilerTaskListManager_Error_creating_marker, e);
            }
        }
    };

    try {
        AspectJPlugin.getWorkspace().run(r, null);
    } catch (CoreException cEx) {
        AJDTErrorHandler.handleAJDTError(UIMessages.CompilerTaskListManager_Error_adding_problem_markers, cEx);
    }
    // Part of the fix for bug 89793 - editor image is not updated
    Collection<AspectJEditor> activeEditorList = AspectJEditor.getActiveEditorList();
    synchronized (activeEditorList) {
        for (AspectJEditor editor : activeEditorList) {
            editor.resetTitleImage();
        }
    }
}

From source file:org.eclipse.ajdt.internal.ui.ajde.UIMessageHandler.java

License:Open Source License

/**
 * Try to map a source location in a project to an IResource
 * // ww  w.ja  v  a  2  s . c o  m
 * @param sloc
 *            the source location
 * @param project
 *            the project to look in first
 * @return the IResource if a match was found, null otherwise
 */
private IResource locationToResource(ISourceLocation sloc, IProject project) {
    IResource resource = null;
    File file = sloc.getSourceFile();
    String loc = file.getPath();
    if (!file.exists()) {
        // 167121: might be a binary file in a directory, which uses ! as a separator
        //  - see org.aspectj.weaver.ShadowMunger.getBinaryFile()
        loc = loc.replace('!', File.separatorChar);
    }
    // try this project
    FileURICache fileCache = ((CoreCompilerConfiguration) AspectJPlugin.getDefault().getCompilerFactory()
            .getCompilerForProject(project).getCompilerConfiguration()).getFileCache();

    resource = fileCache.findResource(loc, project);

    if (resource == null) {
        // try any project
        resource = fileCache.findResource(loc);
        if (resource == null) {
            // fix for declare
            // warning/error bug which
            // returns only file name
            // (unqualified)
            resource = tryToFindResource(loc, project);
        }
        // At least warn that you are going to
        // blow up with an event trace ...
        if (resource == null) {
            AJLog.log(AJLog.COMPILER,
                    "Whilst adding post compilation markers to resources, cannot locate valid eclipse resource for file " //$NON-NLS-1$
                            + loc);
        }
    }

    return resource;
}