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

The following examples show how to use javax.lang.model.type.TypeKind#DECLARED . 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: ResourceStringFoldProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private String bundleFileFromClass(TreePath classTreePath, String bfn) {
    TypeMirror tm = info.getTrees().getTypeMirror(classTreePath);
    if (tm.getKind() != TypeKind.DECLARED) {
        return null;
    }
    Element clazz = ((DeclaredType)tm).asElement();
    while ((clazz instanceof TypeElement)) {
        clazz = ((TypeElement)clazz).getEnclosingElement();
    }
    if (clazz.getKind() == ElementKind.PACKAGE) {
        PackageElement pack = ((PackageElement)clazz);
        if (pack.isUnnamed()) {
            return null;
        }
        return pack.getQualifiedName().toString().replaceAll("\\.", "/") + "/" + bfn;
    }
    return null;
}
 
Example 2
Source File: BeanModelBuilder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addDependency(TypeMirror tm) {
    if (tm.getKind() == TypeKind.ARRAY) {
        addDependency(((ArrayType)tm).getComponentType());
    } else if (tm.getKind() == TypeKind.WILDCARD) {
        WildcardType wt = (WildcardType)tm;
        TypeMirror bound = wt.getSuperBound();
        if (bound == null) {
            bound = wt.getExtendsBound();
        }
        addDependency(bound);
    } else if (tm.getKind() == TypeKind.DECLARED) {
        addDependency(
            ((TypeElement)compilationInfo.getTypes().asElement(tm)).getQualifiedName().toString()
        );
    }
}
 
Example 3
Source File: JavaSymbolProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder visitWildcard(WildcardType t, Boolean p) {
    int len = DEFAULT_VALUE.length();
    DEFAULT_VALUE.append("?"); //NOI18N
    TypeMirror bound = t.getSuperBound();
    if (bound == null) {
        bound = t.getExtendsBound();
        if (bound != null) {
            DEFAULT_VALUE.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD) {
                bound = ((WildcardType)bound).getSuperBound();
            }
            visit(bound, p);
        } else if (len == 0) {
            bound = SourceUtils.getBound(t);
            if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
                DEFAULT_VALUE.append(" extends "); //NOI18N
                visit(bound, p);
            }
        }
    } else {
        DEFAULT_VALUE.append(" super "); //NOI18N
        visit(bound, p);
    }
    return DEFAULT_VALUE;
}
 
Example 4
Source File: EntityClassInfo.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void setType(TypeMirror type, CompilationController controller) {
    if (type.getKind() == TypeKind.DECLARED) {
        DeclaredType declType = (DeclaredType) type;
        Element typeElement = declType.asElement();
        
        initTypeElement(type, controller);
        
        setType(typeElement.toString(), controller);
        for (TypeMirror arg : declType.getTypeArguments()) {
            setTypeArg(arg.toString());
        }
    }
    else if(type.getKind() == TypeKind.ARRAY) {
        isArray = true;
        TypeMirror componentType = ((ArrayType)type).getComponentType();
        
        if ( componentType.getKind() == TypeKind.DECLARED){
            initTypeElement(componentType, controller);
        }
        setType(type.toString(), controller);
    }
    else {
        setType(type.toString(), controller);
    }
}
 
Example 5
Source File: GeneratorUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static List<TypeElement> getAllClasses(TypeElement of) {
    List<TypeElement> result = new ArrayList<>();
    TypeMirror sup = of.getSuperclass();
    TypeElement te = sup.getKind() == TypeKind.DECLARED ? (TypeElement) ((DeclaredType)sup).asElement() : null;
    
    result.add(of);
    
    if (te != null) {
        result.addAll(getAllClasses(te));
    } else {
        if (ERR.isLoggable(ErrorManager.INFORMATIONAL)) {
            ERR.log(ErrorManager.INFORMATIONAL, "te=null, t=" + of);
        }
    }
    
    return result;
}
 
