alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.tasklist.compiler;

import java.util.ArrayList;
import java.util.regex.*;
import org.openide.compiler.ErrorEvent;

/**
 * javac message
 *
 * @author Tim Lebedkov
 */
public class JavacError extends CompileError {
    /**
     * Javac message description.
     */
    private static class MessageInfo {
        private Pattern re;
        private int[] pos;
        private String regexp;
        private int code;
        
        /**
         * Constructor. All regular subexpressions will be used.
         *
         * @param code error code
         * @param regexp regular expression for an error message
         */
        public MessageInfo(int code, String regexp) {
            this(code, regexp, null);
        }
        
        /**
         * Constructor
         *
         * @param code error code
         * @param regexp regular expression for an error message
         * @param pos indices of subexpressions for message parameters or null
         * if all subexpressions should be used
         */
        public MessageInfo(int code, String regexp, int[] pos) {
            this.code = code;
            this.regexp = regexp;
            this.pos = pos;
            re = Pattern.compile(regexp);
        }

        /**
         * Parses an error message
         *
         * @param msg a Javac message to be parsed
         * @return parameters of the message
         */
        public String[] split(String msg) {
            Matcher m = re.matcher(msg);
            if (!m.matches())
                return null;
            
            ArrayList ret = new ArrayList();
            if (pos != null) {
                for (int i = 0; i < pos.length; i++) {
                    int start = m.start(pos[i]);
                    int end = m.end(pos[i]);
                    if (start != -1 && end != -1)
                        ret.add(msg.substring(start, end));
                    else
                        ret.add(null);
                }
            } else {
                for (int i = 1; i < m.groupCount(); i++) {
                    int start = m.start(i);
                    int end = m.end(i);
                    if (start != -1 && end != -1)
                        ret.add(msg.substring(start, end));
                    else
                        ret.add(null);
                }
            }
            return (String[]) ret.toArray(new String[ret.size()]);
        }
        
        /** 
         * Getter for property code.
         *
         * @return Value of property code.
         */
        public int getCode() {
            return code;
        }
    }
    
    /** 
     * All errors which do not refer to a particular line in the source code are
     * preceded by this string.
     */
    private static String ERROR_PREFIX = "error: ";
        
    /**
     * The following string will appear before all messages keyed as:
     * "compiler.note".
     */
    private static String NOTE_KEY = "Note: ";
        
    /** All warning messages are preceded by the following string. */
    private static String WARNING_KEY = "warning: ";
    
    /**
     * The following string will always appear after one of the above deprecation
     * messages.
     */
    private static String DEPRECATION_MSG = 
        "Recompile with -deprecation for details.";
        
    /**
     * The following string will always appear after one of the above deprecation
     * messages.
     */
    private static String UNCHECKED_MSG =
        "Recompile with -warnunchecked for details.";
        
