Home | History | Annotate | Download | only in javaparser
      1 /*
      2  * Copyright (C) 2007-2010 Jlio Vilmar Gesser.
      3  * Copyright (C) 2011, 2013-2016 The JavaParser Team.
      4  *
      5  * This file is part of JavaParser.
      6  *
      7  * JavaParser can be used either under the terms of
      8  * a) the GNU Lesser General Public License as published by
      9  *     the Free Software Foundation, either version 3 of the License, or
     10  *     (at your option) any later version.
     11  * b) the terms of the Apache License
     12  *
     13  * You should have received a copy of both licenses in LICENCE.LGPL and
     14  * LICENCE.APACHE. Please refer to those files for details.
     15  *
     16  * JavaParser is distributed in the hope that it will be useful,
     17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19  * GNU Lesser General Public License for more details.
     20  */
     21 
     22 package com.github.javaparser;
     23 
     24 import java.util.Comparator;
     25 import java.util.Optional;
     26 
     27 import static com.github.javaparser.utils.Utils.EOL;
     28 import static com.github.javaparser.utils.Utils.assertNotNull;
     29 
     30 /**
     31  * A problem that was encountered during parsing.
     32  */
     33 public class Problem {
     34     private final String message;
     35     private final TokenRange location;
     36     private final Throwable cause;
     37 
     38     public Problem(String message, TokenRange location, Throwable cause) {
     39         assertNotNull(message);
     40 
     41         this.message = message;
     42         this.location = location;
     43         this.cause = cause;
     44     }
     45 
     46     @Override
     47     public String toString() {
     48         final StringBuilder str = new StringBuilder(getVerboseMessage());
     49         if (cause != null) {
     50             str.append(EOL).append("Problem stacktrace : ").append(EOL);
     51             for (int i = 0; i < cause.getStackTrace().length; i++) {
     52                 StackTraceElement ste = cause.getStackTrace()[i];
     53                 str.append("  ").append(ste.toString());
     54                 if (i + 1 != cause.getStackTrace().length)
     55                     str.append(EOL);
     56             }
     57         }
     58         return str.toString();
     59     }
     60 
     61     /**
     62      * @return the message that was passed into the constructor.
     63      */
     64     public String getMessage() {
     65         return message;
     66     }
     67 
     68     /**
     69      * @return the message plus location information.
     70      */
     71     public String getVerboseMessage() {
     72         return getLocation().map(l -> l.getBegin().getRange().map(r -> r.begin.toString()).orElse("(line ?,col ?)") + " " + message).orElse(message);
     73     }
     74 
     75     /**
     76      * @return the location that was passed into the constructor.
     77      */
     78     public Optional<TokenRange> getLocation() {
     79         return Optional.ofNullable(location);
     80     }
     81 
     82     /**
     83      * @deprecated use getLocation()
     84      */
     85     @Deprecated
     86     public Optional<TokenRange> getRange() {
     87         return getLocation();
     88     }
     89 
     90     /**
     91      * @return the cause that was passed into the constructor.
     92      */
     93     public Optional<Throwable> getCause() {
     94         return Optional.ofNullable(cause);
     95     }
     96 
     97     /**
     98      * Sorts problems on position.
     99      */
    100     public static Comparator<Problem> PROBLEM_BY_BEGIN_POSITION = (a, b) -> {
    101         final Optional<Position> aBegin= a.getLocation().flatMap(l -> l.getBegin().getRange().map(r -> r.begin));
    102         final Optional<Position> bBegin = b.getLocation().flatMap(l -> l.getBegin().getRange().map(r -> r.begin));
    103 
    104         if (aBegin.isPresent() && bBegin.isPresent()) {
    105             return aBegin.get().compareTo(bBegin.get());
    106         }
    107         if (a.getLocation().isPresent() || b.getLocation().isPresent()) {
    108             if (a.getLocation().isPresent()) {
    109                 return 1;
    110             }
    111             return -1;
    112         }
    113         return 0;
    114     };
    115 
    116 
    117 }
    118