void changeTypeContext(ResolutionContext old, ResolutionContext new_, MethodDeclaration m) { m.setType(changeTypeContext(old, new_, m.getType())); m.setBody( new BlockStmt( Collections.singletonList( new ThrowStmt( new ObjectCreationExpr( null, new ClassOrInterfaceType("UnsupportedOperationException"), Collections.emptyList()))))); NodeUtil.forChildren( m, node -> { Expression scope = node.getScope(); // TODO: Currently guesses that it's a type name if first character is uppercase. // Should check for fields/variables which match instead if (scope instanceof NameExpr && Character.isUpperCase(((NameExpr) scope).getName().charAt(0))) { String name = ((NameExpr) scope).getName(); node.setScope(ASTHelper.createNameExpr(new_.typeToString(old.resolve(name)))); } }, MethodCallExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), VariableDeclarationExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), TypeExpr.class); NodeUtil.forChildren( m, node -> node.setType(changeTypeContext(old, new_, node.getType())), com.github.javaparser.ast.body.Parameter.class); }
@Override public void add(MethodInfo method) { MethodDeclaration methodDeclaration; if (method instanceof MethodDeclarationWrapper) { val wrapper = (MethodDeclarationWrapper) method; methodDeclaration = (MethodDeclaration) wrapper.declaration.clone(); methodDeclaration.setAnnotations(Collections.emptyList()); wrapper .getClassInfo() .changeTypeContext(wrapper.getContext(), getContext(), methodDeclaration); } else { methodDeclaration = new MethodDeclaration(); new MethodDeclarationWrapper(methodDeclaration).setAll(method); } addMember(methodDeclaration); }
@Override public void add(FieldInfo field) { FieldDeclaration fieldDeclaration; if (field instanceof FieldDeclarationWrapper) { val wrapper = (FieldDeclarationWrapper) field; fieldDeclaration = (FieldDeclaration) wrapper.declaration.clone(); fieldDeclaration.setAnnotations(Collections.emptyList()); changeTypeContext(wrapper.getContext(), getContext(), fieldDeclaration); } else { fieldDeclaration = new FieldDeclaration(); val vars = new ArrayList<VariableDeclarator>(); vars.add(new VariableDeclarator(new VariableDeclaratorId("unknown"))); fieldDeclaration.setVariables(vars); new FieldDeclarationWrapper(fieldDeclaration).setAll(field); } addMember(fieldDeclaration); val result = new FieldDeclarationWrapper(fieldDeclaration); if (!field.similar(result)) throw new TransformationException( "After adding to class, didn't match. added: " + field + " result: " + result); }
@Override public List<TypeVariable> getTypeVariables() { return Collections.emptyList(); }