com.sun.tools.javac.code.Lint Java Examples

The following examples show how to use com.sun.tools.javac.code.Lint. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Enter.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
protected Enter(Context context) {
    context.put(enterKey, this);

    log = Log.instance(context);
    reader = ClassReader.instance(context);
    make = TreeMaker.instance(context);
    syms = Symtab.instance(context);
    chk = Check.instance(context);
    memberEnter = MemberEnter.instance(context);
    types = Types.instance(context);
    annotate = Annotate.instance(context);
    lint = Lint.instance(context);
    names = Names.instance(context);

    predefClassDef = make.ClassDef(
        make.Modifiers(PUBLIC),
        syms.predefClass.name, null, null, null, null);
    predefClassDef.sym = syms.predefClass;
    todo = Todo.instance(context);
    fileManager = context.get(JavaFileManager.class);

    Options options = Options.instance(context);
    pkginfoOpt = PkgInfo.get(options);
}
 
Example #2
Source File: Check.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
/** Check for redundant casts (i.e. where source type is a subtype of target type)
 * The problem should only be reported for non-292 cast
 */
public void checkRedundantCast(Env<AttrContext> env, final JCTypeCast tree) {
    if (!tree.type.isErroneous()
            && types.isSameType(tree.expr.type, tree.clazz.type)
            && !(ignoreAnnotatedCasts && TreeInfo.containsTypeAnnotation(tree.clazz))
            && !is292targetTypeCast(tree)) {
        deferredLintHandler.report(new DeferredLintHandler.LintLogger() {
            @Override
            public void report() {
                if (lint.isEnabled(Lint.LintCategory.CAST))
                    log.warning(Lint.LintCategory.CAST,
                            tree.pos(), "redundant.cast", tree.expr.type);
            }
        });
    }
}
 
Example #3
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Flow(Context context) {
    context.put(flowKey, this);
    names = Names.instance(context);
    log = Log.instance(context);
    syms = Symtab.instance(context);
    types = Types.instance(context);
    chk = Check.instance(context);
    lint = Lint.instance(context);
    rs = Resolve.instance(context);
    diags = JCDiagnostic.Factory.instance(context);
    Source source = Source.instance(context);
    allowImprovedRethrowAnalysis = source.allowImprovedRethrowAnalysis();
    allowImprovedCatchAnalysis = source.allowImprovedCatchAnalysis();
    allowEffectivelyFinalInInnerClasses = source.allowEffectivelyFinalInInnerClasses();
    enforceThisDotInit = source.enforceThisDotInit();
}
 
Example #4
Source File: Locations.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
void add(Map<String, List<File>> map, File prefix, File suffix) {
    if (!prefix.isDirectory()) {
        if (warn) {
            String key = prefix.exists()
                    ? "dir.path.element.not.directory"
                    : "dir.path.element.not.found";
            log.warning(Lint.LintCategory.PATH, key, prefix);
        }
        return;
    }

        for (File entry: prefix.listFiles()) {
            if(!entry.isDirectory()) continue;
            File path = (suffix == null) ? entry : FileUtils.resolve(entry,suffix.getPath());
            if (path.isDirectory()) {
                String name = entry.getName();
                List<File> paths = map.get(name);
                if (paths == null)
                    map.put(name, paths = new ArrayList<>());
                paths.add(path);
            }
        }
}
 
Example #5
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitSwitch(JCSwitch tree) {
    ListBuffer<PendingExit> prevPendingExits = pendingExits;
    pendingExits = new ListBuffer<>();
    scan(tree.selector);
    boolean hasDefault = false;
    for (List<JCCase> l = tree.cases; l.nonEmpty(); l = l.tail) {
        alive = true;
        JCCase c = l.head;
        if (c.pat == null)
            hasDefault = true;
        else
            scan(c.pat);
        scanStats(c.stats);
        // Warn about fall-through if lint switch fallthrough enabled.
        if (alive &&
            lint.isEnabled(Lint.LintCategory.FALLTHROUGH) &&
            c.stats.nonEmpty() && l.tail.nonEmpty())
            log.warning(Lint.LintCategory.FALLTHROUGH,
                        l.tail.head.pos(),
                        Warnings.PossibleFallThroughIntoCase);
    }
    if (!hasDefault) {
        alive = true;
    }
    alive |= resolveBreaks(tree, prevPendingExits);
}
 
