Java Code Examples for javax.lang.model.element.ElementKind#EXCEPTION_PARAMETER

The following examples show how to use javax.lang.model.element.ElementKind#EXCEPTION_PARAMETER . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
    long flags = flags();
    if ((flags & PARAMETER) != 0) {
        if (isExceptionParameter())
            return ElementKind.EXCEPTION_PARAMETER;
        else
            return ElementKind.PARAMETER;
    } else if ((flags & ENUM) != 0) {
        return ElementKind.ENUM_CONSTANT;
    } else if (owner.kind == TYP || owner.kind == ERR) {
        return ElementKind.FIELD;
    } else if (isResourceVariable()) {
        return ElementKind.RESOURCE_VARIABLE;
    } else {
        return ElementKind.LOCAL_VARIABLE;
    }
}
 
Example 2
public Object getConstValue() {
    // TODO: Consider if getConstValue and getConstantValue can be collapsed
    if (data == ElementKind.EXCEPTION_PARAMETER ||
        data == ElementKind.RESOURCE_VARIABLE) {
        return null;
    } else if (data instanceof Callable<?>) {
        // In this case, this is a final variable, with an as
        // yet unevaluated initializer.
        Callable<?> eval = (Callable<?>)data;
        data = null; // to make sure we don't evaluate this twice.
        try {
            data = eval.call();
        } catch (Exception ex) {
            throw new AssertionError(ex);
        }
    }
    return data;
}
 
Example 3
Source Project: netbeans   File: UseSpecificCatch.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Determines whether the catch exception parameter is assigned to.
 * 
 * @param ctx HintContext - for CompilationInfo
 * @param variable the inspected variable
 * @param statements statements that should be checked for assignment
 * @return true if 'variable' is assigned to within 'statements'
 */
public static boolean assignsTo(final HintContext ctx, TreePath variable, Iterable<? extends TreePath> statements) {
    final Element tEl = ctx.getInfo().getTrees().getElement(variable);

    if (tEl == null || tEl.getKind() != ElementKind.EXCEPTION_PARAMETER) return true;
    final boolean[] result = new boolean[1];

    for (TreePath tp : statements) {
        new ErrorAwareTreePathScanner<Void, Void>() {
            @Override
            public Void visitAssignment(AssignmentTree node, Void p) {
                if (tEl.equals(ctx.getInfo().getTrees().getElement(new TreePath(getCurrentPath(), node.getVariable())))) {
                    result[0] = true;
                }
                return super.visitAssignment(node, p);
            }
        }.scan(tp, null);
    }

    return result[0];
}
 
Example 4
Source Project: netbeans   File: NPECheck.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public State visitVariable(VariableTree node, Void p) {
    Element e = info.getTrees().getElement(getCurrentPath());
    Map<VariableElement, State> orig = new HashMap<>(variable2State);
    State r = scan(node.getInitializer(), p);
    
    mergeHypotheticalVariable2State(orig);
    
    if (e != null) {
        if (e.getKind() == ElementKind.EXCEPTION_PARAMETER) {
            r = NOT_NULL;
        }
        variable2State.put((VariableElement) e, r);
        TreePath pp = getCurrentPath().getParentPath();
        if (pp != null) {
            addScopedVariable(pp.getLeaf(), (VariableElement)e);
        }
    }
    
    return r;
}
 
Example 5
Source Project: netbeans   File: AssignmentIssues.java    License: Apache License 2.0 6 votes vote down vote up
@Hint(displayName = "#DN_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToCatchBlockParameter", description = "#DESC_org.netbeans.modules.java.hints.AssignmentIssues.assignmentToCatchBlockParameter", category = "assignment_issues", enabled = false, suppressWarnings = "AssignmentToCatchBlockParameter", options=Options.QUERY) //NOI18N
@TriggerTreeKind(Kind.CATCH)
public static List<ErrorDescription> assignmentToCatchBlockParameter(HintContext context) {
    final Trees trees = context.getInfo().getTrees();
    final TreePath catchPath = context.getPath();
    final Element param = trees.getElement(TreePath.getPath(catchPath, ((CatchTree) catchPath.getLeaf()).getParameter()));
    if (param == null || param.getKind() != ElementKind.EXCEPTION_PARAMETER) {
        return null;
    }
    final TreePath block = TreePath.getPath(catchPath, ((CatchTree) catchPath.getLeaf()).getBlock());
    final List<TreePath> paths = new LinkedList<TreePath>();
    new AssignmentFinder(trees, param).scan(block, paths);
    final List<ErrorDescription> ret = new ArrayList<ErrorDescription>(paths.size());
    for (TreePath path : paths) {
        ret.add(ErrorDescriptionFactory.forTree(context, path, NbBundle.getMessage(AssignmentIssues.class, "MSG_AssignmentToCatchBlockParameter", param.getSimpleName()))); //NOI18N
    }
    return ret;
}
 
Example 6
@DefinedBy(Api.LANGUAGE_MODEL)
public ElementKind getKind() {
    long flags = flags();
    if ((flags & PARAMETER) != 0) {
        if (isExceptionParameter())
            return ElementKind.EXCEPTION_PARAMETER;
        else
            return ElementKind.PARAMETER;
    } else if ((flags & ENUM) != 0) {
        return ElementKind.ENUM_CONSTANT;
    } else if (owner.kind == TYP || owner.kind == ERR) {
        return ElementKind.FIELD;
    } else if (isResourceVariable()) {
        return ElementKind.RESOURCE_VARIABLE;
    } else {
        return ElementKind.LOCAL_VARIABLE;
    }
}
 