Example 6
Source File: ConstantNameHint.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isImmutableType(CompilationInfo info, TypeMirror m, Preferences p) {
    if (m == null) {
        return false;
    }
    if (m.getKind().isPrimitive() || !isValidType(m)) {
        return true;
    }
    if (Utilities.isPrimitiveWrapperType(m)) {
        return true;
    }
    if (m.getKind() != TypeKind.DECLARED) {
        return false;
    }
    Element e = ((DeclaredType)m).asElement();
    if (e == null) {
        return false;
    }
    if (e.getKind() == ElementKind.ENUM) {
        return true;
    }
    if (e.getKind() != ElementKind.CLASS) {
        return false;
    }
    String qn = ((TypeElement)e).getQualifiedName().toString();
    
    if (IMMUTABLE_JDK_CLASSES.contains(qn)) {
        return true;
    }
    List<String> classes = getImmutableTypes(p);
    return classes.contains(qn);
}
 
Example 7
Source File: LoggerGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public List<? extends CodeGenerator> create(Lookup context) {
    ArrayList<CodeGenerator> ret = new ArrayList<>();
    JTextComponent component = context.lookup(JTextComponent.class);
    CompilationController controller = context.lookup(CompilationController.class);
    if (component == null || controller == null) {
        return ret;
    }
    TreePath path = context.lookup(TreePath.class);
    path = controller.getTreeUtilities().getPathElementOfKind(TreeUtilities.CLASS_TREE_KINDS, path);
    if (path == null) {
        return ret;
    }
    try {
        controller.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
    } catch (IOException ioe) {
        return ret;
    }
    TypeElement typeElement = (TypeElement) controller.getTrees().getElement(path);
    if (typeElement == null || !typeElement.getKind().isClass()) {
        return ret;
    }
    for (VariableElement ve : ElementFilter.fieldsIn(typeElement.getEnclosedElements())) {
        TypeMirror type = ve.asType();
        if (type.getKind() == TypeKind.DECLARED && ((TypeElement)((DeclaredType)type).asElement()).getQualifiedName().contentEquals(Logger.class.getName())) {
            return ret;
        }
    }
    List<ElementNode.Description> descriptions = new ArrayList<>();
    ret.add(new LoggerGenerator(component, ElementNode.Description.create(controller, typeElement, descriptions, false, false)));
    return ret;
}
 
Example 8
Source File: ComponentProcessor.java    From appinventor-extensions with Apache License 2.0 5 votes vote down vote up
/**
 * Tracks the superclass and superinterfaces for the given type and if the type inherits from
 * {@link com.google.appinventor.components.runtime.Component} then it adds the class to the
 * componentTypes list. This allows properties, methods, and events to use concrete Component
 * types as parameters and return values.
 *
 * @param type a TypeMirror representing a type on the class path
 */
private void updateComponentTypes(TypeMirror type) {
  if (type.getKind() == TypeKind.DECLARED) {
    type.accept(new SimpleTypeVisitor7<Boolean, Set<String>>(false) {
      @Override
      public Boolean visitDeclared(DeclaredType t, Set<String> types) {
        final String typeName = t.asElement().toString();
        if ("com.google.appinventor.components.runtime.Component".equals(typeName)) {
          return true;
        }
        if (!types.contains(typeName)) {
          types.add(typeName);
          final TypeElement typeElement = (TypeElement) t.asElement();
          if (typeElement.getSuperclass().accept(this, types)) {
            componentTypes.add(typeName);
            return true;
          }
          for (TypeMirror iface : typeElement.getInterfaces()) {
            if (iface.accept(this, types)) {
              componentTypes.add(typeName);
              return true;
            }
          }
        }
        return componentTypes.contains(typeName);
      }
    }, visitedTypes);
  }
}
 
Example 9
Source File: Utils.java    From FastAdapter with MIT License 5 votes vote down vote up
public static boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
  if (otherType.equals(typeMirror.toString())) {
    return true;
  }
  if (typeMirror.getKind() != TypeKind.DECLARED) {
    return false;
  }
  DeclaredType declaredType = (DeclaredType) typeMirror;
  List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
  if (typeArguments.size() > 0) {
    StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
    typeString.append('<');
    for (int i = 0; i < typeArguments.size(); i++) {
      if (i > 0) {
        typeString.append(',');
      }
      typeString.append('?');
    }
    typeString.append('>');

    if (typeString.toString().equals(otherType)) {
      return true;
    }
  }
  Element element = declaredType.asElement();
  if (!(element instanceof TypeElement)) {
    return false;
  }
  TypeElement typeElement = (TypeElement) element;
  TypeMirror superType = typeElement.getSuperclass();

  if (isSubtypeOfType(superType, otherType)) {
    return true;
  }
  return false;
}
 