Example #6
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public void visitVarDef(JCVariableDecl tree) {
    Lint lintPrev = lint;
    lint = lint.augment(tree.sym);
    try{
        boolean track = trackable(tree.sym);
        if (track && tree.sym.owner.kind == MTH) {
            newVar(tree);
        }
        if (tree.init != null) {
            scanExpr(tree.init);
            if (track) {
                letInit(tree.pos(), tree.sym);
            }
        }
    } finally {
        lint = lintPrev;
    }
}
 
Example #7
Source File: Annotate.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
protected Annotate(Context context) {
    context.put(annotateKey, this);

    attr = Attr.instance(context);
    chk = Check.instance(context);
    cfolder = ConstFold.instance(context);
    deferredLintHandler = DeferredLintHandler.instance(context);
    enter = Enter.instance(context);
    log = Log.instance(context);
    lint = Lint.instance(context);
    make = TreeMaker.instance(context);
    names = Names.instance(context);
    resolve = Resolve.instance(context);
    syms = Symtab.instance(context);
    typeEnvs = TypeEnvs.instance(context);
    types = Types.instance(context);

    theUnfinishedDefaultValue =  new Attribute.Error(syms.errType);

    Source source = Source.instance(context);
    allowRepeatedAnnos = source.allowRepeatedAnnotations();
    sourceName = source.name;

    blockCount = 1;
}
 
Example #8
Source File: Check.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
/** Check for redundant casts (i.e. where source type is a subtype of target type)
 * The problem should only be reported for non-292 cast
 */
public void checkRedundantCast(Env<AttrContext> env, final JCTypeCast tree) {
    if (!tree.type.isErroneous()
            && types.isSameType(tree.expr.type, tree.clazz.type)
            && !(ignoreAnnotatedCasts && TreeInfo.containsTypeAnnotation(tree.clazz))
            && !is292targetTypeCast(tree)) {
        deferredLintHandler.report(new DeferredLintHandler.LintLogger() {
            @Override
            public void report() {
                if (lint.isEnabled(Lint.LintCategory.CAST))
                    log.warning(Lint.LintCategory.CAST,
                            tree.pos(), "redundant.cast", tree.expr.type);
            }
        });
    }
}
 
Example #9
Source File: Locations.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
private void addDirectory(File dir, boolean warn) {
    if (!dir.isDirectory()) {
        if (warn)
            log.warning(Lint.LintCategory.PATH,
                    "dir.path.element.not.found", dir);
        return;
    }

    File[] files = dir.listFiles();
    if (files == null)
        return;

    for (File direntry : files) {
        if (isArchive(direntry))
            addFile(direntry, warn);
    }
}
 
Example #10
Source File: Check.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
/** Check for redundant casts (i.e. where source type is a subtype of target type)
 * The problem should only be reported for non-292 cast
 */
public void checkRedundantCast(Env<AttrContext> env, final JCTypeCast tree) {
    if (!tree.type.isErroneous()
            && types.isSameType(tree.expr.type, tree.clazz.type)
            && !(ignoreAnnotatedCasts && TreeInfo.containsTypeAnnotation(tree.clazz))
            && !is292targetTypeCast(tree)) {
        deferredLintHandler.report(new DeferredLintHandler.LintLogger() {
            @Override
            public void report() {
                if (lint.isEnabled(Lint.LintCategory.CAST))
                    log.warning(Lint.LintCategory.CAST,
                            tree.pos(), "redundant.cast", tree.expr.type);
            }
        });
    }
}
 
Example #11
Source File: Check.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
/** Check for redundant casts (i.e. where source type is a subtype of target type)
 * The problem should only be reported for non-292 cast
 */
public void checkRedundantCast(Env<AttrContext> env, final JCTypeCast tree) {
    if (!tree.type.isErroneous()
            && types.isSameType(tree.expr.type, tree.clazz.type)
            && !(ignoreAnnotatedCasts && TreeInfo.containsTypeAnnotation(tree.clazz))
            && !is292targetTypeCast(tree)) {
        deferredLintHandler.report(new DeferredLintHandler.LintLogger() {
            @Override
            public void report() {
                if (lint.isEnabled(Lint.LintCategory.CAST))
                    log.warning(Lint.LintCategory.CAST,
                            tree.pos(), "redundant.cast", tree.expr.type);
            }
        });
    }
}
 
