Java Code Examples for javax.lang.model.util.Types#getArrayType()

The following examples show how to use javax.lang.model.util.Types#getArrayType() . 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: ImportAnalysisTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAddImport11() throws IOException {
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("java.util.Map.Entry");
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testAddImport11.pass");
}
 
Example 2
Source File: ImportAnalysisTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAddImport13() throws IOException {
    testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest2.java");
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("java.util.Map.Entry");
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testAddImport13.pass");
}
 
Example 3
Source File: ImportAnalysisTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAddImport14() throws IOException {
    testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest3.java");
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("java.util.Map.Entry");
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testAddImport14.pass");
}
 
Example 4
Source File: ImportAnalysisTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAddImport17() throws IOException {
    testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest6.java");
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("java.lang.String");
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "s", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testAddImport17.pass");
}
 
Example 5
Source File: ImportAnalysisTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testAddImportOrder1() throws IOException {
    testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest7.java");
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("java.util.LinkedList");
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "s", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testAddImportOrder1.pass");
}
 
Example 6
Source File: Util.java    From GoldenGate with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Returns the {@link TypeMirror} for a given {@link Class}.
 *
 * Adapter from https://github.com/typetools/checker-framework/
 */
public static TypeMirror typeFromClass(Types types, Elements elements, Class<?> clazz) {
    if (clazz == void.class) {
        return types.getNoType(TypeKind.VOID);
    } else if (clazz.isPrimitive()) {
        String primitiveName = clazz.getName().toUpperCase();
        TypeKind primitiveKind = TypeKind.valueOf(primitiveName);
        return types.getPrimitiveType(primitiveKind);
    } else if (clazz.isArray()) {
        TypeMirror componentType = typeFromClass(types, elements, clazz.getComponentType());
        return types.getArrayType(componentType);
    } else {
        TypeElement element = elements.getTypeElement(clazz.getCanonicalName());
        if (element == null) {
            throw new IllegalArgumentException("Unrecognized class: " + clazz);
        }
        return element.asType();
    }
}
 
Example 7
Source File: ImportAnalysisTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAddImport8() throws IOException {
    JavaSource src = getJavaSource(testFile);
    Preferences preferences = MimeLookup.getLookup(JavaTokenId.language().mimeType()).lookup(Preferences.class);
    preferences.putBoolean("importInnerClasses", true);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("java.util.Map.Entry");
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    preferences.remove("importInnerClasses");
    assertFiles("testAddImport8.pass");
}
 
Example 8
Source File: ImportAnalysisTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAddImport12() throws IOException {
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            TypeElement map = workingCopy.getElements().getTypeElement("java.util.Map");
            ClassTree nue = make.addClassImplementsClause(clazz, make.QualIdent(map));
            workingCopy.rewrite(clazz, nue);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("java.util.Map.Entry");
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "entry", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testAddImport12.pass");
}
 
Example 9
Source File: ImportAnalysisTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testAddImportSamePackage() throws IOException {
    testFile = getFile(getSourceDir(), getSourcePckg() + "ImportsTest7.java");
    JavaSource src = getJavaSource(testFile);
    Task<WorkingCopy> task = new Task<WorkingCopy>() {

        public void run(WorkingCopy workingCopy) throws IOException {
            workingCopy.toPhase(Phase.RESOLVED);
            CompilationUnitTree cut = workingCopy.getCompilationUnit();
            TreeMaker make = workingCopy.getTreeMaker();
            ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0);
            MethodTree node = (MethodTree) clazz.getMembers().get(0);
            BlockTree body = node.getBody();
            List<StatementTree> stats = new ArrayList<StatementTree>();
            for (StatementTree st : body.getStatements()) {
                stats.add(st);
            }
            TypeElement list = workingCopy.getElements().getTypeElement("org.netbeans.test.codegen.ImportsTest6");
            assertNotNull(list);
            Types types = workingCopy.getTypes();
            TypeMirror tm = types.getArrayType(types.erasure(list.asType()));
            stats.add(make.Variable(make.Modifiers(Collections.<Modifier>emptySet()), "s", make.Type(tm), null));
            workingCopy.rewrite(body, make.Block(stats, false));
        }
    };
    src.runModificationTask(task).commit();
    assertFiles("testAddImportSamePackage.pass");
}
 
Example 10
Source File: TypeMirrors.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
/** Returns a {@link TypeMirror} for the given class (raw T, not T&lt;?&gt;, if T is generic). */
public static TypeMirror typeMirror(
    Types typeUtils,
    Elements elementUtils,
    Class<?> cls) {
  if (cls.equals(void.class)) {
    return typeUtils.getNoType(TypeKind.VOID);
  } else if (cls.isPrimitive()) {
    return typeUtils.getPrimitiveType(TypeKind.valueOf(cls.getSimpleName().toUpperCase()));
  } else if (cls.isArray()) {
    return typeUtils.getArrayType(typeMirror(typeUtils, elementUtils, cls.getComponentType()));
  } else {
    return rawType(typeUtils, elementUtils, cls.getCanonicalName());
  }
}
 
Example 11
Source File: TypeMirrors.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
/** Returns a {@link TypeMirror} for the given type. */
public static TypeMirror typeMirror(Types typeUtils, Elements elementUtils, Type type) {
  if (type instanceof Class) {
    return typeMirror(typeUtils, elementUtils, (Class<?>) type);
  } else if (type instanceof GenericArrayType) {
    Type componentType = ((GenericArrayType) type).getGenericComponentType();
    return typeUtils.getArrayType(typeMirror(typeUtils, elementUtils, componentType));
  } else if (type instanceof ParameterizedType) {
    ParameterizedType pType = (ParameterizedType) type;
    DeclaredType rawType = (DeclaredType) typeMirror(typeUtils, elementUtils, pType.getRawType());
    List<TypeMirror> typeArgumentMirrors = new ArrayList<>();
    for (Type typeArgument : pType.getActualTypeArguments()) {
      typeArgumentMirrors.add(typeMirror(typeUtils, elementUtils, typeArgument));
    }
    DeclaredType owner = (DeclaredType) typeMirror(typeUtils, elementUtils, pType.getOwnerType());
    return typeUtils.getDeclaredType(
        owner,
        (TypeElement) rawType.asElement(),
        typeArgumentMirrors.toArray(new TypeMirror[typeArgumentMirrors.size()]));
  } else if (type instanceof WildcardType) {
    Type lowerBound = getOnlyType(((WildcardType) type).getLowerBounds());
    Type upperBound = getOnlyType(((WildcardType) type).getUpperBounds());
    if (Object.class.equals(upperBound)) {
      upperBound = null;
    }
    return typeUtils.getWildcardType(
        typeMirror(typeUtils, elementUtils, upperBound),
        typeMirror(typeUtils, elementUtils, lowerBound));
  } else if (type == null) {
    return null;
  } else if (type instanceof TypeVariable) {
    throw new IllegalArgumentException("Type variables not supported");
  } else {
    throw new IllegalArgumentException("Unrecognized Type subclass " + type.getClass());
  }
}
 
Example 12
Source File: Utils.java    From paperparcel with Apache License 2.0 4 votes vote down vote up
@Override public TypeMirror visitArray(ArrayType type, Types types) {
  return types.getArrayType(type.getComponentType().accept(this, types));
}