Example 7
public Object getConstValue() {
    // TODO: Consider if getConstValue and getConstantValue can be collapsed
    if (data == ElementKind.EXCEPTION_PARAMETER ||
        data == ElementKind.RESOURCE_VARIABLE) {
        return null;
    } else if (data instanceof Callable<?>) {
        // In this case, this is a final variable, with an as
        // yet unevaluated initializer.
        Callable<?> eval = (Callable<?>)data;
        data = null; // to make sure we don't evaluate this twice.
        try {
            data = eval.call();
        } catch (Exception ex) {
            throw new AssertionError(ex);
        }
    }
    return data;
}
 
Example 8
Source Project: netbeans   File: GoToSupport.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Void visitVariable(VariableElement e, Boolean highlightName) {
    modifier(e.getModifiers());
    
    result.append(getTypeName(info, e.asType(), true));
    
    result.append(' ');
    
    boldStartCheck(highlightName);

    result.append(e.getSimpleName());
    
    boldStopCheck(highlightName);
    
    if (highlightName) {
        if (e.getConstantValue() != null) {
            result.append(" = ");
            result.append(StringEscapeUtils.escapeHtml(e.getConstantValue().toString()));
        }
        
        Element enclosing = e.getEnclosingElement();
        
        if (e.getKind() != ElementKind.PARAMETER && e.getKind() != ElementKind.LOCAL_VARIABLE
                && e.getKind() != ElementKind.RESOURCE_VARIABLE && e.getKind() != ElementKind.EXCEPTION_PARAMETER
                && !TreeShims.BINDING_VARIABLE.equals(e.getKind().name())) {
            result.append(" in ");

            //short typename:
            result.append(getTypeName(info, enclosing.asType(), true));
        }
    }
    
    return null;
}
 
Example 9
Source Project: netbeans   File: ELHyperlinkProvider.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Void visitVariable(VariableElement e, Boolean highlightName) {
    modifier(e.getModifiers());

    result.append(getTypeName(info, e.asType(), true));

    result.append(' ');

    boldStartCheck(highlightName);

    result.append(e.getSimpleName());

    boldStopCheck(highlightName);

    if (highlightName) {
        if (e.getConstantValue() != null) {
            result.append(" = ");
            result.append(e.getConstantValue().toString());
        }

        Element enclosing = e.getEnclosingElement();

        if (e.getKind() != ElementKind.PARAMETER && e.getKind() != ElementKind.LOCAL_VARIABLE && e.getKind() != ElementKind.EXCEPTION_PARAMETER) {
            result.append(" in ");

            //short typename:
            result.append(getTypeName(info, enclosing.asType(), true));
        }
    }

    return null;
}
 
Example 10
Source Project: j2cl   File: CompilationUnitBuilder.java    License: Apache License 2.0 5 votes vote down vote up
private Expression convertIdent(JCIdent identifier) {
  if (isThisExpression(identifier)) {
    return new ThisReference(getCurrentType().getTypeDescriptor());
  }
  if (isSuperExpression(identifier)) {
    return new SuperReference(getCurrentType().getTypeDescriptor());
  }
  Symbol symbol = identifier.sym;
  if (symbol instanceof ClassSymbol) {
    return new JavaScriptConstructorReference(
        environment.createDeclarationForType((ClassSymbol) identifier.sym));
  }
  if (symbol instanceof MethodSymbol) {
    return NullLiteral.get().withComment(identifier.toString());
  }

  VarSymbol varSymbol = (VarSymbol) symbol;
  if (symbol.getKind() == ElementKind.LOCAL_VARIABLE
      || symbol.getKind() == ElementKind.RESOURCE_VARIABLE
      || symbol.getKind() == ElementKind.PARAMETER
      || symbol.getKind() == ElementKind.EXCEPTION_PARAMETER) {
    Variable variable = variableByVariableElement.get(symbol);
    return resolveVariableReference(variable);
  }

  FieldDescriptor fieldDescriptor = environment.createFieldDescriptor(varSymbol, identifier.type);
  Expression qualifier =
      fieldDescriptor.isStatic()
          ? null
          : resolveOuterClassReference(fieldDescriptor.getEnclosingTypeDescriptor(), false);
  return FieldAccess.newBuilder()
      .setQualifier(qualifier)
      .setTargetFieldDescriptor(fieldDescriptor)
      .build();
}
 
Example 11
public boolean isExceptionParameter() {
    return data == ElementKind.EXCEPTION_PARAMETER;
}
 
Example 12
public boolean isExceptionParameter() {
    return data == ElementKind.EXCEPTION_PARAMETER;
}
 
Example 13
Source Project: j2objc   File: ElementUtil.java    License: Apache License 2.0 4 votes vote down vote up
public static boolean isVariable(Element element) {
  ElementKind kind = element.getKind();
  return kind == ElementKind.FIELD || kind == ElementKind.LOCAL_VARIABLE
      || kind == ElementKind.PARAMETER || kind == ElementKind.EXCEPTION_PARAMETER
      || kind == ElementKind.RESOURCE_VARIABLE || kind == ElementKind.ENUM_CONSTANT;
}