Example #12
Source File: Locations.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private void addDirectory(Path dir, boolean warn) {
    if (!Files.isDirectory(dir)) {
        if (warn) {
            log.warning(Lint.LintCategory.PATH,
                    "dir.path.element.not.found", dir);
        }
        return;
    }

    try (Stream<Path> s = Files.list(dir)) {
        s.filter(Locations.this::isArchive)
                .forEach(dirEntry -> addFile(dirEntry, warn));
    } catch (IOException ignore) {
    }
}
 
Example #13
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitClassDef(JCClassDecl tree) {
    if (tree.sym == null) return;
    boolean alivePrev = alive;
    ListBuffer<PendingExit> pendingExitsPrev = pendingExits;
    Lint lintPrev = lint;

    pendingExits = new ListBuffer<>();
    lint = lint.augment(tree.sym);

    try {
        // process all the static initializers
        for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
            if (!l.head.hasTag(METHODDEF) &&
                (TreeInfo.flags(l.head) & STATIC) != 0) {
                scanDef(l.head);
            }
        }

        // process all the instance initializers
        for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
            if (!l.head.hasTag(METHODDEF) &&
                (TreeInfo.flags(l.head) & STATIC) == 0) {
                scanDef(l.head);
            }
        }

        // process all the methods
        for (List<JCTree> l = tree.defs; l.nonEmpty(); l = l.tail) {
            if (l.head.hasTag(METHODDEF)) {
                scan(l.head);
            }
        }
    } finally {
        pendingExits = pendingExitsPrev;
        alive = alivePrev;
        lint = lintPrev;
    }
}
 
Example #14
Source File: Check.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void checkModuleName (JCModuleDecl tree) {
    Name moduleName = tree.sym.name;
    Assert.checkNonNull(moduleName);
    if (lint.isEnabled(LintCategory.MODULE)) {
        JCExpression qualId = tree.qualId;
        while (qualId != null) {
            Name componentName;
            DiagnosticPosition pos;
            switch (qualId.getTag()) {
                case SELECT:
                    JCFieldAccess selectNode = ((JCFieldAccess) qualId);
                    componentName = selectNode.name;
                    pos = selectNode.pos();
                    qualId = selectNode.selected;
                    break;
                case IDENT:
                    componentName = ((JCIdent) qualId).name;
                    pos = qualId.pos();
                    qualId = null;
                    break;
                default:
                    throw new AssertionError("Unexpected qualified identifier: " + qualId.toString());
            }
            if (componentName != null) {
                String moduleNameComponentString = componentName.toString();
                int nameLength = moduleNameComponentString.length();
                if (nameLength > 0 && Character.isDigit(moduleNameComponentString.charAt(nameLength - 1))) {
                    log.warning(Lint.LintCategory.MODULE, pos, Warnings.PoorChoiceForModuleName(componentName));
                }
            }
        }
    }
}
 
Example #15
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitVarDef(JCVariableDecl tree) {
    if (tree.init != null) {
        Lint lintPrev = lint;
        lint = lint.augment(tree.sym);
        try{
            scan(tree.init);
        } finally {
            lint = lintPrev;
        }
    }
}
 
Example #16
Source File: Check.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** Check that an auxiliary class is not accessed from any other file than its own.
 */
void checkForBadAuxiliaryClassAccess(DiagnosticPosition pos, Env<AttrContext> env, ClassSymbol c) {
    if (lint.isEnabled(Lint.LintCategory.AUXILIARYCLASS) &&
        (c.flags() & AUXILIARY) != 0 &&
        rs.isAccessible(env, c) &&
        !fileManager.isSameFile(c.sourcefile, env.toplevel.sourcefile))
    {
        log.warning(pos, "auxiliary.class.accessed.from.outside.of.its.source.file",
                    c, c.sourcefile);
    }
}
 
Example #17
Source File: BaseFileManager.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the context for JavacPathFileManager.
 */
public void setContext(Context context) {
    log = Log.instance(context);
    options = Options.instance(context);
    classLoaderClass = options.get("procloader");
    locations.update(log, options, Lint.instance(context), FSInfo.instance(context));
}
 
