Java Code Examples for javax.lang.model.type.TypeKind#EXECUTABLE

The following examples show how to use javax.lang.model.type.TypeKind#EXECUTABLE . 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: ElementUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean isHidden(Element member, List<? extends Element> members, Elements elements, Types types) {
    for (ListIterator<? extends Element> it = members.listIterator(); it.hasNext();) {
        Element hider = it.next();
        if (hider == member)
            return true;
        if (hider.getSimpleName().contentEquals(member.getSimpleName())) {
            if (elements.hides(member, hider)) {
                it.remove();
            } else {
                if (member instanceof VariableElement && hider instanceof VariableElement
                        && (!member.getKind().isField() || hider.getKind().isField()))
                    return true;
                TypeMirror memberType = member.asType();
                TypeMirror hiderType = hider.asType();
                if (memberType.getKind() == TypeKind.EXECUTABLE && hiderType.getKind() == TypeKind.EXECUTABLE) {
                    if (types.isSubsignature((ExecutableType)hiderType, (ExecutableType)memberType))
                        return true;
                } else {
                    return false;
                }
            }
        }
    }
    return false;
}
 
Example 2
Source File: KratosProcessor.java    From Kratos with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void parseOnKStringUpdate(Element element, Map<TypeElement, BindingClass> targetClassMap,
                                  Set<String> erasedTargetNames) {
    TypeElement enclosingElement = (TypeElement) element.getEnclosingElement();
    BindingClass bindingClass = getOrCreateTargetClass(targetClassMap, enclosingElement, false, false);
    TypeMirror mirror = element.asType();
    if (!(mirror.getKind() == TypeKind.EXECUTABLE))
        return;
    String method = element.toString().trim();
    String methodName = method.substring(0, method.indexOf("("));
    Matcher m = Pattern.compile("\\(([^)]+)\\)").matcher(method);
    if (m.find()) {
        String[] methodTypes = m.group(1).split(",");
        UpdateKStringBinding binding = new UpdateKStringBinding(methodName, methodTypes);
        String kstring = element.getAnnotation(OnKStringChanged.class).value();
        bindingClass.addKStringUpdateBinding(kstring, binding);
    }
    erasedTargetNames.add(enclosingElement.toString());
}
 
Example 3
Source File: TreeConverter.java    From j2objc with Apache License 2.0 6 votes vote down vote up
private Type convertType(TypeMirror typeMirror) {
  com.sun.tools.javac.code.Type type = (com.sun.tools.javac.code.Type) typeMirror;
  if (type.getKind() == TypeKind.EXECUTABLE) {
    Type returnType = Type.newType(type.getReturnType());
    if (type.hasTag(TypeTag.FORALL)) {
      return new ParameterizedType().setType(returnType).setTypeMirror(type.getReturnType());
    } else {
      return returnType;
    }
  }
  if (type.getKind() == TypeKind.DECLARED) {
    List<? extends TypeMirror> typeArgs = ((DeclaredType) type).getTypeArguments();
    if (!typeArgs.isEmpty()) {
      return new ParameterizedType().setType(Type.newType(typeMirror)).setTypeMirror(typeMirror);
    }
  }
  return Type.newType(type);
}
 
Example 4
Source File: TypeUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Find the type of the method descriptor associated to the functional interface.
 * 
 * @param origin functional interface type
 * @return associated method descriptor type or <code>null</code> if the <code>origin</code> is not a functional interface.
 * @since 0.112
 */
public ExecutableType getDescriptorType(DeclaredType origin) {
    Types types = Types.instance(info.impl.getJavacTask().getContext());
    if (types.isFunctionalInterface(((Type)origin).tsym)) {
        Type dt = types.findDescriptorType((Type)origin);
        if (dt != null && dt.getKind() == TypeKind.EXECUTABLE) {
            return (ExecutableType)dt;
        }
    }
    return null;
}
 
Example 5
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static List<? extends TypeMirror> computeAssignment(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    AssignmentTree at = (AssignmentTree) parent.getLeaf();
    TypeMirror     type = null;
    
    if (at.getVariable() == error) {
        type = info.getTrees().getTypeMirror(new TreePath(parent, at.getExpression()));

        if (type != null) {
            //anonymous class?
            type = JavaPluginUtils.convertIfAnonymous(type);

            if (type.getKind() == TypeKind.EXECUTABLE) {
                //TODO: does not actualy work, attempt to solve situations like:
                //t = Collections.emptyList()
                //t = Collections.<String>emptyList();
                //see also testCreateFieldMethod1 and testCreateFieldMethod2 tests:
                type = ((ExecutableType) type).getReturnType();
            }
        }
    }
    
    if (at.getExpression() == error) {
        type = info.getTrees().getTypeMirror(new TreePath(parent, at.getVariable()));
    }
    
    //class or field:
    if (type == null) {
        return null;
    }
    
    types.add(ElementKind.PARAMETER);
    types.add(ElementKind.LOCAL_VARIABLE);
    types.add(ElementKind.FIELD);
    
    return Collections.singletonList(type);
}
 
