private static String[] correspondingGSNames(Tree member) { if (isSetter(member)) { String name = name(member); VariableTree param = ((MethodTree)member).getParameters().get(0); if (param.getType().getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree)param.getType()).getPrimitiveTypeKind() == TypeKind.BOOLEAN) { return new String[] {'g' + name.substring(1), "is" + name.substring(3)}; } return new String[] {'g' + name.substring(1)}; } if (isGetter(member)) { return new String[] {'s' + name(member).substring(1)}; } if (isBooleanGetter(member)) { return new String[] {"set" + name(member).substring(2)}; //NOI18N } return null; }
public void testPrimitiveTypes() throws Exception { TestUtilities.copyStringToFileObject(testFO, "package foo;" + "public class TestClass {" + "}"); runModificationTask(testFO, new Task<WorkingCopy>() { public void run(WorkingCopy copy) throws Exception { GenerationUtils genUtils = GenerationUtils.newInstance(copy); TypeElement scope = SourceUtils.getPublicTopLevelElement(copy); assertEquals(TypeKind.BOOLEAN, ((PrimitiveTypeTree)genUtils.createType("boolean", scope)).getPrimitiveTypeKind()); assertEquals(TypeKind.BYTE, ((PrimitiveTypeTree)genUtils.createType("byte", scope)).getPrimitiveTypeKind()); assertEquals(TypeKind.SHORT, ((PrimitiveTypeTree)genUtils.createType("short", scope)).getPrimitiveTypeKind()); assertEquals(TypeKind.INT, ((PrimitiveTypeTree)genUtils.createType("int", scope)).getPrimitiveTypeKind()); assertEquals(TypeKind.LONG, ((PrimitiveTypeTree)genUtils.createType("long", scope)).getPrimitiveTypeKind()); assertEquals(TypeKind.CHAR, ((PrimitiveTypeTree)genUtils.createType("char", scope)).getPrimitiveTypeKind()); assertEquals(TypeKind.FLOAT, ((PrimitiveTypeTree)genUtils.createType("float", scope)).getPrimitiveTypeKind()); assertEquals(TypeKind.DOUBLE, ((PrimitiveTypeTree)genUtils.createType("double", scope)).getPrimitiveTypeKind()); } }); }
@Override public AnnotatedTypeMirror visitMemberSelect(MemberSelectTree node, AnnotatedTypeFactory f) { Element elt = TreeUtils.elementFromUse(node); if (elt.getKind().isClass() || elt.getKind().isInterface()) return f.fromElement(elt); // The expression might be a primitive type (as in "int.class"). if (!(node.getExpression() instanceof PrimitiveTypeTree)) { // TODO: why don't we use getSelfType here? if (node.getIdentifier().contentEquals("this")) { return f.getEnclosingType((TypeElement)InternalUtils.symbol(node.getExpression()), node); } // We need the original t with the implicit annotations AnnotatedTypeMirror t = f.getAnnotatedType(node.getExpression()); if (t instanceof AnnotatedDeclaredType) return AnnotatedTypes.asMemberOf(f.types, f, t, elt); } return f.fromElement(elt); }
private String createHashCodeLineForField(VariableTree field) { Name fieldName = field.getName(); Tree fieldType = field.getType(); if (fieldType.getKind() == Tree.Kind.PRIMITIVE_TYPE) { if (((PrimitiveTypeTree) fieldType).getPrimitiveTypeKind() == TypeKind.BOOLEAN) { return "hash += (" + fieldName + " ? 1 : 0"; // NOI18N } return "hash += (int)" + fieldName + ";"; // NOI18N } return "hash += (" + fieldName + " != null ? " + fieldName + ".hashCode() : 0);"; // NOI18N }
@Override public Tree visitPrimitiveType(PrimitiveTypeTree tree, Void p) { PrimitiveTypeTree n = make.PrimitiveType(tree.getPrimitiveTypeKind()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }
public Boolean visitPrimitiveType(PrimitiveTypeTree node, TreePath p) { if (p == null) return super.visitPrimitiveType(node, p); PrimitiveTypeTree t = (PrimitiveTypeTree) p.getLeaf(); return node.getPrimitiveTypeKind() == t.getPrimitiveTypeKind(); }
private static boolean isSetter(Tree member) { return member.getKind() == Tree.Kind.METHOD && name(member).startsWith("set") //NOI18N && ((MethodTree)member).getParameters().size() == 1 && ((MethodTree)member).getReturnType().getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree)((MethodTree)member).getReturnType()).getPrimitiveTypeKind() == TypeKind.VOID; }
private static boolean isGetter(Tree member) { return member.getKind() == Tree.Kind.METHOD && name(member).startsWith("get") //NOI18N && ((MethodTree)member).getParameters().isEmpty() && (((MethodTree)member).getReturnType().getKind() != Tree.Kind.PRIMITIVE_TYPE || ((PrimitiveTypeTree)((MethodTree)member).getReturnType()).getPrimitiveTypeKind() != TypeKind.VOID); }
private static boolean isBooleanGetter(Tree member) { return member.getKind() == Tree.Kind.METHOD && name(member).startsWith("is") //NOI18N && ((MethodTree)member).getParameters().isEmpty() && ((MethodTree)member).getReturnType().getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree)((MethodTree)member).getReturnType()).getPrimitiveTypeKind() == TypeKind.BOOLEAN; }
/** * Changes long type of the field to short type. */ public void testFieldType() throws IOException { System.err.println("testFieldType"); process( new Transformer<Void, Object>() { public Void visitVariable(VariableTree node, Object p) { super.visitVariable(node, p); if ("typeField".contentEquals(node.getName())) { PrimitiveTypeTree pt = make.PrimitiveType(TypeKind.SHORT); VariableTree vt = make.Variable( node.getModifiers(), node.getName(), pt, node.getInitializer() ); model.setElement(vt, model.getElement(node)); model.setType(vt, model.getType(node)); model.setPos(vt, model.getPos(node)); //copy.rewrite(node.getType(), tree); copy.rewrite(node, vt); } return null; } } ); assertFiles("testFieldType.pass"); }
public void testAddFieldToIndex0() throws Exception { testFile = new File(getWorkDir(), "Test.java"); TestUtilities.copyStringToFile(testFile, "package hierbas.del.litoral;\n\n" + "import java.io.*;\n\n" + "public class Test {\n" + " public void taragui() {\n" + " }\n" + "}\n" ); String golden = "package hierbas.del.litoral;\n\n" + "import java.io.*;\n\n" + "public class Test {\n" + "\n" + " int field1;\n" + " public void taragui() {\n" + " }\n" + "}\n"; process( new Transformer<Void, Object>() { public Void visitClass(ClassTree node, Object p) { super.visitClass(node, p); ModifiersTree mods = make.Modifiers(Collections.<Modifier>emptySet()); PrimitiveTypeTree type = make.PrimitiveType(TypeKind.INT); VariableTree var = make.Variable(mods, "field1", type, null); ClassTree copy = make.insertClassMember(node, 0, var); this.copy.rewrite(node, copy); return null; } } ); String res = TestUtilities.copyFileToString(testFile); assertEquals(golden, res); }
private void addIfMatchMethod(final LocationResult location, final TestLocator testLocator, final List<RenameRefactoring> renameRefactoringsList) { if(location.getFileObject() != null && testLocator.getFileType(location.getFileObject()).equals(TestLocator.FileType.TEST)) { try { JavaSource.forFileObject(location.getFileObject()).runUserActionTask(new Task<CompilationController>() { @Override public void run(CompilationController javac) throws Exception { final Element methodElement = treePathHandle.resolveElement(javac); String methodName = methodElement.getSimpleName().toString(); String testMethodName = RefactoringUtils.getTestMethodName(methodName); javac.toPhase(JavaSource.Phase.RESOLVED); CompilationUnitTree cut = javac.getCompilationUnit(); Tree classTree = cut.getTypeDecls().get(0); List<? extends Tree> members = ((ClassTree) classTree).getMembers(); for (int i = 0; i < members.size(); i++) { Tree member = members.get(i); if(member.getKind() != Tree.Kind.METHOD) { continue; } MethodTree methodTree = (MethodTree) member; if (methodTree.getName().contentEquals(testMethodName) && methodTree.getReturnType().getKind() == Tree.Kind.PRIMITIVE_TYPE && ((PrimitiveTypeTree) methodTree.getReturnType()).getPrimitiveTypeKind() == TypeKind.VOID) { // test method should at least be void classTree = ((ClassTree) classTree).getMembers().get(i); TreePath tp = TreePath.getPath(cut, classTree); RenameRefactoring renameRefactoring = new RenameRefactoring(Lookups.singleton(TreePathHandle.create(tp, javac))); renameRefactoring.setNewName(RefactoringUtils.getTestMethodName(refactoring.getNewName())); renameRefactoring.setSearchInComments(true); renameRefactoringsList.add(renameRefactoring); break; } } } }, true); } catch (IOException ex) { Exceptions.printStackTrace(ex); } } }
@Override public Void visitPrimitiveType(PrimitiveTypeTree tree, List<Node> d) { List<Node> below = new ArrayList<Node>(); addCorrespondingType(below); addCorrespondingComments(below); super.visitPrimitiveType(tree, below); d.add(new TreeNode(info, getCurrentPath(), below)); return null; }
public void testCreateMethodTree() throws Exception { final MethodModel methodModel = MethodModel.create( "method", "void", "{ String name; }", // for now, Retouche requires those parenthesis (they won't appear in file) Collections.<MethodModel.Variable>emptyList(), Collections.<String>emptyList(), Collections.<Modifier>emptySet() ); TestUtilities.copyStringToFileObject(testFO, "package foo;" + "public class TestClass {" + "}"); runModificationTask(testFO, new Task<WorkingCopy>() { public void run(WorkingCopy workingCopy) throws IOException { workingCopy.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); MethodTree methodTree = MethodModelSupport.createMethodTree(workingCopy, methodModel); assertEquals(0, methodTree.getModifiers().getFlags().size()); PrimitiveTypeTree returnTypeTree = (PrimitiveTypeTree) methodTree.getReturnType(); assertTrue(TypeKind.VOID == returnTypeTree.getPrimitiveTypeKind()); assertTrue(methodTree.getName().contentEquals("method")); assertEquals(0, methodTree.getParameters().size()); assertEquals(0, methodTree.getThrows().size()); List<? extends StatementTree> statements = methodTree.getBody().getStatements(); assertEquals(1, statements.size()); } }); }
@Override public Mirror visitPrimitiveType(PrimitiveTypeTree arg0, EvaluationContext evaluationContext) { TypeKind type = arg0.getPrimitiveTypeKind(); VirtualMachine vm = evaluationContext.getDebugger().getVirtualMachine(); if (vm == null) { return null; } switch(type) { case BOOLEAN: return vm.mirrorOf(true).type(); case BYTE: return vm.mirrorOf((byte) 0).type(); case CHAR: return vm.mirrorOf('a').type(); case DOUBLE: return vm.mirrorOf(0.).type(); case FLOAT: return vm.mirrorOf(0f).type(); case INT: return vm.mirrorOf(0).type(); case LONG: return vm.mirrorOf(0l).type(); case SHORT: return vm.mirrorOf((short) 0).type(); case VOID: return vm.mirrorOfVoid().type(); default: throw new IllegalStateException("Tree = "+arg0); } }
@Override public Void visitPrimitiveType(PrimitiveTypeTree node, Void unused) { sync(node); switch (node.getPrimitiveTypeKind()) { case BOOLEAN: token("boolean"); break; case BYTE: token("byte"); break; case SHORT: token("short"); break; case INT: token("int"); break; case LONG: token("long"); break; case CHAR: token("char"); break; case FLOAT: token("float"); break; case DOUBLE: token("double"); break; case VOID: token("void"); break; default: throw new AssertionError(node.getPrimitiveTypeKind()); } return null; }
@Test void testVoidLambdaParameter() throws IOException { String code = "package t; class Test { " + "Runnable r = (void v) -> { };" + "}"; DiagnosticCollector<JavaFileObject> coll = new DiagnosticCollector<>(); JavacTaskImpl ct = (JavacTaskImpl) tool.getTask(null, fm, coll, null, null, Arrays.asList(new MyFileObject(code))); CompilationUnitTree cut = ct.parse().iterator().next(); ClassTree clazz = (ClassTree) cut.getTypeDecls().get(0); VariableTree field = (VariableTree) clazz.getMembers().get(0); assertEquals("actual kind: " + field.getInitializer().getKind(), field.getInitializer().getKind(), Kind.LAMBDA_EXPRESSION); LambdaExpressionTree lambda = (LambdaExpressionTree) field.getInitializer(); assertEquals("actual parameters: " + lambda.getParameters().size(), lambda.getParameters().size(), 1); Tree paramType = lambda.getParameters().get(0).getType(); assertEquals("actual parameter type: " + paramType.getKind(), paramType.getKind(), Kind.PRIMITIVE_TYPE); TypeKind primitiveTypeKind = ((PrimitiveTypeTree) paramType).getPrimitiveTypeKind(); assertEquals("actual parameter type: " + primitiveTypeKind, primitiveTypeKind, TypeKind.VOID); }
@Override public Void visitPrimitiveType(PrimitiveTypeTree tree, VisitorState visitorState) { VisitorState state = visitorState.withPath(getCurrentPath()); for (PrimitiveTypeTreeMatcher matcher : primitiveTypeMatchers) { if (!isSuppressed(matcher, state)) { try { reportMatch(matcher.matchPrimitiveType(tree, state), tree, state); } catch (Throwable t) { handleError(matcher, t); } } } return super.visitPrimitiveType(tree, state); }
@Override public Number visitTypeCast(TypeCastTree node, Void p) { Number value = node.getExpression().accept(this, null); if (value == null) { return null; } if (!(node.getType() instanceof PrimitiveTypeTree)) { return null; } TypeKind kind = ((PrimitiveTypeTree) node.getType()).getPrimitiveTypeKind(); return cast(kind, value); }
private boolean returnsVoid(MethodTree methodNode) { if (methodNode.getReturnType().getKind().equals(Tree.Kind.PRIMITIVE_TYPE)) { PrimitiveTypeTree retType = (PrimitiveTypeTree) methodNode.getReturnType(); return retType.getPrimitiveTypeKind().equals(TypeKind.VOID); } return false; }
@Override public Void visitPrimitiveType(PrimitiveTypeTree expected, Tree actual) { Optional<PrimitiveTypeTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } checkForDiff(expected.getPrimitiveTypeKind() == other.get().getPrimitiveTypeKind(), "Expected primitive type kind to be <%s> but was <%s>.", expected.getPrimitiveTypeKind(), other.get().getPrimitiveTypeKind()); return null; }
public Boolean visitPrimitiveType(PrimitiveTypeTree node, ConstructorData p) { super.visitPrimitiveType(node, p); return null; }
private String simpleName(Tree t) { if (t == null) { return Bundle.DisplayName_Unknown(); } if (t.getKind() == Kind.IDENTIFIER) { return ((IdentifierTree) t).getName().toString(); } if (t.getKind() == Kind.MEMBER_SELECT) { return ((MemberSelectTree) t).getIdentifier().toString(); } if (t.getKind() == Kind.METHOD_INVOCATION) { return scan(t, null); } if (t.getKind() == Kind.PARAMETERIZED_TYPE) { return simpleName(((ParameterizedTypeTree) t).getType()) + "<...>"; // NOI18N } if (t.getKind() == Kind.ARRAY_ACCESS) { return simpleName(((ArrayAccessTree) t).getExpression()) + "[]"; //NOI18N } if (t.getKind() == Kind.PARENTHESIZED) { return "(" + simpleName(((ParenthesizedTree)t).getExpression()) + ")"; //NOI18N } if (t.getKind() == Kind.TYPE_CAST) { return simpleName(((TypeCastTree)t).getType()); } if (t.getKind() == Kind.ARRAY_TYPE) { return simpleName(((ArrayTypeTree)t).getType()); } if (t.getKind() == Kind.PRIMITIVE_TYPE) { return ((PrimitiveTypeTree) t).getPrimitiveTypeKind().name().toLowerCase(); } throw new IllegalStateException("Currently unsupported kind of tree: " + t.getKind()); // NOI18N }
@Override public List<? extends TypeMirror> visitPrimitiveType(PrimitiveTypeTree node, Object p) { return null; }
public Void visitPrimitiveType(PrimitiveTypeTree node, Void ignore) { // The following call of getPrimitiveTypeKind should not throw an AssertionError System.out.println(node + " " + node.getPrimitiveTypeKind()); return null; }
@Override public R visitPrimitiveType(PrimitiveTypeTree ptt, P p) { return null; }
@Override public List<T> visitPrimitiveType(PrimitiveTypeTree node, T p) { return checkForCriteria(node); }
@Override public PrimitiveTypeTree createPrimitiveType(Token type, TypeKind kind) { return new PrimitiveTypeTreeImpl(new TokenJCCAdapter(type), kind); }
@Override public Object visitPrimitiveType(PrimitiveTypeTree t, Trees p) { info("PrimitiveTypeTree" + CL + t.getKind() + SP + t); return super.visitPrimitiveType(t, p); }
@Override public Pair<ASTRecord, Integer> visitPrimitiveType(PrimitiveTypeTree node, Insertion ins) { dbug.debug("TypePositionFinder.visitPrimitiveType(%s)%n", node); return pathAndPos((JCTree) node); }
@Override @Nullable public Unifier visitPrimitiveType(PrimitiveTypeTree tree, @Nullable Unifier unifier) { return getPrimitiveTypeKind().equals(tree.getPrimitiveTypeKind()) ? unifier : null; }
@Override public UPrimitiveTypeTree visitPrimitiveType(PrimitiveTypeTree tree, Void v) { return UPrimitiveTypeTree.create(tree.getPrimitiveTypeKind()); }