Example #18
Source File: TypeEnter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
Type attribImportType(JCTree tree, Env<AttrContext> env) {
    Assert.check(completionEnabled);
    Lint prevLint = chk.setLint(allowDeprecationOnImport ?
            lint : lint.suppress(LintCategory.DEPRECATION, LintCategory.REMOVAL));
    try {
        // To prevent deep recursion, suppress completion of some
        // types.
        completionEnabled = false;
        return attr.attribType(tree, env);
    } finally {
        completionEnabled = true;
        chk.setLint(prevLint);
    }
}
 
Example #19
Source File: Flow.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void visitVarDef(JCVariableDecl tree) {
    if (tree.init != null) {
        Lint lintPrev = lint;
        lint = lint.augment(tree.sym);
        try{
            scan(tree.init);
        } finally {
            lint = lintPrev;
        }
    }
}
 
Example #20
Source File: Locations.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected void lazy() {
    if (!inited) {
        warn = lint.isEnabled(Lint.LintCategory.PATH);

        for (LocationHandler h: handlersForLocation.values()) {
            h.update(options);
        }

        inited = true;
    }
}
 
Example #21
Source File: BaseFileManager.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Set the context for JavacPathFileManager.
 */
public void setContext(Context context) {
    log = Log.instance(context);
    options = Options.instance(context);
    classLoaderClass = options.get("procloader");
    locations.update(log, options, Lint.instance(context), FSInfo.instance(context));
}
 
Example #22
Source File: Locations.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
protected void lazy() {
    if (!inited) {
        warn = lint.isEnabled(Lint.LintCategory.PATH);

        for (LocationHandler h: handlersForLocation.values()) {
            h.update(options);
        }

        inited = true;
    }
}
 
Example #23
Source File: Enter.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected Enter(Context context) {
    context.put(enterKey, this);

    log = Log.instance(context);
    make = TreeMaker.instance(context);
    syms = Symtab.instance(context);
    chk = Check.instance(context);
    typeEnter = TypeEnter.instance(context);
    types = Types.instance(context);
    annotate = Annotate.instance(context);
    lint = Lint.instance(context);
    names = Names.instance(context);
    modules = Modules.instance(context);
    diags = JCDiagnostic.Factory.instance(context);

    predefClassDef = make.ClassDef(
        make.Modifiers(PUBLIC),
        syms.predefClass.name,
        List.nil(),
        null,
        List.nil(),
        List.nil());
    predefClassDef.sym = syms.predefClass;
    todo = Todo.instance(context);
    fileManager = context.get(JavaFileManager.class);

    Options options = Options.instance(context);
    pkginfoOpt = PkgInfo.get(options);
    typeEnvs = TypeEnvs.instance(context);
}
 
Example #24
Source File: Locations.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public void addFile(File file, boolean warn) {
    if (contains(file)) {
        // discard duplicates
        return;
    }

    if (!fsInfo.exists(file)) {
        /* No such file or directory exists */
        if (warn) {
            log.warning(Lint.LintCategory.PATH,
                    Warnings.PathElementNotFound(file));
        }
        super.add(file);
        return;
    }

    File canonFile = fsInfo.getCanonicalFile(file);
    if (canonicalValues.contains(canonFile)) {
        /* Discard duplicates and avoid infinite recursion */
        return;
    }


    /* Now what we have left is either a directory or a file name
     conforming to archive naming convention */
    super.add(file);
    canonicalValues.add(canonFile);

    if (expandJarClassPaths && fsInfo.isFile(file) ) {
        addJarClassPath(file, warn);
    }
}
 
Example #25
Source File: Locations.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private void addDirectory(File dir, boolean warn) {
    if (!dir.isDirectory()) {
        if (warn) {
            log.warning(Lint.LintCategory.PATH,
                    Warnings.DirPathElementNotFound(dir));
        }
        return;
    }

    for (File dirEntry: dir.listFiles(Locations.this::isArchive)) {
        addFile(dirEntry, warn);
    }
}
 
Example #26
Source File: Option.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static Set<String> getXLintChoices() {
    Set<String> choices = new LinkedHashSet<>();
    choices.add("all");
    for (Lint.LintCategory c : Lint.LintCategory.values()) {
        choices.add(c.option);
        choices.add("-" + c.option);
    }
    choices.add("none");
    return choices;
}
 