    private static MessageInfo[] MESSAGES = {
        new MessageInfo(1, "(\\S*) is abstract; cannot be instantiated"),
        new MessageInfo(2, "abstract methods cannot have a body"),
        new MessageInfo(3, "(\\S*) is already defined in a single-type import"),
        new MessageInfo(4, "(\\S*) is already defined in this compilation unit"),
        new MessageInfo(5, "(\\S*) is already defined in (\\S*)"),
        new MessageInfo(6, "anonymous class implements interface; cannot have arguments"),
        new MessageInfo(7, "anonymous class implements interface; cannot have qualifier for new"),
        new MessageInfo(8, "array required, but (\\S*) found"),
        
        new MessageInfo(9, "break outside switch or loop"),
        new MessageInfo(10, "call to (\\S*) must be first statement in constructor"),
        new MessageInfo(11, "cannot access (\\S*); (\\S*)"),
        new MessageInfo(12, "(\\S*) in (\\S*) cannot be applied to \\x28(\\S*)\\x29"),
        new MessageInfo(13, "cannot assign a value to final variable (\\S*)"),
        new MessageInfo(14, "(\\S*) cannot be dereferenced"),
        new MessageInfo(15, "cannot inherit from final (\\S*)"),
        new MessageInfo(16, "cannot reference (\\S*) before supertype constructor has been called"),
        new MessageInfo(17, "cannot return a value from method whose result type is void"),
        new MessageInfo(18, "cannot select a static class from a parameterized type"),
        new MessageInfo(19, "(\\S*) cannot be inherited with different arguments: <(\\S*)> and <(\\S*)>"),
        new MessageInfo(20, "'catch' without 'try'"),
        new MessageInfo(21, "(\\S*) clashes with package of same name"),
        new MessageInfo(22, "code too large for try statement"),
        new MessageInfo(23, "constant expression required"),
        new MessageInfo(24, "continue outside of loop"),
        new MessageInfo(25, "cyclic inheritance involving (\\S*)"),
         
        new MessageInfo(26, "package (\\S*) does not exist"),
        new MessageInfo(27, "duplicate class: (\\S*)"),
        new MessageInfo(28, "duplicate case label"),
        new MessageInfo(29, "duplicate default label"),

        new MessageInfo(30, "'else' without 'if'"),
        new MessageInfo(31, "empty character literal"),
        new MessageInfo(32, "error reading (\\S*); (\\S*)"),
        new MessageInfo(33, "exception (\\S*) has already been caught"),
        new MessageInfo(34, "exception (\\S*) is never thrown in body of corresponding try statement"),

        new MessageInfo(35, "'finally' without 'try'"),
        new MessageInfo(36, "floating point number too large"),
        new MessageInfo(37, "floating point number too small"),

        new MessageInfo(38, "inner classes cannot have static declarations"),
        new MessageInfo(39, "illegal character: \\\\(\\S*)"),
        new MessageInfo(40, "illegal combination of modifiers: (\\S*) and (\\S*)"),
        new MessageInfo(41, "illegal escape character"),
        new MessageInfo(42, "illegal forward reference"),
        new MessageInfo(43, "illegal initializer for (\\S*)"),
        new MessageInfo(44, "illegal line end in character literal"),
        new MessageInfo(45, "illegal qualifier; (\\S*) is not an inner class"),
        new MessageInfo(46, "illegal start of expression"),
        new MessageInfo(47, "illegal start of type"),
        new MessageInfo(48, "illegal unicode escape"),
        new MessageInfo(49, "improperly formed type, some parameters are missing"),
        new MessageInfo(50, "incomparable types: (\\S*) and (\\S*)"),
        new MessageInfo(51, "integer number too large: (\\S*)"),
        new MessageInfo(52, "internal error; cannot instantiate (\\S*) at (\\S*) to \\x28(\\S*)\\x29"),
        new MessageInfo(53, "interfaces (\\S*) and (\\S*) are incompatible; both define (\\S*), but with different return type"),
        new MessageInfo(54, "interface expected here"),
        new MessageInfo(55, "interface methods cannot have body"),
        new MessageInfo(56, "hexadecimal numbers must contain at least one hexadecimal digit"),
        new MessageInfo(57, "invalid method declaration; return type required"),

        new MessageInfo(58, "label (\\S*) already in use"),
        new MessageInfo(59, "local variable (\\S*) is accessed from within inner class; needs to be declared final"),

        new MessageInfo(60, "malformed floating point literal"),
        new MessageInfo(61, "missing method body, or declare abstract"),
        new MessageInfo(62, "missing return statement"),
        new MessageInfo(63, "missing return value"),
        new MessageInfo(64, "modifier (\\S*) not allowed here"),

        new MessageInfo(65, "name clash: (\\S*) and (\\S*) have the same erasure"),
        new MessageInfo(66, "name clash: (\\S*) in (\\S*) and (\\S*) in (\\S*) have the same erasure, yet none overrides the other"),
        new MessageInfo(67, "(\\S*) is reserved for internal use"),
        new MessageInfo(68, "native methods cannot have a body"),
        new MessageInfo(69, "no enclosing instance of type (\\S*) is in scope"),
        new MessageInfo(70, "no interface expected here"),
        new MessageInfo(71, "(\\S*) has no match in entry in (\\S*); required (\\S*)"),
        new MessageInfo(72, "(\\S*) in (\\S*) is not defined in a public class or interface; cannot be accessed from outside package"),
        new MessageInfo(73, "(\\S*) is not public in (\\S*); cannot be accessed from outside package"),
        new MessageInfo(74, "not a loop label: (\\S*)"),
        new MessageInfo(75, "not a statement"),
        new MessageInfo(76, "not an enclosing class: (\\S*)"),

        new MessageInfo(77, "operator (\\S*) cannot be applied to (\\S*)"),
        
        new MessageInfo(78, "package (\\S*) clashes with class of same name"),
        
        new MessageInfo(79, "qualified new of static class"),
        
        new MessageInfo(80, "recursive constructor invocation"),
        new MessageInfo(81, "reference to (\\S*) is ambiguous, both (\\S*) (\\S*) in (\\S*) and (\\S*) (\\S*) in (\\S*) match"),
        new MessageInfo(82, "repeated interface"),
        new MessageInfo(83, "repeated modifier"),
        new MessageInfo(84, "(\\S*) has (\\S*) access in (\\S*)"),
        new MessageInfo(85, "return outside method"),
        
        new MessageInfo(86, "signature does not match (\\S*); incompatible supertype"),
        new MessageInfo(87, "signature does not match (\\S*); incompatible interfaces"),
        new MessageInfo(88, "(\\S*) should be declared abstract; it does not define (\\S*) in (\\S*)"),
        new MessageInfo(89, "error writing source; cannot overwrite input file (\\S*)"),
        new MessageInfo(90, "the symbol (\\S*) conflicts with a compiler-synthesized symbol in (\\S*)"),
        
        new MessageInfo(91, "'try' without 'catch' or 'finally'"),
        new MessageInfo(92, "type (\\S*) does not take parameters"),
        new MessageInfo(93, "type variables cannot be dereferenced"),
        new MessageInfo(94, "type variable (\\S*) occurs more than once in result type of (\\S*); cannot be left uninstantiated"),
        new MessageInfo(95, "type variable (\\S*) occurs more than once in type of (\\S*); cannot be left uninstantiated"),
        
        new MessageInfo(96, "unclosed character literal"),
        new MessageInfo(97, "unclosed comment"),
        new MessageInfo(98, "unclosed string literal"),
        new MessageInfo(99, "unsupported encoding: (\\S*)"),
        new MessageInfo(100, "error reading source file: (\\S*)"),
        new MessageInfo(101, "undefined label: (\\S*)"),
        new MessageInfo(102, "unreachable statement"),
        new MessageInfo(103, "initializer must be able to complete normally"),
        new MessageInfo(104, "unreported exception (\\S*); must be caught or declared to be thrown"),
        new MessageInfo(105, "'void' type not allowed here"),
        
        new MessageInfo(106, "wrong number of type arguments; required (\\S*)"),
        
        new MessageInfo(107, "variable (\\S*) might already have been assigned to"),
        new MessageInfo(108, "variable (\\S*) might not have been initialized"),
        new MessageInfo(109, "variable (\\S*) might be assigned in loop"),
        
        new MessageInfo(110, "as of release 1\\x2E4, 'assert' is a keyword, and may not be used as an identifier"),
        
        // In the following string, {1} will always be the detail message from
        // java.io.IOException.
        new MessageInfo(111, "error while writing (\\S*): (\\S*)"),
        
        // In the following string, {0} is the name of the class in the Java source.
        // It really should be used two times..
        new MessageInfo(112, "class (\\S*) is public, should be declared in a file named (\\S*)\\x2Ejava"),
        
        // The following error messages do not refer to a line in the source code.
        new MessageInfo(113, "cannot read: (\\S*)"),
        
        // Fatal Errors
        new MessageInfo(114, "Fatal Error: Unable to locate package java\\x2Elang in classpath or bootclasspath"),
        new MessageInfo(115, "Fatal Error: Unable to locate method (\\S*)"),
        
        new MessageInfo(116, "(\\S*) uses or overrides a deprecated API\\x2E"),
        new MessageInfo(117, "Some input files use or override a deprecated API\\x2E"),
        
        new MessageInfo(118, "(\\S*) uses unchecked operations\\x2E"),
        new MessageInfo(119, "Some input files use unchecked operations\\x2E"),
        
        // extra output when using -checkclassfile (code/ClassReader)
        new MessageInfo(120, "class file has later version than expected: (\\S*)"),
        new MessageInfo(121, "unrecognized attribute: (\\S*)"),
        
        // warnings
        new MessageInfo(122, "(\\S*) in (\\S*) has been deprecated"),
        new MessageInfo(123, "unchecked assignment: (\\S*) to (\\S*)"),
        new MessageInfo(124, "unchecked assignment to variable (\\S*) of raw type class (\\S*)"),
        new MessageInfo(125, "unchecked call to (\\S*) as a member of the raw type (\\S*)"),
        new MessageInfo(126, "unchecked cast to type (\\S*)"),
        new MessageInfo(127, "unchecked generic array creation"),
        new MessageInfo(128, "unchecked method invocation: (\\S*) in (\\S*) is applied to ((\\S*))"),
        new MessageInfo(129, "as of release 1\\x2E4, 'assert' is a keyword, and may not be used as an identifier"),
        new MessageInfo(130, "possible fall-through into case"),

        // The following are tokens which are non-terminals in the language. They should
        // be named as JLS3 calls them when translated to the appropriate language.
        // ""
        // ""
        // ""
        // ""
        // ""
        // ""
        // ""
        // ""
        // ""
        
        /*
         * The argument to the following string will always be one of the following:
         * 1. one of the above non-terminals
         * 2. a keyword (JLS1.8)
         * 3. a boolean literal (JLS3.10.3)
         * 4. the null literal (JLS3.10.7)
         * 5. a Java separator (JLS3.11)
         * 6. an operator (JLS3.12)
         *
         * This is the only place these tokens will be used.
         */
        new MessageInfo(131, "(\\S*) expected"),

        // The following are related in form, but do not easily fit the above paradigm.
        new MessageInfo(132, "'case', 'default' or '}' expected"),
        new MessageInfo(133, "'class' or 'interface' expected"),
        new MessageInfo(134, "'\\x2Eclass' expected"),
        new MessageInfo(135, "'\\x28' or '\\x5B' expected"),
        
        // The argument to this string will always be either 'case' or 'default'.
        new MessageInfo(136, "orphaned (\\S*)"),
        
        new MessageInfo(137, ""),

        new MessageInfo(138, "cannot access (\\S*)\n(\\S*)\n"),

        new MessageInfo(139, "bad class file: (\\S*)\n(\\S*)\nPlease remove or make sure it appears in the correct subdirectory of the classpath\\x2E"),

        // Please remove or make sure it appears in the correct subdirectory of the classpath.

        // The following are all possible strings for the second argument ((\\S*)) of the
        // above strings.
        new MessageInfo(140, "bad class signature: (\\S*)"),
        new MessageInfo(141, "bad constant pool tag: (\\S*)"),
        new MessageInfo(142, "bad constant pool tag: (\\S*) at (\\S*)"),
        new MessageInfo(143, "bad signature: (\\S*)"),
        new MessageInfo(144, "class file contains wrong class: (\\S*)"),
        new MessageInfo(145, "file (\\S*)\\x2Eclass not found"),
        new MessageInfo(146, "file does not contain class (\\S*)"),
        new MessageInfo(147, "illegal start of class file"),
        new MessageInfo(148, "unable to access file: (\\S*)"),
        new MessageInfo(149, "unicode string in class file not supported"),
        new MessageInfo(150, "undeclared type variable: (\\S*)"),
        new MessageInfo(151, "class file has wrong version (\\S*)\\x2E(\\S*), should be (\\S*)\\x2E(\\S*)"),

        new MessageInfo(152, "type parameter (\\S*) is not within its bound"),
        new MessageInfo(153, "type parameter (\\S*) is not within its bound; (\\S*)"),

        // The following are all possible strings for the second argument ((\\S*)) of the
        // above string.
        // none yet...

        new MessageInfo(154, "(\\S*)\nfound   : (\\S*)\nrequired: (\\S*)"),

        // The following are all possible strings for the first argument ((\\S*)) of the
        // above string.
        new MessageInfo(155, "incompatible types"),
        new MessageInfo(156, "inconvertible types"),
        new MessageInfo(157, "possible loss of precision"),

        // The following are all possible strings for the first argument ({0}))
        // "class"
        // "class or array"
        // "reference"
        new MessageInfo(158, "unexpected type\nfound   : (\\S*)\nrequired: (\\S*)"),


        // The first argument ((\\S*)) is a "kindname".
        new MessageInfo(159, "abstract (\\S*) (\\S*) cannot be accessed directly"),

        // The first argument ((\\S*)) is a "kindname".
        new MessageInfo(160, "non-static (\\S*) (\\S*) cannot be referenced from a static context"),

        // Both arguments ((\\S*), (\\S*)) are "kindname"s.  (\\S*) is a comma-separated list
        // of kindnames (the list should be identical to that provided in source.
        new MessageInfo(161, "unexpected type\nrequired: (\\S*)\nfound   : (\\S*)"),

        // The first argument ((\\S*)) is a "kindname".
        new MessageInfo(162, "cannot resolve symbol\nsymbol: (\\S*) (\\S*) (\\S*) (\\S*)"),


        // The first argument ((\\S*)) and fifth argument ((\\S*)) are "kindname"s.
        new MessageInfo(163, "cannot resolve symbol\nsymbol  : (\\S*)( (\\S*))?( (\\S*))?( (\\S*))?\nlocation: (\\S*)( (\\S*))?", new int[] {1, 3, 5, 7, 8, 10}),

        // The following are all possible string for "kindname".
        // They should be called whatever the JLS calls them after it been translated
        // to the appropriate language.
        // "class", new Integer(0)},
        // "constructor"
        // "identifier({0})"
        // "interface"
        // "method"
        // "package"
        // "bound of type variable"
        // "value"
        // "variable"

        new MessageInfo(164, "(\\S*); (\\S*) and (\\S*) are static"),
        new MessageInfo(165, "(\\S*); overridden method is (\\S*)"),

        // In the following string (\\S*) is a space separated list of Java Keywords, as
        // they would have been declared in the source code

        
        new MessageInfo(166, "(\\S*); attempting to assign weaker access privileges; was (\\S*)"),
        new MessageInfo(167, "(\\S*); overridden method does not throw (\\S*)"),
        new MessageInfo(168, "(\\S*); attempting to use incompatible return type"),

        // The following are all possible strings for the first argument ({0}) of the
        // above strings.
        new MessageInfo(169, "(\\S*) in (\\S*) cannot override (\\S*) in (\\S*)"),
        new MessageInfo(170, "(\\S*) in (\\S*) cannot implement (\\S*) in (\\S*)"),
        new MessageInfo(171, "(\\S*) in (\\S*) clashes with (\\S*) in (\\S*)")
    };

    private int code = -1;
    private String[] params;
    
    /**
     * Creates a new instance
     *
     * @param ev an error
     */
    public JavacError(ErrorEvent ev) {
        super(ev);
        for (int i = 0; i < MESSAGES.length; i++) {
            params = MESSAGES[i].split(getDetails());
            if (params != null) {
                code = MESSAGES[i].getCode();
                break;
            }
        }
    }
    
    /** 
     * Error code
     *
     * @return error number
     */
    public int getCode() {
        return code;
    }
    
    /**
     * A small test method
     *
     * @param p not used
     *
    public static void main(String[] p) {
        String text = 
            "anonymous class implements interface; cannot have arguments";
        String pattern = 
            "anonymous class implements interface; cannot have arguments";
        MessageInfo mi = new MessageInfo(200, pattern);
        System.out.println(new Date());
        for (int i = 0; i < 1000000; i++) {
            String[] ret = mi.split(text);
        }
        System.out.println(new Date());
        /*if (ret == null)
            System.out.println("does not match");
        else {
            for (int i = 0; i < ret.length; i++) {
                if (ret[i] == null)
                    System.out.println("does not exist");
                else
                    System.out.println("'" + ret[i] + "'");
            }
        }*/
    /*}*/
}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.