Example 10
Source File: IntroduceLocalExtensionTransformer.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private List<Tree> addInterfaces(TypeElement source) {
    List<Tree> implementsList = new ArrayList<>();
    Set<String> implemented = new HashSet<>();
    TypeElement typeElement = source;
    while (typeElement != null) {
        for (TypeMirror typeMirror : typeElement.getInterfaces()) {
            Tree type = make.Type(typeMirror);
            if (typeMirror.getKind() == TypeKind.DECLARED) {
                DeclaredType declaredType = (DeclaredType) typeMirror;
                List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
                List<Tree> newTypeParams = new LinkedList<>();
                for (TypeMirror typeParam : typeArguments) {
                    Element element = workingCopy.getTypes().asElement(typeParam);
                    if (source.equals(element)) {
                        newTypeParams.add(make.QualIdent(fqn));
                    } else {
                        newTypeParams.add(make.Type(typeParam));
                    }
                }
                if (!newTypeParams.isEmpty()) {
                    type = make.ParameterizedType(make.QualIdent(declaredType.asElement()), newTypeParams);
                }
            }
            if (implemented.add(typeMirror.toString())) {
                implementsList.add(type);
            }
        }
        TypeMirror superclass = typeElement.getSuperclass();
        if (superclass.getKind() != TypeKind.NONE) {
            typeElement = (TypeElement) workingCopy.getTypes().asElement(superclass);
        } else {
            typeElement = null;
        }
    }
    return implementsList;
}
 
Example 11
Source File: ControllerGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public TypeMirror visitTypeVariable(TypeVariable t, CompilationInfo p) {
    TypeMirror lb = t.getLowerBound() == null ? null : visit(t.getLowerBound(), p);
    TypeMirror ub = t.getUpperBound() == null ? null : visit(t.getUpperBound(), p);
    if (ub.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType)ub;
        TypeElement tel = (TypeElement)dt.asElement();
        if (tel.getQualifiedName().contentEquals("java.lang.Object")) { // NOI18N
            ub = null;
        } else if (tel.getSimpleName().length() == 0) {
            ub = null;
        }
    }
    return p.getTypes().getWildcardType(ub, lb);
}
 