Example #27
Source File: Check.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void checkModuleName (JCModuleDecl tree) {
    Name moduleName = tree.sym.name;
    Assert.checkNonNull(moduleName);
    if (lint.isEnabled(LintCategory.MODULE)) {
        JCExpression qualId = tree.qualId;
        while (qualId != null) {
            Name componentName;
            DiagnosticPosition pos;
            switch (qualId.getTag()) {
                case SELECT:
                    JCFieldAccess selectNode = ((JCFieldAccess) qualId);
                    componentName = selectNode.name;
                    pos = selectNode.pos();
                    qualId = selectNode.selected;
                    break;
                case IDENT:
                    componentName = ((JCIdent) qualId).name;
                    pos = qualId.pos();
                    qualId = null;
                    break;
                default:
                    throw new AssertionError("Unexpected qualified identifier: " + qualId.toString());
            }
            if (componentName != null) {
                String moduleNameComponentString = componentName.toString();
                int nameLength = moduleNameComponentString.length();
                if (nameLength > 0 && Character.isDigit(moduleNameComponentString.charAt(nameLength - 1))) {
                    log.warning(Lint.LintCategory.MODULE, pos, Warnings.PoorChoiceForModuleName(componentName));
                }
            }
        }
    }
}
 
Example #28
Source File: ClassReader.java    From lua-for-android with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/** Construct a new class reader. */
protected ClassReader(Context context) {
    context.put(classReaderKey, this);
    annotate = Annotate.instance(context);
    names = Names.instance(context);
    syms = Symtab.instance(context);
    types = Types.instance(context);
    fileManager = context.get(JavaFileManager.class);
    if (fileManager == null)
        throw new AssertionError("FileManager initialization error");
    diagFactory = JCDiagnostic.Factory.instance(context);

    log = Log.instance(context);

    Options options = Options.instance(context);
    verbose         = options.isSet(Option.VERBOSE);

    Source source = Source.instance(context);
    allowSimplifiedVarargs = source.allowSimplifiedVarargs();
    allowModules     = source.allowModules();

    saveParameterNames = options.isSet(PARAMETERS);

    profile = Profile.instance(context);

    typevars = WriteableScope.create(syms.noSymbol);

    lintClassfile = Lint.instance(context).isEnabled(LintCategory.CLASSFILE);

    initAttributeReaders();
}
 
Example #29
Source File: Check.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** Check that an auxiliary class is not accessed from any other file than its own.
 */
void checkForBadAuxiliaryClassAccess(DiagnosticPosition pos, Env<AttrContext> env, ClassSymbol c) {
    if (lint.isEnabled(Lint.LintCategory.AUXILIARYCLASS) &&
        (c.flags() & AUXILIARY) != 0 &&
        rs.isAccessible(env, c) &&
        !fileManager.isSameFile(c.sourcefile, env.toplevel.sourcefile))
    {
        log.warning(pos, "auxiliary.class.accessed.from.outside.of.its.source.file",
                    c, c.sourcefile);
    }
}
 
Example #30
Source File: JavacFiler.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
JavacFiler(Context context) {
    this.context = context;
    fileManager = context.get(JavaFileManager.class);
    elementUtils = JavacElements.instance(context);

    log = Log.instance(context);
    modules = Modules.instance(context);
    names = Names.instance(context);
    syms = Symtab.instance(context);

    initialInputs = synchronizedSet(new LinkedHashSet<>());
    fileObjectHistory = synchronizedSet(new LinkedHashSet<>());
    generatedSourceNames = synchronizedSet(new LinkedHashSet<>());
    generatedSourceFileObjects = synchronizedSet(new LinkedHashSet<>());

    generatedClasses = synchronizedMap(new LinkedHashMap<>());

    openTypeNames  = synchronizedSet(new LinkedHashSet<>());

    aggregateGeneratedSourceNames = new LinkedHashSet<>();
    aggregateGeneratedClassNames  = new LinkedHashSet<>();
    initialClassNames  = new LinkedHashSet<>();

    lint = (Lint.instance(context)).isEnabled(PROCESSING);

    Options options = Options.instance(context);

    defaultTargetModule = options.get(Option.DEFAULT_MODULE_FOR_CREATED_FILES);
}