Example 6
Source File: ConvertToDiamondBulkHint.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Checks whether an ambiguous overload exists. Javacs prior to JDK9 use imperfect
 * inference for type parameters, which causes more overloads to match the invocation,
 * therefore causing an error during compilation. If the diamond hint was applied
 * in such a case, the result would not be error-highlighted in editor, but would
 * fail to compile using JDK &lt; 9.
 * <p/>
 * The check is very rough, so it should not be used to generate errors as older
 * javacs do.
 * <p/>
 * See defect #248162
 */
private static boolean checkAmbiguousOverload(CompilationInfo info, TreePath newPath) {
    if (info.getSourceVersion().compareTo(SourceVersion.RELEASE_8) > 0) {
        return false;
    }
    Element el = info.getTrees().getElement(newPath);
    if (el == null || el.getKind() != ElementKind.CONSTRUCTOR) {
        return false;
    }
    ExecutableElement ctor = (ExecutableElement)el;
    DeclaredType resolvedType = (DeclaredType)info.getTrees().getTypeMirror(newPath);
    ExecutableType ctorType = (ExecutableType)info.getTypes().asMemberOf(resolvedType, el);
    for (ExecutableElement ee : ElementFilter.constructorsIn(el.getEnclosingElement().getEnclosedElements())) {
        if (ee == el) {
            continue;
        }
        if (ee.getParameters().size() != ctor.getParameters().size()) {
            continue;
        }
        TypeMirror t = info.getTypes().asMemberOf(resolvedType, ee);
        if (!Utilities.isValidType(t) || t.getKind() != TypeKind.EXECUTABLE) {
            continue;
        }
        ExecutableType et = (ExecutableType)t;
        for (int i = 0; i < ee.getParameters().size(); i++) {
            TypeMirror earg = et.getParameterTypes().get(i);
            TypeMirror carg = ctorType.getParameterTypes().get(i);
            if (!earg.getKind().isPrimitive() && !carg.getKind().isPrimitive()) {
                TypeMirror erasedC = info.getTypes().erasure(carg);
                TypeMirror erasedE = info.getTypes().erasure(earg);
                if (info.getTypes().isAssignable(erasedC, earg) && 
                    !info.getTypes().isSameType(erasedC, erasedE)) {
                    // invalid hint here!
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 7
Source File: ChangeMethodReturnType.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
    TreePath parentPath = treePath.getParentPath();
    if (parentPath == null || parentPath.getLeaf().getKind() != Kind.RETURN) return null;
    
    TreePath method = null;
    TreePath tp = treePath;

    while (tp != null && !TreeUtilities.CLASS_TREE_KINDS.contains(tp.getLeaf().getKind())) {
        if (tp.getLeaf().getKind() == Kind.METHOD) {
            method = tp;
            break;
        }

        tp = tp.getParentPath();
    }

    if (method == null) return null;

    MethodTree mt = (MethodTree) tp.getLeaf();

    if (mt.getReturnType() == null) return null;

    TypeMirror targetType = purify(info, info.getTrees().getTypeMirror(treePath));

    if (targetType == null) return null;

    if (targetType.getKind() == TypeKind.EXECUTABLE) {
        String expression = info.getText().substring((int) info.getTrees().getSourcePositions().getStartPosition(info.getCompilationUnit(), treePath.getLeaf()), (int) info.getTrees().getSourcePositions().getEndPosition(info.getCompilationUnit(), treePath.getLeaf()));
        Scope s = info.getTrees().getScope(treePath);
        ExpressionTree expr = info.getTreeUtilities().parseExpression(expression, new SourcePositions[1]);

        targetType = purify(info, info.getTreeUtilities().attributeTree(expr, s));
    }

    if (targetType == null || targetType.getKind() == TypeKind.EXECUTABLE) return null;

    return Collections.singletonList(new FixImpl(info, method, TypeMirrorHandle.create(targetType), info.getTypeUtilities().getTypeName(targetType).toString()).toEditorFix());
}
 
Example 8
Source File: VarArgsCast.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public List<Fix> run(CompilationInfo info, String diagnosticKey, int offset, TreePath treePath, Data<Void> data) {
    TreePath call = treePath;//.getParentPath();
    
    if (call.getLeaf().getKind() != Kind.METHOD_INVOCATION) {
        call = call.getParentPath();
        if (call.getLeaf().getKind() != Kind.METHOD_INVOCATION) {
            return null;
        }
    }
    
    MethodInvocationTree mit = (MethodInvocationTree) call.getLeaf();
    TypeMirror mType = info.getTrees().getTypeMirror(new TreePath(call, mit.getMethodSelect()));
    
    if (mType == null || mType.getKind() != TypeKind.EXECUTABLE) {
        return null;
    }
    
    ExecutableType methodType = (ExecutableType) mType;
    
    if (methodType.getParameterTypes().isEmpty() || methodType.getParameterTypes().get(methodType.getParameterTypes().size() - 1).getKind() != TypeKind.ARRAY) {
        return null;
    }
    
    ArrayType targetArray = (ArrayType) methodType.getParameterTypes().get(methodType.getParameterTypes().size() - 1);
    TreePath target = new TreePath(call, mit.getArguments().get(mit.getArguments().size() - 1));
    
    return Arrays.asList(new FixImpl(info, target, targetArray.getComponentType()).toEditorFix(),
                         new FixImpl(info, target, targetArray).toEditorFix());
}
 
Example 9
Source File: StandaloneExecutableType.java    From buck with Apache License 2.0 5 votes vote down vote up
public StandaloneExecutableType(
    TypeMirror returnType,
    List<? extends TypeVariable> typeVariables,
    List<? extends TypeMirror> parameterTypes,
    List<? extends TypeMirror> thrownTypes,
    List<? extends AnnotationMirror> annotations) {
  super(TypeKind.EXECUTABLE, annotations);
  this.returnType = returnType;
  this.typeVariables = Collections.unmodifiableList(new ArrayList<>(typeVariables));
  this.parameterTypes = Collections.unmodifiableList(new ArrayList<>(parameterTypes));
  this.thrownTypes = Collections.unmodifiableList(new ArrayList<>(thrownTypes));
}
 
Example 10
Source File: CreateElementUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static List<? extends TypeMirror> computeMethodInvocation(Set<ElementKind> types, CompilationInfo info, TreePath parent, Tree error, int offset) {
    MethodInvocationTree nat = (MethodInvocationTree) parent.getLeaf();
    int realArgumentError = -1;
    int i = 0;
    for (Tree param : nat.getArguments()) {
        if (param == error) {
            realArgumentError = i;
            break;
        }
        i++;
    }
    
    if (realArgumentError != (-1)) {
        List<TypeMirror> proposedTypes = new ArrayList<TypeMirror>();
        int[] proposedIndex = new int[1];
        List<ExecutableElement> ee = org.netbeans.modules.editor.java.Utilities.fuzzyResolveMethodInvocation(info, parent, proposedTypes, proposedIndex);
        
        if (ee.isEmpty()) { //cannot be resolved
            TypeMirror executable = info.getTrees().getTypeMirror(new TreePath(parent, nat.getMethodSelect()));
            
            if (executable == null || executable.getKind() != TypeKind.EXECUTABLE) return null;
            
            ExecutableType et = (ExecutableType) executable;
            
            if (realArgumentError >= et.getParameterTypes().size()) {
                return null;
            }
            
            proposedTypes.add(et.getParameterTypes().get(realArgumentError));
        }
        
        types.add(ElementKind.PARAMETER);
        types.add(ElementKind.LOCAL_VARIABLE);
        types.add(ElementKind.FIELD);
        
        return proposedTypes;
    }
    
    return null;
}
 
Example 11
Source File: JDIWrappersTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private TypeMirror adjustTypeMirror(TypeMirror tm) {
    if (tm.getKind() == TypeKind.EXECUTABLE) {
        tm = ((ExecutableType) tm).getReturnType();
        tm = adjustTypeMirror(tm);
    } else if (tm.getKind() == TypeKind.ARRAY) {
        tm = ((ArrayType) tm).getComponentType();
        tm = adjustTypeMirror(tm);
    }
    return tm;
}
 
Example 12
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public List<? extends TypeMirror> visitMethodInvocation(MethodInvocationTree node, Object p) {
    TypeMirror execType = info.getTrees().getTypeMirror(
            new TreePath(getCurrentPath(), node.getMethodSelect()));
    if (execType == null || execType.getKind() != TypeKind.EXECUTABLE) {
        return null;
    }
    return visitMethodOrNew(node, p, node.getArguments(),
            (ExecutableType)execType);
}
 
Example 13
Source File: TurbineTypeMirror.java    From turbine with Apache License 2.0 4 votes vote down vote up
@Override
public TypeKind getKind() {
  return TypeKind.EXECUTABLE;
}
 
Example 14
Source File: Type.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public TypeKind getKind() {
    return TypeKind.EXECUTABLE;
}
 
Example 15
Source File: Type.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public TypeKind getKind() {
    return TypeKind.EXECUTABLE;
}
 
Example 16
Source File: Type.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
public TypeKind getKind() {
    return TypeKind.EXECUTABLE;
}
 
Example 17
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static boolean isValidValueType(TypeMirror m) {
    return isValidType(m) && m.getKind() != TypeKind.EXECUTABLE;
}
 
Example 18
Source File: ExecutableTypeImpl.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public TypeKind getKind() {
	return TypeKind.EXECUTABLE;
}
 
Example 19
Source File: GeneratedExecutableElement.java    From j2objc with Apache License 2.0 4 votes vote down vote up
@Override
public TypeKind getKind() {
  return TypeKind.EXECUTABLE;
}
 
Example 20
Source File: Type.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
public TypeKind getKind() {
    return TypeKind.EXECUTABLE;
}