Example 12
Source File: ElementHandle.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Gets {@link ElementHandle} from {@link TypeMirrorHandle} representing {@link DeclaredType}.
 * @param typeMirrorHandle from which the {@link ElementHandle} should be retrieved. Permitted
 * {@link TypeKind} is {@link TypeKind#DECLARED}.
 * @return an {@link ElementHandle}
 * @since 0.29.0
 */
public static @NonNull ElementHandle<? extends TypeElement> from (@NonNull final TypeMirrorHandle<? extends DeclaredType> typeMirrorHandle) {
    Parameters.notNull("typeMirrorHandle", typeMirrorHandle);
    if (typeMirrorHandle.getKind() != TypeKind.DECLARED) {
        throw new IllegalStateException("Incorrect kind: " + typeMirrorHandle.getKind());
    }
    return (ElementHandle<TypeElement>)typeMirrorHandle.getElementHandle();
}
 
Example 13
Source File: AptDeserializerBuilder.java    From domino-jackson with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected Set<MethodSpec> moreMethods() {
    Set<MethodSpec> methods = new HashSet<>();
    // Object instance can be created by InstanceBuilder
    // only for non-abstract classes
    if (beanType.getKind() == TypeKind.DECLARED
            && ((DeclaredType) beanType).asElement().getKind() == ElementKind.CLASS
            && !((DeclaredType) beanType).asElement().getModifiers().contains(Modifier.ABSTRACT)) {
        methods.add(buildInitInstanceBuilderMethod());
    }

    MethodSpec initIgnoreFieldsMethod = buildInitIgnoreFields(beanType);
    if (nonNull(initIgnoreFieldsMethod)) {
        methods.add(initIgnoreFieldsMethod);
    }

    JsonIgnoreProperties ignorePropertiesAnnotation = typeUtils.asElement(beanType).getAnnotation(JsonIgnoreProperties.class);

    if (nonNull(ignorePropertiesAnnotation)) {
        methods.add(buildIgnoreUnknownMethod(ignorePropertiesAnnotation.ignoreUnknown()));
    }

    Optional<BeanIdentityInfo> beanIdentityInfo = Type.processIdentity(beanType);
    if(beanIdentityInfo.isPresent()){
        methods.add(buildInitIdentityInfoMethod(beanIdentityInfo.get()));
    }

    return methods;
}
 
Example 14
Source File: JavaOperationsImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@CheckForNull
public String getSuperClass(@NonNull final String cls) throws QueryException {
    final TypeElement te = findClass(cls);
    if (te == null) {
        return null;
    }
    final TypeMirror superType = te.getSuperclass();
    if (superType.getKind() != TypeKind.DECLARED) {
        return null;
    }
    return ((TypeElement)((DeclaredType)superType).asElement()).getQualifiedName().toString();
}
 
Example 15
Source File: JsfTable.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/** @param commands a message that will be added to the end of each line in table,
 *  it will be formated using {0} = iterator variable
 */
public static void createTable(CompilationController controller, TypeElement bean, String variable, StringBuffer stringBuffer,
        String commands, JpaControllerUtil.EmbeddedPkSupport embeddedPkSupport, String tableVarName) {
    if (tableVarName == null) {
        tableVarName = "item"; //NOI18N
    }
    int formType = 1;
    TypeMirror dateTypeMirror = controller.getElements().getTypeElement("java.util.Date").asType();
    if (bean != null) {
        ExecutableElement[] methods = JpaControllerUtil.getEntityMethods(bean);
        boolean fieldAccess = JpaControllerUtil.isFieldAccess(bean);
        for (ExecutableElement method : methods) {
            String methodName = method.getSimpleName().toString();
            if (methodName.startsWith("get")) {
                int isRelationship = JpaControllerUtil.isRelationship(method, fieldAccess);
                String name = methodName.substring(3);
                String propName = JpaControllerUtil.getPropNameFromMethod(methodName);
                if (EntityClass.isId(method, fieldAccess)) {
                    TypeMirror rType = method.getReturnType();
                    if (TypeKind.DECLARED == rType.getKind()) {
                        DeclaredType rTypeDeclared = (DeclaredType)rType;
                        TypeElement rTypeElement = (TypeElement) rTypeDeclared.asElement();
                        if (JpaControllerUtil.isEmbeddableClass(rTypeElement)) {
                            if (embeddedPkSupport == null) {
                                embeddedPkSupport = new JpaControllerUtil.EmbeddedPkSupport();
                            }
                            for (ExecutableElement pkMethod : embeddedPkSupport.getPkAccessorMethods(bean)) {
                                if (!embeddedPkSupport.isRedundantWithRelationshipField(bean, pkMethod)) {
                                    String pkMethodName = pkMethod.getSimpleName().toString();
                                    String pkPropTitle = pkMethodName.substring(3);
                                    String pkPropName = propName + "." + JpaControllerUtil.getPropNameFromMethod(pkMethodName);
                                    stringBuffer.append(MessageFormat.format(ITEM [1], new Object [] {pkPropTitle, null, pkPropName, tableVarName}));
                                }
                            }
                        }
                        else {
                            stringBuffer.append(MessageFormat.format(ITEM [1], new Object [] {name, variable, propName, tableVarName}));
                        }
                    }
                } else if (controller.getTypes().isSameType(dateTypeMirror, method.getReturnType())) {
                    //param 3 - temporal, param 4 - date/time format
                    String temporal = EntityClass.getTemporal(method, fieldAccess);
                    if (temporal == null) {
                        stringBuffer.append(MessageFormat.format(ITEM [formType], new Object [] {name, variable, propName, tableVarName}));
                    } else {
                        stringBuffer.append(MessageFormat.format(ITEM [2], new Object [] {name, variable, propName, temporal, EntityClass.getDateTimeFormat(temporal), tableVarName}));
                    }
                } else if (isRelationship == JpaControllerUtil.REL_NONE || isRelationship == JpaControllerUtil.REL_TO_ONE) {
                    stringBuffer.append(MessageFormat.format(ITEM [formType], new Object [] {name, variable, propName, tableVarName}));
                }
            }
        }
    }
    stringBuffer.append(MessageFormat.format(commands, new Object [] {tableVarName}));
}
 
Example 16
Source File: Utilities.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Note: may return {@code null}, if an intersection type is encountered, to indicate a 
 * real type cannot be created.
 */
private static TypeMirror resolveCapturedTypeInt(CompilationInfo info, TypeMirror tm) {
    if (tm == null) return tm;
    
    TypeMirror orig = SourceUtils.resolveCapturedType(tm);

    if (orig != null) {
        tm = orig;
    }
    
    if (tm.getKind() == TypeKind.WILDCARD) {
        TypeMirror extendsBound = ((WildcardType) tm).getExtendsBound();
        TypeMirror superBound = ((WildcardType) tm).getSuperBound();
        if (extendsBound != null || superBound != null) {
            TypeMirror rct = resolveCapturedTypeInt(info, extendsBound != null ? extendsBound : superBound);
            if (rct != null) {
                switch (rct.getKind()) {
                    case WILDCARD:
                        return rct;
                    case ARRAY:
                    case DECLARED:
                    case ERROR:
                    case TYPEVAR:
                    case OTHER:
                        return info.getTypes().getWildcardType(
                                extendsBound != null ? rct : null, superBound != null ? rct : null);
                }
            } else {
                // propagate failure out of all wildcards
                return null;
            }
        }
    } else if (tm.getKind() == TypeKind.INTERSECTION) {
        return null;
    }
    
    if (tm.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) tm;
        List<TypeMirror> typeArguments = new LinkedList<TypeMirror>();
        
        for (TypeMirror t : dt.getTypeArguments()) {
            TypeMirror targ = resolveCapturedTypeInt(info, t);
            if (targ == null) {
                // bail out, if the type parameter is a wildcard, it's probably not possible
                // to create a proper parametrized type from it
                if (t.getKind() == TypeKind.WILDCARD || t.getKind() == TypeKind.INTERSECTION) {
                    return null;
                }
                // use rawtype
                typeArguments.clear();
                break;
            }
            typeArguments.add(targ);
        }
        
        final TypeMirror enclosingType = dt.getEnclosingType();
        if (enclosingType.getKind() == TypeKind.DECLARED) {
            return info.getTypes().getDeclaredType((DeclaredType) enclosingType, (TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        } else {
            if (dt.asElement() == null) return dt;
            return info.getTypes().getDeclaredType((TypeElement) dt.asElement(), typeArguments.toArray(new TypeMirror[0]));
        }
    }

    if (tm.getKind() == TypeKind.ARRAY) {
        ArrayType at = (ArrayType) tm;
        TypeMirror tm2 = resolveCapturedTypeInt(info, at.getComponentType());
        return info.getTypes().getArrayType(tm2 != null ? tm2 : tm);
    }
    
    return tm;
}
 
Example 17
Source File: SerializerBuilder.java    From domino-jackson with Apache License 2.0 4 votes vote down vote up
private boolean hasTypeJsonInclude() {
    if (beanType.getKind() == TypeKind.DECLARED) {
        return ((DeclaredType) beanType).asElement().getAnnotation(JsonInclude.class) != null;
    }
    return false;
}
 
Example 18
Source File: BoxedIdentityComparison.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@TriggerPatterns({
    @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Integer")),
    @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Byte")),
    @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Short")),
    @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Long")),
    @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Float")),
    @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Double")),
    @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Character")),
    @TriggerPattern(value = "$x == $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Boolean")),

    @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Integer")),
    @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Byte")),
    @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Short")),
    @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Long")),
    @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Float")),
    @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Double")),
    @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Character")),
    @TriggerPattern(value = "$x != $y", constraints = @ConstraintVariableType(variable = "$x", type = "java.lang.Boolean")),

})
public static ErrorDescription wrapperComparisonUsingIdentity(HintContext ctx) {
    TreePath logExprPath = ctx.getPath();
    BinaryTree bt = (BinaryTree)logExprPath.getLeaf();
    TreePath wrapperPath = ctx.getVariables().get("$x"); // NOI18N
    Tree otherOperand = bt.getRightOperand();

    // JLS 15.21; if the other type is primitive, the comparison is correct
    TypeMirror t = ctx.getInfo().getTrees().getTypeMirror(new TreePath(logExprPath, otherOperand));
    if (t == null || t.getKind() != TypeKind.DECLARED) {
        return null;
    }
    t = ctx.getInfo().getTrees().getTypeMirror(wrapperPath);
    // primitive type is assignable to the wrapper, so it will trigger the hint
    if (t == null || t.getKind() != TypeKind.DECLARED) {
        return null;
    }
    final Fix fix;
    
    if (ctx.getInfo().getSourceVersion().compareTo(SourceVersion.RELEASE_7) < 0) {
       fix = null;
    } else {
        fix = new NullSafeEqualsFix(TreePathHandle.create(logExprPath, ctx.getInfo())).toEditorFix();
    }
    
    return ErrorDescriptionFactory.forTree(ctx, logExprPath, 
            TEXT_BoxedValueIdentityComparison(ctx.getInfo().getTypeUtilities().getTypeName(t)), fix);
}
 
Example 19
Source File: SourceUtils.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static TypeMirror resolveCapturedTypeInt(CompilationInfo info, TypeMirror tm) {
    if (tm == null) {
        return tm;
    }
    
    TypeMirror orig = resolveCapturedType(tm);

    if (orig != null) {
        tm = orig;
    }
    
    if (tm.getKind() == TypeKind.WILDCARD) {
        TypeMirror extendsBound = ((WildcardType) tm).getExtendsBound();
        TypeMirror rct = resolveCapturedTypeInt(info, extendsBound != null ? extendsBound : ((WildcardType) tm).getSuperBound());
        if (rct != null) {
            return rct.getKind() == TypeKind.WILDCARD ? rct : info.getTypes().getWildcardType(extendsBound != null ? rct : null, extendsBound == null ? rct : null);
        }
    }
    
    if (tm.getKind() == TypeKind.DECLARED) {
        DeclaredType dt = (DeclaredType) tm;
        TypeElement el = (TypeElement) dt.asElement();
        if (((DeclaredType)el.asType()).getTypeArguments().size() != dt.getTypeArguments().size()) {
            return info.getTypes().getDeclaredType(el);
        }
        
        List<TypeMirror> typeArguments = new LinkedList<>();
        
        for (TypeMirror t : dt.getTypeArguments()) {
            typeArguments.add(resolveCapturedTypeInt(info, t));
        }
        
        final TypeMirror enclosingType = dt.getEnclosingType();
        if (enclosingType.getKind() == TypeKind.DECLARED) {
            return info.getTypes().getDeclaredType((DeclaredType) enclosingType, el, typeArguments.toArray(new TypeMirror[0]));
        } else {
            return info.getTypes().getDeclaredType(el, typeArguments.toArray(new TypeMirror[0]));
        }
    }

    if (tm.getKind() == TypeKind.ARRAY) {
        ArrayType at = (ArrayType) tm;
        TypeMirror componentType = resolveCapturedTypeInt(info, at.getComponentType());
        switch (componentType.getKind()) {
            case VOID:
            case EXECUTABLE:
            case WILDCARD:  // heh!
            case PACKAGE:
                break;
            default:
                return info.getTypes().getArrayType(componentType);
        }
    }
    
    return tm;
}
 
Example 20
Source File: SnakeProcessor.java    From Snake with Apache License 2.0 4 votes vote down vote up
private boolean isSubtypeOfType(TypeMirror typeMirror, String otherType) {
    if (isTypeEqual(typeMirror, otherType)) {
        return true;
    }

    if (typeMirror.getKind() != TypeKind.DECLARED) {
        return false;
    }

    DeclaredType declaredType = (DeclaredType) typeMirror;
    List<? extends TypeMirror> typeArguments = declaredType.getTypeArguments();
    if (typeArguments.size() > 0) {
        StringBuilder typeString = new StringBuilder(declaredType.asElement().toString());
        typeString.append('<');
        for (int i = 0; i < typeArguments.size(); i++) {
            if (i > 0) {
                typeString.append(',');
            }
            typeString.append('?');
        }
        typeString.append('>');
        if (typeString.toString().equals(otherType)) {
            return true;
        }
    }

    Element element = declaredType.asElement();
    if (!(element instanceof TypeElement)) {
        return false;
    }

    TypeElement typeElement = (TypeElement) element;
    TypeMirror superType = typeElement.getSuperclass();
    if (isSubtypeOfType(superType, otherType)) {
        return true;
    }

    for (TypeMirror interfaceType : typeElement.getInterfaces()) {
        if (isSubtypeOfType(interfaceType, otherType)) {
            return true;
        }
    }

    return false;
}